我正在尝试获取linkedin oauth2访问令牌,但我坚持最后要求https://www.linkedin.com/oauth/v2/accesstoken
const body = new URLSearchParams([
['grant_type', 'authorization_code'],
['code', code],
['redirect_uri', 'http://localhost:3000/'],
['client_id', CLIENT_ID],
['client_secret', CLIENT_SECRET]
]);
const headers = new Headers({'Content-Type': 'x-www-form-urlencoded'});
window.fetch('https://www.linkedin.com/oauth/v2/accessToken', method: 'POST',body, headers)
.then(response => response.json())
.then(data => {
// rest of code
})
LinkedIn返回
提取API无法加载https://www.linkedin.com/oauth/v2/accesstoken。对飞行前请求的响应不会传递访问控制检查:在请求的资源上没有"访问控制"标头。因此,不允许访问原点" http://localhost:3000"。响应具有HTTP状态代码404.如果不透明的响应满足您的需求,请将请求模式设置为"无现体",以通过禁用CORS来获取资源。
因此,我尝试以"禁止"模式提出请求。我有200个,但我无法得到响应,状态为0,身体为null and vession。
对于任何遇到此问题的人。我们通过将数据从LinkedIn(代码)传递到我们的后端来解决。后端无需发送任何前置选项请求,并且可以毫无问题地获取访问令牌。
我发现了您的问题,Content-Type
标头值不正确。
这是
const headers = new Headers({'Content-Type': 'application/x-www-form-urlencoded'});
不
const headers = new Headers({'Content-Type': 'x-www-form-urlencoded'});
文档:https://developer.linkedin.com/docs/oauth2(步骤3)