0

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

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)
Wen Yao
  • 31
  • 1
  • 4

1 Answers1

0

Because of some security-related limitations, Github prevents developers from implementing the OAuth Web Application Flow on a client-side only application.

References:

Working Solution:

Wen Yao
  • 31
  • 1
  • 4