0

我在我的网站中使用 Google 登录按钮。我能够授权用户并获取用户的个人资料信息。

我已按照此将 Google 登录集成到您的网络应用中

<div id="my-signin2" ></div>

gapi.signin2.render('my-signin2', {
            'scope': 'profile email',
            'width': 240,
            'height': 50,
            'longtitle': true,
            'theme': 'light',
            'onsuccess': this.onSuccess,
            'onfailure': this.onFailure
        });

      onSuccess(googleUser) {
               var id_token = googleUser.getAuthResponse().id_token;
                var name = googleUser.getBasicProfile().getName());
                var email = googleUser.getBasicProfile().getEmail());
                var imageUrl = googleUser.getBasicProfile().getImageUrl());

      }
      onFailure(error) {
      }

现在我需要在他授权使用谷歌时阅读用户联系人。

因此,为了获取联系人,我添加了一个范围

https://www.googleapis.com/auth/contacts.readonly

        gapi.signin2.render('my-signin2', {
            'scope': 'profile email https://www.googleapis.com/auth/contacts.readonly',
            'width': 240,
            'height': 50,
            'longtitle': true,
            'theme': 'light',
            'onsuccess': this.onSuccess,
            'onfailure': this.onFailure
        });

每件事都运行良好。用户使用 gmail 授权后,它会向用户显示权限屏幕并请求权限。

但是当用户拒绝权限时,它甚至没有记录用户。即使他拒绝,我也希望用户登录,如果我没有得到他们的联系人也没关系。

我已阅读有关请求额外范围的信息。

        var options = new gapi.auth2.SigninOptionsBuilder(
                {'scope': 'https://www.googleapis.com/auth/contacts.readonly'});

        googleUser = auth2.currentUser.get();
        googleUser.grant(options).then(
            function(success){
              console.log(JSON.stringify({message: "success", value: success}));
            },
            function(fail){
              alert(JSON.stringify({message: "fail", value: fail}));
            });

当我在 onSuccess(用户授权电子邮件后)之后使用它时,它总是会使用失败方法。给出错误“ popup_closed_by_user

4

1 回答 1

1

根据此线程中的讨论

找到了解决方案。

创建了另一个按钮用于请求附加范围并调用按钮的授予方法 onClick。

如果您设置这样的选项,它不起作用

var options = new gapi.auth2.SigninOptionsBuilder(
            {'scope': 'https://www.googleapis.com/auth/contacts.readonly'});

为了获得范围,您应该使用方法

let googleUser = auth2.currentUser.get();

var options = new gapi.auth2.SigninOptionsBuilder();
options.setScope('https://www.googleapis.com/auth/contacts.readonly');


googleUser.grant(options).then(
        function(success){
          console.log(JSON.stringify({message: "success", value: success}));
        },
        function(fail){
          alert(JSON.stringify({message: "fail", value: fail}));
});
于 2017-05-10T09:21:10.487 回答