Cors issue on github oauth

import request from 'superagent';

const self = this;
    request
      .post('https://github.com/login/oauth/access_token')
      .set('Content-Type', 'multipart/form-data')
      .query({
        client_id: CLIENT_ID,
        client_secret: CLIENT_SECRET,
        callback: 'http://127.0.0.1:3000/callback',
        code,
        state,
      })
      .end((err, res) => {
        const token = res.body.access_token;
        console.log(token);
        self.setToken(token);
      });
Run codeHide result

The code above will give me such an error

XMLHttpRequest cannot load https://github.com/login/oauth/access_token?client_id=112asdecf3805fdada12& ... 127.0.0.1% 3A3000% 2Fcallback & code = 434ebd7bb98d9809bf6e & state = HelloWorld1234. There is no "Access-Control-Allow-Origin" header in the requested resource. Origin ' http : //127.0.0.1lla000 ' therefore access is not allowed.

I have no idea why, although I registered an oauth application with github and a callback, this http://127.0.0.1:3000/callback

+4
source share
1 answer

API GitHub CORS, , , https://github.com/login/oauth/access_token OAuth CORS -.

- https://github.com/prose/gatekeeper:

Gatekeeper: OAuth GitHub.

- , , Github - OAuth .

. Gatekeeper, , , .

: , https://cors-anywhere.herokuapp.com/

var req = new XMLHttpRequest();
req.open('POST',
  'https://cors-anywhere.herokuapp.com/https://github.com/login/oauth/access_token',
  true);
req.setRequestHeader('Accept', 'application/json');
req.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
req.send('code=' + encodeURIComponent(location.query.code) +
    '&client_id=foo' +
    '&client_secret=bar');
...

. Cors - CORS.

+4

Source: https://habr.com/ru/post/1669326/


All Articles