Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/376.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript Google登录获取用户令牌_Javascript_Google Signin - Fatal编程技术网

Javascript Google登录获取用户令牌

Javascript Google登录获取用户令牌,javascript,google-signin,Javascript,Google Signin,尝试获取我获得的用户令牌: 未捕获的TypeError:profile.getAuthResponse不是函数 在哪里 profile=googleUser 这是我的代码: function onSignIn(googleUser) { var profile = googleUser.getBasicProfile(); console.log('ID: ' + profile.getId()); // Do not send to your backend! Use an I

尝试获取我获得的用户令牌:

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

在哪里

profile=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);
}
没有什么特别的

我在google文档上读到,为了安全起见,我应该使用函数getAuthResponse().id_token,而不是GoogleUser.getId()

在这个问题上:
在这里,Sharkos建议使用gapi.auth.getToken().access_令牌,但我得到null

知道我为什么会出错吗?

试试这个

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

可能存在的副本