Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/362.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 如何在angular 5应用程序中从Gmail API获取授权代码_Javascript_Angular_Google Api_Gmail Api - Fatal编程技术网

Javascript 如何在angular 5应用程序中从Gmail API获取授权代码

Javascript 如何在angular 5应用程序中从Gmail API获取授权代码,javascript,angular,google-api,gmail-api,Javascript,Angular,Google Api,Gmail Api,我在一个Angular应用程序中工作,我想在这个Angular应用程序中添加Gmail 我遵循了这个教程 一切正常,我通过access\u-token获得了成功响应,但我没有得到authorization\u-code 如何获得授权\u code 以下是我的配置: this.auth2 = gapi.auth2.init({ client_id: 'bla-bla-bla-2.apps.googleusercontent.com', cookiepoli

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

我遵循了这个教程

一切正常,我通过
access\u-token
获得了成功响应,但我没有得到
authorization\u-code

如何获得
授权\u 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',
      });
此外,我也没有获得
刷新\u令牌
,正如您所看到的,我已经设置了
访问\u类型:“脱机”

我得到了如上图所示的响应


谢谢。

我找到了解决办法。早些时候,我做了一个
POST
Ajax调用,但这不是必需的

我们只需要分两步进行一个小的设置:

第1步。准备URL:

<a href="giveAboveURLHere">Connect With Gmail</a>
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>

祝贺你!!!你已经做到了

每当用户单击此链接时,谷歌都会请求对所提供的范围进行许可,如果最终用户授予了访问权限,则谷歌将使用查询参数中的
授权代码
重定向到给定的
重定向url

然后在服务器端,您需要使用此
授权\u-code
进行另一个
API
调用,然后谷歌将提供
access\u-token
refresh\u-token

谢谢,我希望这会有帮助。

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

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


它启动google帐户选择弹出窗口,并在弹出窗口之后立即向作用域授予权限。它返回
authorization\u code
,我可以使用它在我的API中获取刷新令牌和访问令牌(
$gClient->authenticate($code);
)。

这是我发现使用js客户端库获取认证代码的最佳方法。 只提到了g-site上的rest 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)
            });
          }

嗨@Arpit Meena你找到答案了吗?A也有同样的问题。@pelcomppl,您正在进行
POST
Ajax调用吗?