# Get AccessToken to authorize API

**URL:** <https://community.auth0.com/t/get-accesstoken-to-authorize-api/70426>\
**Category:** Get Help\
**Tags:** nodejs, access\_token, api-authorization\
**Created:** [September 28, 2021, 4:14pm UTC](https://community.auth0.com/t/get-accesstoken-to-authorize-api/70426 "2021-09-28T16:14:17Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![camediaonline](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/camediaonline/32/12811_2.png) [@camediaonline](https://community.auth0.com/u/camediaonline)\
**Post date:** [September 28, 2021, 4:14pm UTC](https://community.auth0.com/t/get-accesstoken-to-authorize-api/70426/1 "2021-09-28T16:14:17Z")

</div>

Hi there,

I have just started with Auth0, got through some videos read the docs and made an small local nodejs client app (localhost:3000) that could authenticate using the forms from Auth0.  
Then i took a look howto protect an Api and how to access it from that client.  
What i have so far:

Code:

- NodeJs Client App on localhost 3000
- NodeJs API on localhost 3001 - sample code from Auth0 doc that checks the jwt token

Auth0:

- Created an Client called ExpressClient (set the urls; login with id-token works with NodeJS client app)
- Created an API called ExpressAPI
  - Setup M2M and authorized the “ExpressClient”

Code:

- Added route “/token” with code to get an accss-token

```auto
var options = {
    method: 'POST',
    url: 'https://myurl.com/oauth/token',
    headers: {'content-type': 'application/x-www-form-urlencoded'},
    data: {
      grant_type: 'client_credentials',
      client_id: process.env.CLIENT_ID,
      client_secret: process.env.SECRET,
      audience: process.env.API_IDENTIFIER
    }
  };

   axios.request(options)
   .then( (response) => {    
    res.send(
      `<h1>My Token</h1>    
       <p>${stringify(response.data)}</p>
      `)
   })
  .catch((error) => {
    res.send(
      `<h1>My Token</h1>    
      <hr>
      <h2>${error}</h2>       
      `)

  });

```

The result is always an 401.  
I expected by the given configuration in [Auth0.com](http://Auth0.com), that my client is recognized by client\_id and Secret  
and so i would get back an access\_token.

What am i doing wrong here?

Thanks and regards  
Antonio

---

<div class="post-metadata">

**Author:** ![dan.woda](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/dan.woda/32/18340_2.png) [@dan.woda](https://community.auth0.com/u/dan.woda)\
**Post date:** [September 29, 2021, 10:25pm UTC](https://community.auth0.com/t/get-accesstoken-to-authorize-api/70426/3 "2021-09-29T22:25:23Z")

</div>

Hi @camediaonline,

Welcome to the Auth0 Community!

Have you authorized the application to request a token for the API? Does the error provide any more information?

> **[API Settings](https://auth0.com/docs/get-started/apis/api-settings)**
>
> Describes the settings related to APIs available in the Auth0 Dashboard.

---

<div class="post-metadata">

**Author:** ![camediaonline](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/camediaonline/32/12811_2.png) [@camediaonline](https://community.auth0.com/u/camediaonline)\
**Post date:** [September 30, 2021, 6:17am UTC](https://community.auth0.com/t/get-accesstoken-to-authorize-api/70426/4 "2021-09-30T06:17:10Z")

</div>

Hi Dan,

on “Applications \> APIs”, Tab “Machine to Machine Applications” i set client to authorized. I did not expand the client item there and did not set any further information, said “Permissions”.

I have tried two different approaches:

NodeJS Client App

1. using content-type: application/x-www-form-urlencoded

```auto
error.response.status: 401
error.response.data: 
[
  {"error":"1"," error_description":"2"},
   "access_denied",
   "Unauthorized"
]

```

1. using content-type: application/json

```auto
error.response.status: 403
error.response.data:
[
  {"error":"1","error_description":"2","error_uri":"3"},
   "unauthorized_client","Grant type 'client_credentials' not allowed for the client.",
   "https://auth0.com/docs/clients/client-grant-types"
]

```

I’ve also tried using Postman with _application/json_ there i got also 403 and it produced an log entry in Dashboard, but not when using the NodeJS Client App.

That’s it. I hope you have some ideas for me…

Thanks and regards  
Antonio

---

<div class="post-metadata">

**Author:** ![dan.woda](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/dan.woda/32/18340_2.png) [@dan.woda](https://community.auth0.com/u/dan.woda)\
**Post date:** [September 30, 2021, 5:39pm UTC](https://community.auth0.com/t/get-accesstoken-to-authorize-api/70426/5 "2021-09-30T17:39:20Z")

</div>

I think it might be how you are encoding your form data. See this doc:

[https://axios-http.com/docs/urlencoded](https://axios-http.com/docs/urlencoded)

---

<div class="post-metadata">

**Author:** ![camediaonline](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/camediaonline/32/12811_2.png) [@camediaonline](https://community.auth0.com/u/camediaonline)\
**Post date:** [September 30, 2021, 6:26pm UTC](https://community.auth0.com/t/get-accesstoken-to-authorize-api/70426/6 "2021-09-30T18:26:06Z")

</div>

Hi dan,

thanks for your reply. I think that was the problem for the 401 error caused in the client app.  
Now i get an 403, and this time it is also shown in the logs.

So the main problem seems to be the grant type and that client\_credentials are not allowed for the client. If i now have understood it the right way, this flow is reserved for M2M Applications, and i have configured the client app as “Regular Web Application”. So i need to follow an other kind of flow. I will try these out first.

Thanks again. I will accept your answer as the solving one, because in this case it’s the 401 that misleaded me, and for that you pointed to the solution. The 403 error is the right one, that gives the feedback what really is wrong in the way i try to receive an access-token.

Feel free to correct me if i’m wrong.

Thanks and regards  
Antonio

---

<div class="post-metadata">

**Author:** ![system](https://global.discourse-cdn.com/auth0/original/3X/b/5/b560f266932fcce5d57f09a6595bd50518cae3f3.jpeg) [@system](https://community.auth0.com/u/system)\
**Post date:** [October 15, 2021, 6:26pm UTC](https://community.auth0.com/t/get-accesstoken-to-authorize-api/70426/7 "2021-10-15T18:26:42Z")

</div>

This topic was automatically closed 15 days after the last reply. New replies are no longer allowed.
