<tfoot id='NolBq'></tfoot>
<i id='NolBq'><tr id='NolBq'><dt id='NolBq'><q id='NolBq'><span id='NolBq'><b id='NolBq'><form id='NolBq'><ins id='NolBq'></ins><ul id='NolBq'></ul><sub id='NolBq'></sub></form><legend id='NolBq'></legend><bdo id='NolBq'><pre id='NolBq'><center id='NolBq'></center></pre></bdo></b><th id='NolBq'></th></span></q></dt></tr></i><div id='NolBq'><tfoot id='NolBq'></tfoot><dl id='NolBq'><fieldset id='NolBq'></fieldset></dl></div>

<legend id='NolBq'><style id='NolBq'><dir id='NolBq'><q id='NolBq'></q></dir></style></legend>

    <small id='NolBq'></small><noframes id='NolBq'>

      <bdo id='NolBq'></bdo><ul id='NolBq'></ul>

      1. github oauth 上的 cors 问题

        时间:2023-10-14

          <tbody id='TZStT'></tbody>

        <i id='TZStT'><tr id='TZStT'><dt id='TZStT'><q id='TZStT'><span id='TZStT'><b id='TZStT'><form id='TZStT'><ins id='TZStT'></ins><ul id='TZStT'></ul><sub id='TZStT'></sub></form><legend id='TZStT'></legend><bdo id='TZStT'><pre id='TZStT'><center id='TZStT'></center></pre></bdo></b><th id='TZStT'></th></span></q></dt></tr></i><div id='TZStT'><tfoot id='TZStT'></tfoot><dl id='TZStT'><fieldset id='TZStT'></fieldset></dl></div>
            • <bdo id='TZStT'></bdo><ul id='TZStT'></ul>
            • <legend id='TZStT'><style id='TZStT'><dir id='TZStT'><q id='TZStT'></q></dir></style></legend>

                <small id='TZStT'></small><noframes id='TZStT'>

                  <tfoot id='TZStT'></tfoot>
                  本文介绍了github oauth 上的 cors 问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

                  问题描述

                  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);
                        });

                  上面的代码会给我这样的错误

                  The code above will give me an error like this

                  XMLHttpRequest 无法加载https://github.com/login/oauth/access_token?client_id=112asdecf3805fdada12&…127.0.0.1%3A3000%2Fcallback&code=434ebd7bb98d9809bf6e&state=HelloWorld1234.请求中不存在Access-Control-Allow-Origin"标头资源.因此,不允许使用来源 'http://127.0.0.1:3000'访问.

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

                  我不知道为什么即使我已经使用 github 注册了 oauth 应用程序并且回调 url 是 http://127.0.0.1:3000/callback

                  I have no idea why even though I've registered the oauth application with github and callback url is http://127.0.0.1:3000/callback

                  推荐答案

                  虽然所有实际的 GitHub API 端点通过发送正确的响应头来支持 CORS,它是 一个已知的问题 用于创建 OAuth 访问令牌的 https://github.com/login/oauth/access_token 端点不支持来自 Web 应用程序的 CORS 请求.

                  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.

                  这种情况的非常具体的解决方法是使用 https://github.com/prose/gatekeeper:

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

                  Gatekeeper:使客户端应用程序能够与 GitHub 共舞 OAuth.

                  由于一些与安全相关的限制,Github 阻止您在仅客户端应用程序上实施 OAuth Web 应用程序流程.

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

                  这真是太糟糕了.因此,我们构建了 Gatekeeper,这是您使其工作所需的缺失部分.

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

                  一般的解决方法是:使用开放的反向代理,例如 https://cors-anywhere.herokuapp.com/

                  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');
                  ...
                  

                  另请参阅 如何在任何地方使用 Cors 进行反向代理和添加 CORS 标头.

                  这篇关于github oauth 上的 cors 问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

                  上一篇:Instagram 公共 API 的新 CORS 政策? 下一篇:Github API 和 Access-Control-Allow-Origin

                  相关文章

                  最新文章

                    <i id='anDUG'><tr id='anDUG'><dt id='anDUG'><q id='anDUG'><span id='anDUG'><b id='anDUG'><form id='anDUG'><ins id='anDUG'></ins><ul id='anDUG'></ul><sub id='anDUG'></sub></form><legend id='anDUG'></legend><bdo id='anDUG'><pre id='anDUG'><center id='anDUG'></center></pre></bdo></b><th id='anDUG'></th></span></q></dt></tr></i><div id='anDUG'><tfoot id='anDUG'></tfoot><dl id='anDUG'><fieldset id='anDUG'></fieldset></dl></div>
                  1. <small id='anDUG'></small><noframes id='anDUG'>

                      • <bdo id='anDUG'></bdo><ul id='anDUG'></ul>

                    1. <legend id='anDUG'><style id='anDUG'><dir id='anDUG'><q id='anDUG'></q></dir></style></legend>

                      <tfoot id='anDUG'></tfoot>