google-登录不要 return 用户信息

google-signin don't return user info

我正在使用 Google Web 组件中的 google-signin 元素,但我不知道如何 return 用户信息。

<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());
  }

对不起,如果这是愚蠢的问题,但我不擅长网络开发。 谢谢你的建议。

来自聚合物文档:

The google-signin-success event is triggered when a user successfully authenticates and google-signin-failure is triggered when this is not the case. Both events will also provide the data returned by the Google client authentication process. You can also use isAuthorized attribute to observe authentication state.

Additional events, such as google-signout-attempted and google-signed-out are triggered when the user attempts to sign-out and successfully signs out.

The google-signin-necessary event is fired when scopes requested via google-signin-aware elements require additional user permissions.

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