我正在使用Google中的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());
}对不起,如果这是愚蠢的问题,但我不擅长网络开发。谢谢你的建议。
发布于 2015-06-17 15:43:15
来自聚合物文献:
当用户成功身份验证时会触发google-signin-success事件,如果情况并非如此,则会触发google-signin-failure事件。这两个事件还将提供由Google客户端身份验证过程返回的数据。还可以使用isAuthorized属性来观察身份验证状态。 当用户尝试签出并成功退出时,会触发其他事件,如google-signout-attempted和google-signed-out。 当通过google感知的元素请求作用域需要额外的用户权限时,会触发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
https://stackoverflow.com/questions/30891336
复制相似问题