"The token request must specify a 'grant_type" when sending a token request with value with Got

496 Views Asked by At

I am using JavaScript for API programming to monitor URL's health check. This is for API monitoring in AppDynamics. Need to use GOT library.

I am checking response of the HTTP request.

const assert = require("assert");

(async() => {
  var url = "https://example.com/oauth2/v1/token";
  var headers = {
    'Accept': 'application/json',
    'Content-Type': 'application/x-www-form-urlencoded',
    'Authorization': 'Basic xyz'
  };
  var payload = {
    'grant_type': 'password',
    'scope': 'okta.groups.read okta.users.read',
    'username': 'userid',
    'password': 'xxxxx'
  };
  var response = await client.post(url, {
    headers: headers,
    json: payload,
  })
  console.log(response.statusCode);
  console.log(response.statusMessage);
  console.log(response.body);

})();

But I see below response.

{"error":"invalid_request","error_description":"The token request must specify a 'grant_type'. Valid values: [password, authorization_code]"}

I did clearly mention 'grant_type': 'password' in the code with payload variable. I admit, I am new to JavaScript, but I looked at lot of online examples, but still could not figure out, why got library does not recognize payload.

0

There are 0 best solutions below