使用nodeJS(OAuth2)执行http请求

时间:2019-02-11 14:15:25

标签: node.js oauth request

我必须调用将OAuth2与客户端凭据一起使用的API。

我在做这件事时遇到了麻烦...

这是我生成的代码(使用请求包):

const credentials = {
    client: {
        id: 'MY_ID',
        secret: 'My_PASSWORD'
    },
    auth: {
        tokenHost: 'DOMAIN',
        tokenPath: 'PATH',
        scope: '',
        grantType: "client_credentials"
    }
};

var options = { 
    method: 'POST',
    url: credentials.auth.tokenHost + credentials.auth.tokenPath,
    headers: { 'content-type': 'application/json' },
    body: { 
        grant_type: credentials.auth.grantType,
        client_id: credentials.client.id,
        client_secret: credentials.client.secret
    },
    json: true
};

request(options, function (error, response, body) {
  if (error) throw new Error(error);

  console.log(body);
});

我遇到此错误:

{ error: 'invalid_request',
  error_description: 'Missing form parameter: grant_type' }

也许我想念一些东西。

如果您能帮助我解决这个问题,对您非常好:)

PS:它适用于Postman,所以我的值是正确的。

0 个答案:

没有答案