将 Simple OAuth2 与 NodeJS 一起使用时,内容类型与 JSON 不兼容

Car*_*ein 5 azure node.js

我希望通过 Azure AD使用OAuth2进行身份验证。

服务器.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);
  }
Run Code Online (Sandbox Code Playgroud)

我按照存储库提供的示例进行操作,但出现错误Access Token error The content-type is not JSON compatible

如何使用 NodeJS 和 Simple OAuth2 通过 Microsoft Azure 授权我的 OAuth2?

opr*_*mus 4

我也遇到过这个错误。在我的例子中,原因是 oAuth2 服务器使用 text/plain 的内容类型标头进行响应 - 即使正文实际上是 JSON。

将 simple-oauth2 的 http 选项中的“json”配置选项更改为“force”为我解决了这个问题。

  • 我设法通过将其更改为 true `.getToken(tokenConfig, {json: true})` 来修复它 (7认同)