React-Redux アプリで 2 つのルートを作成しました。ホームページとコールバック URL を含む github アプリケーション設定を既に追加しています。
1. このルートをたどると : https://reduxapp.herokuapp.com/signin Github ログイン ボタンをクリックします ==> githubGeturi
2. Github はコードhttps://reduxapp.herokuapp.com/auth/callback?code=9536286a59228e7784a1 でリダイレクトし、githubSendCode ('9536286a59228e7784a1') アクションがトリガーされます
ネットワーク呼び出しで OPTIONS 呼び出しが通過することを確認できますが、POST 呼び出しは発生しません。コンソール エラーが表示されます。
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 呼び出しを行うことです。ここでソリューション全体を表示できます: https://github.com/steelx/ReduxWeatherApp/commit/6215634ca543a4760ea96397fe31b61f22184d91