When oauth/token is called, you can't access

Hello

I’m Japanese. I’m contributing using the translation function.
Please permit clumsy English.

I give up a request in oauth/tokne using axios at present, I’m in trouble because the following error goes out.

error: "access_denied"
error_description: "Unauthorized"

Actual source cord

var options = {
  method: 'POST',
  url: 'https://MY_DOMAIN/oauth/token',
  headers: {
      'content-type': 'application/x-www-form-urlencoded'
  },
  data: {
    grant_type: 'client_credentials',
    client_id: 'MY_CLIENT_ID',
    client_secret: 'MY_CLIENT_SECRET',
    audience: 'https://MY_DOMAIN/api/v2/',
  }
}

axios.request(options).then(function (response) {
  console.log(response)
}).catch(function (error) {
  console.error(error)
})

But, when a request is sent by the following curl command, a normal result is returning.

curl --request POST \
  --url 'https://MY_DOMAIN/oauth/token' \
  --header 'content-type: application/x-www-form-urlencoded' \
  --data grant_type=client_credentials \
  --data 'client_id=MY_CLIENT_ID' \
  --data client_secret=MY_CLIENT_SECRET \
  --data 'audience=https://MY_DOMAIN/api/v2/'  

What is the cause? Please help!

You’re sending an header that specifies that the request body being sent is in the application/x-www-form-urlencoded format, however, you don’t seem to following the recommendation at (https://github.com/axios/axios#using-applicationx-www-form-urlencoded-format) when sending data in such format.

1 Like

Thank you very much for your answer.

It was possible to acquire a right result by the following mounting.

const qs = require('qs')

const data = {
    grant_type: 'client_credentials',
    client_id: 'MY_CLIENT_ID',
    client_secret: 'MY_CLIENT_SECRET',
    audience: 'https://MY_DOMAIN/api/v2/',
}

var options = {
  method: 'POST',
  url: 'https://MY_DOMAIN/oauth/token',
  headers: {
      'content-type': 'application/x-www-form-urlencoded'
  },
  data: qs.stringy(data),
}

axios.request(options).then(function (response) {
  console.log(response)
}).catch(function (error) {
  console.error(error)
})

Thank you very much really!

1 Like

Thanks for sharing the code snippet with the rest of community!

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