手动触发打开 Google 登录弹出窗口,"Google Identity Services"以获取"id_token"



有没有一种可编程的方法可以用";谷歌身份服务";就像你以前用gapi.auth2.authorize一样?我有一个用例,其中有一个完全自定义的谷歌登录按钮,通过单击它,我调用gapi.auth2.authorize来显示谷歌登录弹出窗口。用户成功登录后,我收到id_token。然后将此令牌发送到后端服务器,以验证/验证用户并将其登录到我的应用程序。例如:

const config: gapi.auth2.AuthorizeConfig = {
client_id: <client_id>,
scope: 'email profile openid',
prompt: 'consent',
response_type: 'id_token',
include_granted_scopes: false
};

gapi.auth2.authorize(config, (response) => {
console.log(response.id_token);
// redirect or send to backend server
})

据我所见,现在有办法用新的";谷歌身份服务";API据我所知,您使用google.accounts.id下的方法进行身份验证,使用google.accounts.oauth2下的方法授权。使用google.accounts.id下的方法,我可以只显示prompt(一键弹出(或调用renderButton(渲染按钮可以在一定程度上自定义(。并且CCD_ 10下的方法仅返回"0";access_token";(例如id_token1(或"CCD_;授权码";(例如requestCode(。

我会假设在google.accounts.id下会有一些类似requestIdToken(或requestCredential(的方法。

如何显示登录模式窗口

据我所知,谷歌不希望您创建自己的按钮并添加onClick监听器来调用登录模式窗口。

相反,他们希望自己渲染按钮。

google.accounts.id.renderButton([elementRef or selector], {button config})

您可以使用他们的配置程序来获取该配置。

如何获取id_token

一旦你渲染了按钮,你就需要";初始化监听器";(做一次它不是点击事件(

google.accounts.id.initialize({
client_id: [your_client_id],
callback: (authResponse) => {
const token = authResponse.credentials <-- I believe this is an id_token now
}

现在你只需点击谷歌渲染的按钮,它就会显示模式窗口。当您登录时,应该触发包含JWT令牌的回调。

相关内容

最新更新