【问题标题】:OAuth 2.0 in Node js Access Token Error The content-type is not JSON compatibleOAuth 2.0 in Node js Access Token Error The content-type is not JSON compatible
【发布时间】:2020-05-28 11:34:05
【问题描述】:

我正在尝试在 Node js 的 API REST 中使用 OAuth 2.0,但我无法生成 access_token

当我尝试生成 access_token 时出现下一个错误 Access Token Error The content-type is not JSON compatible

我有下一个代码

import { Request, Response } from 'express';

    class OAuthController {

        async obtenerToken(req: Request, res: Response) {

            try {
                console.log(req.body)
                let grant_type = req.body.grant_type;
                let username = req.body.username;
                let password = req.body.password;
                let client_id = req.body.client_id;
                let client_secret = req.body.client_secret;
                let scope = req.body.scope;

                if (grant_type === "password" && client_id === "clientIdPDEZacatecas" && client_secret === "clientSecretPDEZacatecas") {
                    //Buscamos en mongo si el usuario existe con los parametros usuario y password
                    let existeUsuario = true;

                    if (existeUsuario == true) {
                        //GENERAMOS EL TOKEN

                        const credentials = {
                            client: {
                                id: 'clienteIdPDEZacatecas',
                                secret: 'clientSecretPDEZacatecas',
                            }, 
                            auth: {
                                tokenHost: 'http://localhost:3000/oauth'
                            }
                        };

                        const oauth2 = require('simple-oauth2').create(credentials);

                        const tokenConfig = {
                            username: username,
                            password: password,
                            scope: scope,
                        };

                        const result = await oauth2.ownerPassword.getToken(tokenConfig);
                        const accessToken = oauth2.accessToken.create(result);

                        console.log(accessToken)

                        res.status(200).json(accessToken);
                    }
                    else {
                        console.log('No existe el usaurio');
                        let error = {
                            error: 400,
                            message: 'Usuario no registrado'
                        }
                        res.status(400).json(error);
                    }
                }
                else {
                    console.log('Parametros incorrectos')
                    let error = {
                        error: 400,
                        message: 'Sin permisos'
                    }
                    res.status(400).json(error);
                }
            } catch (error) {
                console.log('Access Token Error', error.message);
            }
        }
    }
    export const oAuthController = new OAuthController();

执行代码时,我的控制台显示 Access Token Error The content-type is not JSON compatible

我正在使用 simple-oauth2 https://www.npmjs.com/package/simple-oauth2,带有 grant_type:password 我正在尝试生成访问令牌

【问题讨论】:

    标签: javascript node.js api oauth oauth-2.0


    【解决方案1】:

    我遇到了这个问题,对我来说这是一个配置问题。该示例提供的配置如下

    OAUTH_AUTHORITY=https://login.microsoftonline.com/common
    OAUTH_ID_METADATA=/v2.0/.well-known/openid-configuration
    OAUTH_AUTHORIZE_ENDPOINT=/oauth2/v2.0/authorize
    OAUTH_TOKEN_ENDPOINT=/oauth2/v2.0/token
    

    wreck 使用 Url.URL 将 OAUTH_AUTHORITY 与 OAUTH_TOKEN_ENDPOINT 组合在一起,这会导致 https://login.microsoftonline.com/oauth2/v2.0/token 并因此失去公共性。这会导致 404,因此不再有 JSON 响应。

    我稍微更改了配置,并从相对路径中删除了前导斜杠,并在基本 URL 中添加了尾随斜杠。

    OAUTH_AUTHORITY=https://login.microsoftonline.com/common/
    OAUTH_ID_METADATA=/v2.0/.well-known/openid-configuration
    OAUTH_AUTHORIZE_ENDPOINT=oauth2/v2.0/authorize
    OAUTH_TOKEN_ENDPOINT=oauth2/v2.0/token
    

    所以 OAUTH_TOKEN_ENDPOINT 是相对的。我还没有弄清楚为什么它对授权有效,但仍然有效。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2022-12-19
      • 2018-11-16
      • 2020-11-11
      • 1970-01-01
      • 1970-01-01
      • 2019-02-18
      • 2012-02-03
      相关资源
      最近更新 更多