【问题标题】:Content-type is not JSON compatible while using Simple OAuth2 with NodeJS将 Simple OAuth2 与 NodeJS 一起使用时,内容类型与 JSON 不兼容
【发布时间】:2020-02-22 22:29:46
【问题描述】:

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

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);
  }

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

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

【问题讨论】:

标签: node.js azure


【解决方案1】:

这样设置auth属性

  auth: {
    tokenHost: 'https://login.microsoftonline.com/common/',
    tokenPath: 'oauth2/v2.0/token',
    authorizePath: 'oauth2/v2.0/authorize',
  },

【讨论】:

    【解决方案2】:

    这是因为你只使用tokenHost你还需要添加tokenPath

    例子:

    auth: {
        tokenHost: 'https://example.com',
        tokenPath: '/path/to/token/endpoint/'
    }
    

    【讨论】:

      【解决方案3】:

      我发现了这个:https://github.com/lelylan/simple-oauth2/issues/279#issuecomment-569733862

      确保在示例中更改了三个 url。没有{json: true},它对我有用

      此解决方案不同于Microsoft tutorial

      【讨论】:

        【解决方案4】:

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

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

        【讨论】:

        • 你能告诉我实际的代码吗?提前致谢!
        • 我设法将其更改为 true .getToken(tokenConfig, {json: true})
        猜你喜欢
        • 1970-01-01
        • 2020-08-05
        • 2012-04-29
        • 1970-01-01
        • 2013-05-02
        • 1970-01-01
        • 2020-06-30
        • 2022-01-11
        • 1970-01-01
        相关资源
        最近更新 更多