简体   繁体   English

检索LinkedIn访问令牌

[英]Retrieve LinkedIn access token

I'm trying to get linkedIn oauth2 access token but I stuck on making last request to https://www.linkedin.com/oauth/v2/accessToken 我正在尝试获取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 returns LinkedIn返回

Fetch API cannot load https: //www.linkedin.com/oauth/v2/accessToken. Fetch API无法加载https://www.linkedin.com/oauth/v2/accessToken。 Response to preflight request doesn't pass access control check: No 'Access-Control-Allow-Origin' header is present on the requested resource. 对预检请求的响应未通过访问控制检查:请求的资源上不存在“Access-Control-Allow-Origin”标头。 Origin 'http: //localhost:3000' is therefore not allowed access. 因此不允许来源'http:// localhost:3000'访问。 The response had HTTP status code 404. If an opaque response serves your needs, set the request's mode to 'no-cors' to fetch the resource with CORS disabled. 响应具有HTTP状态代码404.如果不透明响应满足您的需要,请将请求的模式设置为“no-cors”以获取禁用CORS的资源。

so I tried to make request in 'no-cors' mode. 所以我试图以'无人'模式提出请求。 I've got 200 but I can't get body of response,status is 0, body is null and response.json() throws SyntaxError: Unexpected end of input. 我有200但是我无法得到响应体,状态为0,body为null并且response.json()抛出SyntaxError:意外的输入结束。

I found out your issue, The Content-Type header value is not correct. 我发现了您的问题, Content-Type标头值不正确。

It's meant to be 它的意思是

const headers = new Headers({'Content-Type': 'application/x-www-form-urlencoded'});

NOT

const headers = new Headers({'Content-Type': 'x-www-form-urlencoded'});

DOCS: https://developer.linkedin.com/docs/oauth2 (Step 3) DOCS: https//developer.linkedin.com/docs/oauth2 (第3步)

For anyone who came across this problem. 对于任何遇到此问题的人。 We solved it by passing data from LinkedIn (code) to our backend. 我们通过将数据从LinkedIn(代码)传递到我们的后端来解决它。 Backend have no need to send any preflight OPTIONS requests and can get access token without problem. 后端无需发送任何预检OPTIONS请求,并且可以毫无问题地获得访问令牌。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM