【问题标题】:How to initiate OAuth2.0 flow?如何启动 OAuth2.0 流程?
【发布时间】:2021-12-18 19:11:51
【问题描述】:

如何 POST 到 Google 的 OAuth 2.0 端点以进行授权?

我正在 React 中构建一个 chrome 扩展,并且一直在关注 Google's documentation。这看起来很简单,但我并没有完全掌握实现的机制。

例如,在我的 popup.js 文件中,我调用了我的 background.js 文件,该文件执行和 axios POST 请求到创建的重定向 url。指南中的第 3 步说谷歌会提示用户同意,但是,这永远不会发生。我收到 200 条回复,但不知道该去哪里。

我做错了什么?谢谢!

  axios
      .post(
        `https://accounts.google.com/o/oauth2/v2/auth?
         scope=https%3A//www.googleapis.com/auth/drive.metadata.readonly&
         include_granted_scopes=true&
         response_type=token&
         state=state_parameter_passthrough_value&
         redirect_uri=https%3A//oauth2.example.com/code& 
         client_id=client_id` //actual values added
      )
      .then(function (response) {
        console.log('RESPONSE', response);
      });

【问题讨论】:

    标签: javascript reactjs google-chrome-extension google-api google-drive-api


    【解决方案1】:

    该文档中的第 2 步的标题

    第 2 步:重定向到 Google 的 OAuth 2.0 服务器

    您正在尝试使用 POST 进行 XHR 请求。

    该文档提供了带有和不带有客户端库的示例代码。没有客户端库,你可以看到它是一个使用表单的GET 请求(它会改变浏览器中的 URL,有效地重定向):

    /*
     * Create form to request access token from Google's OAuth 2.0 server.
     */
    function oauthSignIn() {
      // Google's OAuth 2.0 endpoint for requesting an access token
      var oauth2Endpoint = 'https://accounts.google.com/o/oauth2/v2/auth';
    
      // Create <form> element to submit parameters to OAuth 2.0 endpoint.
      var form = document.createElement('form');
      form.setAttribute('method', 'GET'); // Send as a GET request.
      form.setAttribute('action', oauth2Endpoint);
    
      // Parameters to pass to OAuth 2.0 endpoint.
      var params = {'client_id': 'YOUR_CLIENT_ID',
                    'redirect_uri': 'YOUR_REDIRECT_URI',
                    'response_type': 'token',
                    'scope': 'https://www.googleapis.com/auth/drive.metadata.readonly',
                    'include_granted_scopes': 'true',
                    'state': 'pass-through value'};
    
      // Add form parameters as hidden input values.
      for (var p in params) {
        var input = document.createElement('input');
        input.setAttribute('type', 'hidden');
        input.setAttribute('name', p);
        input.setAttribute('value', params[p]);
        form.appendChild(input);
      }
    
      // Add form to page and submit it to open the OAuth 2.0 endpoint.
      document.body.appendChild(form);
      form.submit();
    }
    

    【讨论】:

    • 感谢您的回复!在尝试(错误地)发布 axios 之前,我已经实现了上面的示例代码。我的挂断是,一旦调用了该函数,我就不确定该去哪里。例如,&lt;button onClick={() =&gt; oauthSignIn()}&gt;GRANT ACCESS&lt;/button&gt;。在进入关于 google 弹出窗口的第 3 步之前,文档状态为 the snippet creates a form that opens the request to that endpoint.。调用该函数应该启动弹出窗口还是我也误解了?
    • 一旦您重定向用户,谷歌将处理用户输入并重定向回您定义为 YOUR_REDIRECT_URL 的任何内容。然后,您的应用程序将必须处理 google 的请求以及查询参数(请参阅第 4 步)。
    • 做更多研究,看起来需要打开链接才能预设同意屏幕:stackoverflow.com/questions/59761064/…。感谢您的帮助,我现在走在正确的轨道上:)
    • 是的,在浏览器中打开它是最简单的方法。不客气!
    猜你喜欢
    • 2021-04-23
    • 2020-08-01
    • 1970-01-01
    • 2020-05-29
    • 1970-01-01
    • 2020-07-22
    • 1970-01-01
    • 2021-07-25
    • 1970-01-01
    相关资源
    最近更新 更多