【问题标题】:I can't get the linkedin access token with axios, keep getting status 400我无法使用 axios 获取linkedin 访问令牌,继续获取状态 4​​00
【发布时间】:2019-08-24 23:56:30
【问题描述】:

API 说状态码 400 可能是语法错误,但我找不到。我已经有了认证码和app凭证,并且url已经注册了。

我尝试过使用和不使用 qs。

exports.getAccessToken = (req, res, next) => {
    let payload = req.body;
    let request_body = qs.stringify({
        "grant_type": "authorization_code",
        "code": payload.code,
        "redirect_uri": linkedin.redirect_uri,
        "client_id": linkedin.clientId,
        "client_secret": linkedin.clientSecret
    });
    let config = {
        headers: {
          'Content-Type': 'application/x-www-form-urlencoded'
        }
    };
    axios.post("https://www.linkedin.com/oauth/v2/accessToken", request_body, config).then(
        response => {
            res.json(response.data);
        },
        error => {
            res.json(error);
        }
    );
}

【问题讨论】:

标签: node.js axios linkedin-api


【解决方案1】:

您使用了错误的 url 来获取 accesstoken。

  • 步骤 1

首先你需要使用以下网址

curl -X GET \
  'https://www.linkedin.com/oauth/v2/authorization?response_type=code&client_id=[CLIENT_ID]&redirect_uri=https://getpostman.com/oauth2/callback&state=CA&scope=r_emailaddress%20r_ads%20w_organization_social%20rw_ads%20r_basicprofile%20r_liteprofile%20r_ads_reporting%20r_organization_social%20rw_organization_admin%20w_member_social' \
  -H 'Postman-Token: 1c7d6199-f41b-43bc-b9ae-10d4bde0d968' \
  -H 'cache-control: no-cache'

响应包含需要在步骤 2 中使用的代码

  • 第二步

使用从第 1 步收到的 CODE

curl -X GET \
  'https://www.linkedin.com/oauth/v2/accessToken?grant_type=authorization_code&code=[CODE]&redirect_uri=https://getpostman.com/oauth2/callback&client_id=[CLIENT_ID]&client_secret=[CLIENT_SECRET]' \
  -H 'Postman-Token: f9542a93-fc9d-4cc4-aa38-a10f6cf2eb6f' \
  -H 'cache-control: no-cache'

这会给你访问令牌

【讨论】:

  • 看起来在原始问题中,他们已经完成了第 1 步。他们遇到的问题是第 2 步并让它以编程方式与 axios 一起工作。
猜你喜欢
  • 2020-11-06
  • 1970-01-01
  • 2017-06-02
  • 1970-01-01
  • 2021-05-07
  • 1970-01-01
  • 2015-10-11
  • 2018-09-26
  • 1970-01-01
相关资源
最近更新 更多