cors issue on github oauth

前端 未结 1 914
小鲜肉
小鲜肉 2020-12-16 18:41



        
相关标签:
1条回答
  • 2020-12-16 18:56

    While all the actual GitHub API endpoints support CORS by sending the right response headers, it is a known issue that the https://github.com/login/oauth/access_token endpoint for creating an OAuth access token does not support CORS requests from Web applications.

    The very specific workaround for this case is to use https://github.com/prose/gatekeeper:

    Gatekeeper: Enables client-side applications to dance OAuth with GitHub.

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

    This is a real bummer. So we built Gatekeeper, which is the missing piece you need in order to make it work.

    The general workaround is: Use an open reverse proxy like 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');
    ...
    

    See also How to use Cors anywhere to reverse proxy and add CORS headers.

    0 讨论(0)
提交回复
热议问题