gpt4 book ai didi

javascript - 如何从登录的 Google Sign In(仅客户端应用程序)正确获取访问 token ?

转载 作者:行者123 更新时间:2023-12-01 00:57:30 26 4
gpt4 key购买 nike

我使用 Google 登录按钮让用户使用其 Google 帐户登录:

    gapi.signin2.render("container-btn-google-login", {
scope: "email profile",
onsuccess: (user) => {
let token = user.getAuthResponse();
setToken(token);

// How can I get the Access Token here
// that should be the same as user.Zi.access_token?

resolve(token);
},
});

我将调用其他 Google API,但不想使用他们的 Javascript 库(我将使用 fetch 来调用他们的 REST API)。

但是,当我将 Authorization: token.id_token 作为 header 时,请求被拒绝。如果我手动输入一个可以从控制台看到的值 (user.Zi.access_token),它就可以工作。

如何从 Google API Javascript SDK 中提取 access_token?使用像 Zi 这样神秘且未记录的东西对我来说似乎并不安全。

最佳答案

好的,我找到了relevant documentation这里。我们可以使用 user.getAuthResponse(true) 来获取带有访问 token 和可能其他有用信息的身份验证响应:

参数为includeAuthorizationData:

Optional: A boolean that specifies whether to always return an access token and scopes. By default, the access token and requested scopes are not returned when fetch_basic_profile is true (the default value) and no additional scopes are requested.

我的问题中的工作代码示例:

let token = user.getAuthResponse(true);
console.log(token.access_token)

关于javascript - 如何从登录的 Google Sign In(仅客户端应用程序)正确获取访问 token ?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56425727/

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