【问题标题】:How to get authorization_code from Gmail API in angular 5 app如何在 Angular 5 应用程序中从 Gmail API 获取授权码
【发布时间】:2019-01-21 08:05:35
【问题描述】:

我在一个 Angular 应用中工作,我想在这个 Angular 应用中添加 Gmail。

我已经按照这个教程https://medium.com/@mcflyDev/angular-2-or-4-import-google-contacts-d0ffb13d8626

一切正常,我收到了access_token 的成功响应,但我没有收到authorization_code

我怎样才能得到authorization_code

这是我的配置:

     this.auth2 = gapi.auth2.init({
        client_id: 'bla-bla-bla-2.apps.googleusercontent.com',
        cookiepolicy: 'single_host_origin',
        scope: 'https://www.googleapis.com/auth/gmail.readonly',
        access_type: 'offline',
        response_type: 'code',
        auth_uri: 'https://accounts.google.com/o/oauth2/v2/auth',
        prompt: 'select_account',
        include_granted_scopes: true,
        grant_type: 'authorization_code',
      });

另外,我也没有得到refresh_token,你可以看到我已经设置了access_type: 'offline'

我收到了如上图所示的响应。

谢谢。

【问题讨论】:

  • 嗨@Arpit Meena 你找到答案了吗? A有同样的问题。
  • @pelcomppl,你在打POST Ajax 电话吗?

标签: javascript angular google-api gmail-api


【解决方案1】:

我得到了解决方案。之前我在进行POST Ajax 调用,但这不是必需的。

我们只需要分两步做一个小设置:

步骤 1. 准备 URL:

网址应如下所示:

 https://accounts.google.com/o/oauth2/auth?redirect_uri=:your_redirect_url&response_type=code&client_id=:your_client_id&scope=https://www.googleapis.com/auth/gmail.send&approval_prompt=force&access_type=offline

您可以将此 URL 粘贴到浏览器中,然后按 Enter 以检查它是否正常工作。

步骤 2. 在 HTML 中创建锚标记:

<a href="giveAboveURLHere">Connect With Gmail</a>

恭喜!!!你已经完成了。

每当用户单击此链接时,Google 都会请求对所提供范围的许可,如果最终用户授予访问权限,那么 Google 将重定向到给定的 redirect_url,并在查询参数中使用 authorization_code

然后在服务器端,您需要使用此authorization_code 再次调用API,然后Google 将提供access_tokenrefresh_token

谢谢,希望对你有帮助。

【讨论】:

    【解决方案2】:

    @Arpit 米娜 我找到了另一个解决方案:

    在“与 Gmail 连接”按钮上单击我运行 this.auth2.grantOfflineAccess()

    它会触发 google 帐户选择弹出窗口,并在该弹出窗口之后立即向范围授予权限。它返回authorization_code,我可以使用它在我的API ($gClient-&gt;authenticate($code);) 中获取refresh_token 和access_token。

    【讨论】:

      【解决方案3】:

      这是我发现最好的使用 js 客户端库获取身份验证代码的方法。 rest on g-site 只提到了 http/rest 方法。

      function initClient() {
                  gapi.client.init({
                    apiKey: API_KEY,
                    clientId: CLIENT_ID,
                    discoveryDocs: DISCOVERY_DOCS,
                    scope: SCOPES,
                    access_type:'offline',
                    include_granted_scopes: true
                  }).then(function () {
               
                    offinebutton.onclick = offlinefunction;
                    
                  }, function(error) {
                    appendPre(JSON.stringify(error, null, 2));
                  });
                }
      
          function offlinefunction() {
                  gapi.auth2.getAuthInstance().grantOfflineAccess().then(function(resp) {
                  console.log(resp.code)
                  });
                }
      

      【讨论】:

        猜你喜欢
        • 2018-06-22
        • 1970-01-01
        • 1970-01-01
        • 2016-08-18
        • 1970-01-01
        • 1970-01-01
        • 2017-09-15
        • 2020-02-26
        • 2019-05-11
        相关资源
        最近更新 更多