Dshiz
Dshiz

Reputation: 3331

How to get a client token from Azure for Microsoft Graph using nodejs and node-fetch

I've been trying to follow the direction here and applying it to nodejs.

I'm getting the following error:

  error: 'invalid_request',
  error_description: "AADSTS900144: The request body must contain the following parameter: 'grant_type'.\r\n" +
    'Trace ID: <trace id>\r\n' +
    'Correlation ID: <correlation id>\r\n' +
    'Timestamp: 2021-05-12 22:27:30Z',
  error_codes: [ 900144 ],
  timestamp: '2021-05-12 22:27:30Z',
  trace_id: '<trace id>',
  correlation_id: '<correlation id>',
  error_uri: 'https://login.microsoftonline.com/error?code=900144'

This is my code.

const fetch = require('node-fetch');
let  tenantId='<my tenant id>';


    let token = fetch(`https://login.microsoftonline.com/${tenantId}/oauth2/v2.0/token`, {
        method: 'post',
        headers: {'Content-Type': 'application/json'},
        body: JSON.stringify({
            client_id: '<my client id>',
            scope: 'https://graph.microsoft.com',
            client_secret: '<my client secret>',
            grant_type: 'client_credentials',
        })
      }).then(function(response) {
        return response.json()
      }).then(json => {
          console.log(json)
      })

What doesn't it like about my body that it doesn't see 'grant_type' in it?

Another way to ask this question, might be, How to convert this curl command to a node-fetch request using POST:

# Replace {tenant} with your tenant!
curl -X POST -H "Content-Type: application/x-www-form-urlencoded" -d 'client_id=535fb089-9ff3-47b6-9bfb-4f1264799865&scope=https%3A%2F%2Fgraph.microsoft.com%2F.default&client_secret=qWgdYAmab0YSkuL1qKv5bPX&grant_type=client_credentials' 'https://login.microsoftonline.com/{tenant}/oauth2/v2.0/token'

I should note that this CURL example does work work correctly when I pass my tenant ID, client ID and secret.

What is wrong with my fetch body?

Upvotes: 1

Views: 1179

Answers (2)

David
David

Reputation: 543

I'd rather use FormData or URLSearchParams and then toString() rather than literal String:

let postFormData = new URLSearchParams();
postFormData.append('scope', 'https://graph.microsoft.com/.default')
postFormData.append('grant_type', 'client_credentials')
Object.keys(appConfig).map((key, index, array) => {
    postFormData.append(key, Object.values(appConfig)[index])
})
try {
    const res = await fetch(`https://login.microsoftonline.com/${appConfig.dirId}/oauth2/v2.0/token`, {
    method: "POST",
    headers: {
      'Content-Type': 'application/x-www-form-urlencoded'
    },
    body: postFormData.toString()
  }
)
const response = (await res.json())
return response
} catch (e) {
  console.log(e)
  throw Error('Error getting auth token')
}

Upvotes: 1

Stanley Gong
Stanley Gong

Reputation: 12153

Try this :

const fetch = require('node-fetch');

let  tenantId='';
let  clientID = '';
let clientSecret = '';

let token = fetch(`https://login.microsoftonline.com/${tenantId}/oauth2/v2.0/token`, {
    method: 'post',
    headers: {'Content-Type': 'application/x-www-form-urlencoded'},
    body: `client_id=${clientID}&Client_secret=${clientSecret}&grant_type=client_credentials&scope=https://graph.microsoft.com/.default`
    }).then(function(response) {
    return response.json()
    }).then(json => {
        console.log(json)
    })

Result:

enter image description here

Upvotes: 3

Related Questions