【发布时间】:2021-11-14 03:52:50
【问题描述】:
我正在尝试访问 Linkedin accessToken API,但在 react js(前端)中总是遇到 CORS 错误。在 URL 栏中或通过邮递员直接点击时,Samething 可以工作。 这是我得到的错误:
Access to fetch at 'https://www.linkedin.com/oauth/v2/accessToken' from origin 'http://localhost:3000' has been blocked by CORS policy: No 'Access-Control-Allow-Origin' header is present on the requested resource. If an opaque response serves your needs, set the request's mode to 'no-cors' to fetch the resource with CORS disabled.
我的代码是:
const queryParams = querystring.stringify({
redirect_uri: process.env.REACT_APP_LINKEDIN_REDIRECT_URI,
client_id: process.env.REACT_APP_LINKEDIN_CLIENT_ID,
client_secret: process.env.REACT_APP_LINKEDIN_CLIENT_SECRET,
grant_type: 'authorization_code',
code: code,
});
const headers = {
'Content-Type': 'application/x-www-form-urlencoded',
};
const response = await fetch(`https://www.linkedin.com/oauth/v2/accessToken`, {
method: 'POST',
headers: headers,
body: queryParams,
});
`
【问题讨论】:
-
您是否尝试将
mode: 'no-cors'添加到fetch中的选项中? (见developer.mozilla.org/en-US/docs/Web/API/Fetch_API/Using_Fetch) -
@Me.Name 是的,我做了,还是一样
-
@Me.Name 是的,我做到了,它不会显示 cors 错误,但同时它也没有显示任何响应。即没有回应
标签: javascript reactjs cors frontend linkedin-api