javascript - Google 登录获取用户 token

标签 javascript google-signin

尝试获取我获得的用户 token :

Uncaught TypeError: profile.getAuthResponse is not a function

哪里

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

在这个问题中:Google access_token is undefined
这里Sharkos建议使用gapi.auth.getToken().access_token,但我得到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()

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

关于javascript - Google 登录获取用户 token ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47037798/

相关文章:

javascript - JS Ray Tracer 对象无限长

javascript - ng-include 不起作用

javascript - 尝试获取配置文件数据时出现 API key 无效错误

ios - 架构 x86_64 的重复符号 _GSDK_GTMNSDataZLibExportToSuppressLibToolWarning

java - Google 登录错误 12500 SHA1 双重检查 GoogleSignInAccount account =completedTask.getResult(ApiException.class);

用于 google web 登录的 TypeScript 绑定(bind)

javascript - gulp 记者归档

javascript - 检查值 select2 是否为空

javascript - 如何使用不同的 ID 调用相同的方法

android - Firebase google auth 自动选择用户。如何强制它弹出 Gmail 帐户