node.jsoauth-2.0httprequestmicrosoft-graph-apinpm-request

The request body must contain the following parameter: 'grant_type'... but I put it in the body


I put the grant_type parameter in the request body as the documentation shows, but Microsoft Graph still complains that it's not there;

const tokenRequestBody = [
    "grant_type=client_credentials",
    "scope=https%3A%2F%2Fgraph.microsoft.com%2F.default",
    `client_secret=${config.appClient.password}`
].join("&");

request.post(
    {
        url: tokenRequestUrl,
        json: true,
        headers: {
            "content-type": "application/application/x-www-form-urlencoded"
        },
        body: tokenRequestBody
    },
    (err, req, body) => {
        console.log(body.error_description); 
        // Logs: The request body must contain the following parameter: 'grant_type'.

    }
);

Solution

  • I figured it out. I had to use form instead of body for the request node module.