所以我刚刚从LinkedIn oauth 1.0升级到2.0,并且已经有大约一天的时间出现此错误。我看到了有关php的帖子,但是我无法在Node JS(Javascript)中弄清楚这是我当前的代码:
axios
.post("https://www.linkedin.com/oauth/v2/accessToken", {
grant_type: "authorization_code",
code: req.query.code,
redirect_uri: keys.linkedinCallbackURL,
client_id: keys.linkedinConsumerKey,
client_secret: keys.linkedinConsumerSecret
})
.then(res2 => {
console.log(res2);
})
.catch(error => {
console.log(error);
});
如果您有任何想法请告诉我:)
链接到php解决方案:LinkedIn OAuth a required parameter "clien_id" is missing
链接到LinkedIn指南:https://developer.linkedin.com/docs/oauth2 (在第3步发生错误)
答案 0 :(得分:2)
LinkedIn文档要求您以application/x-www-form-urlencoded
的形式发送数据,并向我们展示了这个示例:
POST /oauth/v2/accessToken HTTP/1.1
Host: www.linkedin.com
Content-Type: application/x-www-form-urlencoded
grant_type=authorization_code&code=987654321&redirect_uri=https%3A%2F%2Fwww.myapp.com%2Fauth%2Flinkedin&client_id=123456789&client_secret=shhdonottell
Axios documentation说,默认情况下,正文被序列化为JSON:
默认情况下,axios将JavaScript对象序列化为JSON。
为了正确地序列化正文,您应该使用querystring
模块,如下所示:
const querystring = require('querystring');
axios
.post("https://www.linkedin.com/oauth/v2/accessToken", querystring.stringify({
grant_type: "authorization_code",
code: req.query.code,
redirect_uri: keys.linkedinCallbackURL,
client_id: keys.linkedinConsumerKey,
client_secret: keys.linkedinConsumerSecret
}));