Javascript Axios CORS问题,Github oauth无法获取访问令牌
我在我的React Redux应用程序上创建了2条路由。我已经添加了带有主页和回调URL的github应用程序设置 一,。 当你走这条路线时: 单击Github登录按钮,==>githubGeturi 二,。 Github使用代码重定向回 和githubSendCode('9536286a59228e7784a1')操作被触发 你们可以在网络看涨期权中看到看涨期权看涨期权,但看涨期权从来并没有发生过。您会得到一个控制台错误:Javascript Axios CORS问题,Github oauth无法获取访问令牌,javascript,github,oauth,cors,axios,Javascript,Github,Oauth,Cors,Axios,我在我的React Redux应用程序上创建了2条路由。我已经添加了带有主页和回调URL的github应用程序设置 一,。 当你走这条路线时: 单击Github登录按钮,==>githubGeturi 二,。 Github使用代码重定向回 和githubSendCode('9536286a59228e7784a1')操作被触发 你们可以在网络看涨期权中看到看涨期权看涨期权,但看涨期权从来并没有发生过。您会得到一个控制台错误: XMLHttpRequest cannot load https://
XMLHttpRequest cannot load https://github.com/login/oauth/access_token?client_id=32b70bf671e04762b26c&…_secret=123456789123456789123456789&code=9536286a59228e7784a1. Response to preflight request doesn't pass access control check: No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'https://reduxapp.herokuapp.com' is therefore not allowed access.
下面是我的动作功能:
const CLIENT_ID = '32b70bf671e04762b26c';
const CLIENT_SECRET = '123456789123456789123456789';
const ROOT_URL = window.location.origin;
const REDIRECT_URL = `${ROOT_URL}/auth/callback`;
const AUTHORIZE_URL = 'https://github.com/login/oauth/authorize';
const ACCESS_TOKEN_URL = 'https://github.com/login/oauth/access_token';
const STATE = _.random(10000);
export function githubGeturi() {
const GITHUB_URL = `${AUTHORIZE_URL}?client_id=${CLIENT_ID}&scope=user,public_repo&redirect_uri=${REDIRECT_URL}`;
return (dispatch) => dispatch(signinUrl(GITHUB_URL));
}
export function githubSendCode(code) {
const GITHUB_URL = `${ACCESS_TOKEN_URL}?client_id=${CLIENT_ID}&client_secret=${CLIENT_SECRET}&code=${code}`;
axios.defaults.headers.post['Access-Control-Allow-Origin'] = '*';
const axiosPost = axios.post(
GITHUB_URL,
{
headers: {
'Content-Type': 'application/x-www-form-urlencoded',
'Accept': 'text/json'
}
});
return (dispatch) => {
dispatch(signinRequest());
return axiosPost
.then(
success => dispatch(signinSuccess(success)),
error => dispatch(signinError(error))
);
};
}
========
我找到的唯一可能的方法是与服务器进行POST呼叫。
您可以在此处查看整个解决方案:似乎无法通过JavaScript调用该端点 在您的示例中,我看到OPTIONS方法调用失败,这是因为当您向请求添加额外的头时,axios会这样做,但POST调用也会失败
您可以在应用程序和服务器上的github之间设置一个代理,只需转发您的请求和回复即可。这不是这个问题的重复吗?这是oauth API发布,不要在这里发布你的秘密!似乎它们不支持实际的Web流。使用本地代理服务器,使用github进行ajax调用是解决方案。