I'm looking to authenticate using OAuth2 with Azure AD.
server.js
const express = require("express");
const app = express();
const port = process.env.PORT || 5000;
var bodyParser = require("body-parser");
app.use(bodyParser.urlencoded({ extended: false }));
app.use(bodyParser.json());
app.get("/authorize", async (req, res) => {
  const credentials = {
    client: {
      id: "xxx",
      secret: "xxx"
    },
    auth: {
      tokenHost:
        "xxx"
    }
  };
  const oauth2 = require("simple-oauth2").create(credentials);
  const tokenConfig = {
    scope: "<scope>" 
  };
  const httpOptions = {};
  try {
    const result = await oauth2.clientCredentials.getToken(
      tokenConfig,
      httpOptions
    );
    const accessToken = oauth2.accessToken.create(result);
  } catch (error) {
    console.log("Access Token error", error.message);
  }
I followed the example provided by the repository but I'm getting an error Access Token error The content-type is not JSON compatible.
How can I authorize my OAuth2 with Microsoft Azure using NodeJS and Simple OAuth2?
I came across this error also. The reason in my case was that the oAuth2 server was responding with a content-type header of text/plain - even though the body was actually JSON.
Changing the "json" configuration option to "force" in the http options of simple-oauth2 solved it for me.
This is because you are only using tokenHost you also need to add tokenPath.
Example:
auth: {
    tokenHost: 'https://example.com',
    tokenPath: '/path/to/token/endpoint/'
}
                        If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With