gpt4 book ai didi

javascript - Google 登录获取用户 token

转载 作者:行者123 更新时间:2023-12-03 03:08:48 28 4
gpt4 key购买 nike

尝试获取我获得的用户 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/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com