node.js - 我无法使用 axios 获取linkedin 访问令牌,继续获取状态 400
问题描述
API 说状态代码 400 可能是语法错误,但我找不到它。我已经有验证码和应用程序凭据,并且 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);
}
);
}
解决方案
您使用了错误的 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 中使用的代码
- 第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'
这将为您提供访问令牌
推荐阅读
- reactjs - ReactJS、JSX 和
- python - 检查数字中的数字在python中是否按递增顺序排列
- c++ - 只接受整数输入
- elasticsearch - 在 DynamoDB 中搜索数据或使用搜索服务
- cmake - How to set DYLD_LIBRARY_PATH from cmake for executable file?
- git - 错误:无法获取 linaro-swg/arm-trusted-firmware.git
- scala - Spark(Scala)过滤结构数组而不爆炸
- bash - bash 脚本:main 中未调用 printLine() 函数
- node.js - How to establish SSL/TLS socket connection with NodeJS
- php - Wordpress 简码:引用 DOM 中的元素