Good day,
I am trying on github oauth2.0 apis and got stuck on calling /access_token
with fetch and axios.
I have successfully gotten /authorize
api to work and have client_id
, client_secret
, and code
passed successfully to getAccessToken
function. However, failed at making the fetch with cors error issue returned.
Things that I had explored
- 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
Code:
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 error:
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)