Google登录获取用户令牌

时间:2017-10-31 14:26:53

标签: javascript google-signin

尝试获取用户的令牌:

  

未捕获的TypeError:profile.getAuthResponse不是函数

其中

  

个人资料= googleUser

这是我的代码:

function onSignIn(googleUser) {
    var profile = googleUser.getBasicProfile();
    console.log('ID: ' + profile.getId()); // Do not send to your backend! Use an ID token instead.
    console.log('Name: ' + profile.getName());
    console.log('Image URL: ' + profile.getImageUrl());
    console.log('Email: ' + profile.getEmail()); // This is null if the 'email' scope is not present.
    console.log('Token id: ' + profile.getAuthResponse().id_token);
}

那里没什么特别的。

我在谷歌文档中读到我应该使用函数 getAuthResponse()。id_token 而不是 GoogleUser.getId()以保证安全。

在这个问题中:Google access_token is undefined
在这里,Sharkos建议使用 gapi.auth.getToken()。access_token ,但我为空

为什么我收到这个错误?

1 个答案:

答案 0 :(得分:1)

试试这个

function onSignIn(googleUser) {
    var profile = googleUser.getBasicProfile();
    console.log('ID: ' + profile.getId()); // Do not send to your backend! Use an ID token instead.
    console.log('Name: ' + profile.getName());
    console.log('Image URL: ' + profile.getImageUrl());
    console.log('Email: ' + profile.getEmail()); // This is null if the 'email' scope is not present.
    console.log('Token id: ' + googleUser.getAuthResponse().id_token);
}

你设置var profile = googleUser.getBasicProfile();因此,您无法在getBasicProfile()上使用getAuthResponse()。它必须是googleUser.getAuthResponse()

https://developers.google.com/identity/sign-in/web/reference#googleusergetauthresponseincludeauthorizationdata