我正在使用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());
}
对不起,如果这是愚蠢的问题,但我不擅长网络开发。 谢谢你的建议。
答案 0 :(得分:3)
来自聚合物文件:
用户成功时会触发 google-signin-success 事件 如果不是,则会触发身份验证并触发 google-signin-failure 案子。这两个事件也将提供由...返回的数据 Google客户端身份验证过程您也可以使用 isAuthorized 属性以观察身份验证状态。
其他活动,例如 google-signout-attempts 和 当用户尝试注销时,会触发 google-signed-out 成功退出。
通过请求范围时会触发 google-signin-required 事件 Google-signin-aware元素需要额外的用户权限。
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