再会,
我正在尝试使用 github oauth2.0 apis 并坚持/access_token
使用 fetch 和 axios 进行调用。
我已经成功地让/authorize
api 工作并拥有client_id
, client_secret
, 并code
成功传递给getAccessToken
函数。但是,无法通过返回 cors 错误问题进行提取。
我探索过的东西
- https://www.sohamkamani.com/blog/javascript/2018-06-24-oauth-with-node-js/
- https://github.com/sohamkamani/node-oauth-example
- https://github.com/evert/fetch-mw-oauth2
代码:
export const authorize = async () => {
const uri = withQuery('https://github.com/login/oauth/authorize', {
client_id: process.env.VUE_APP_CLIENT_ID,
redirect_uri: 'http://localhost:8080/authorize',
scope: 'read:user',
});
window.location.replace(uri);
};
export const getAccessToken = async (code: string) => {
const { VUE_APP_CLIENT_ID, VUE_APP_CLIENT_SECRET } = process.env;
const uri = withQuery('https://github.com/login/oauth/access_token', {
client_id: VUE_APP_CLIENT_ID,
client_secret: VUE_APP_CLIENT_SECRET,
code,
});
axios({
method: 'post',
url: uri,
headers: {
accept: 'application/json',
},
}).then(response => {
console.log('response: ', response);
});
};
Axios 错误:
authorize:1 Access to XMLHttpRequest at 'https://github.com/login/oauth/access_token?client_id=abcdefgh&client_secret=abcdefgh' from origin 'http://localhost:8080' has been blocked by CORS policy: No 'Access-Control-Allow-Origin' header is present on the requested resource.
createError.js?2d83:16 Uncaught (in promise) Error: Network Error
at createError (createError.js?2d83:16)
at XMLHttpRequest.handleError (xhr.js?b50d:81)