1

我正在使用来自 Google Web Components的 google-signin元素,但我不知道如何返回用户信息。

<google-signin
      client-id="{{my-id}}" scopes="email profile"
      signed-in="{{signedIn}}"></google-signin>

我尝试编写一些 JS 函数,但没有成功。

function (signedIn) {
       var profile = gapi.auth2.getAuthInstance().currentUser.get().getBasicProfile();
       console.log('Name: ' + profile.getName());
       console.log('Image: ' + profile.getImageUrl());
  }

如果这是愚蠢的问题,请原谅,但我不擅长网络开发。感谢您的意见。

4

1 回答 1

3

来自聚合物文档:

当用户成功认证时会触发google-signin-success事件,如果不是这样,则会触发google-signin-failure。这两个事件还将提供 Google 客户端身份验证过程返回的数据。您还可以使用isAuthorized 属性来观察身份验证状态。

当用户尝试注销并成功注销时,会触发其他事件,例如google-signout-attemptedgoogle-signed- out。

当通过 google-signin-aware 元素请求的范围需要额外的用户权限时,会触发google-signin-necessary事件。

https://elements.polymer-project.org/elements/google-signin

<google-signin ... id="myLoginIn"></google-signin>
<script>
  var t = document.querySelector('#t');

  t.addEventListener('google-signin-success', function(data) {
    ...
  });
</script>

或者您可以使用:

<google-signin client-id="{{my-id}}" scopes="email profile" signed-in="{{signedIn}}"></google-signin>

<google-signin-aware
        scopes="{{scope}}"
        signed-in="{{signedIn}}"
        is-authorized="{{isAuthorized}}"
        need-additional-auth="{{needAdditionalAuth}}"
        on-google-signin-aware-success="handleSignIn"
        on-google-signin-aware-signed-out="handleSignOut"></google-signin-aware>

然后你可以得到这样的用户名:

var aware = document.querySelector('#awareness');
aware.handleSignIn = function(response) {
      console.log('[Aware] Signin Response', response);
      var userName = gapi.auth2.getAuthInstance().currentUser.get().getBasicProfile().getName();
};

在这里您可以找到完整的演示:https ://github.com/GoogleWebComponents/google-signin/blob/master/demo/index.html

于 2015-06-17T15:43:15.393 回答