Skip to main content
 首页 » 编程设计

javascript之谷歌一键登录不返回 idToken

2025年05月04日12lyj

我正在尝试按照本指南使新的谷歌一键登录工作:

https://developers.google.com/identity/one-tap/web/get-started

当我打电话时:

const hintPromise = googleyolo.hint({ 
  supportedAuthMethods: [ 
    "https://accounts.google.com" 
  ], 
  supportedIdTokenProviders: [ 
    // Google can provide ID tokens -- signed assertions of a user's 
    // identity -- which you can use to create more streamlined sign-in 
    // and sign-up experiences. 
    { 
      uri: "https://accounts.google.com", 
      clientId: "YOUR_GOOGLE_CLIENT_ID" 
    } 
  ] 
}); 

我在 promise 回调中得到响应,没有错误。但是 idToken 是空的...
hintPromise.then((credential) => { 
    if (credential.idToken) {                       // <= THIS IS ALWAYS FALSE!!! 
        // Send the token to your auth backend. 
        loginWithGoogleIdToken(credential.idToken); 
    } 
}, (error) => { console.log(error); }); 
credential对象看起来像这样:
{ 
  authDomain: "http://localhost:3000", 
  authMethod: "https://accounts.google.com", 
  displayName: "testName", 
  id: "testEmail@gmail.com" 
} 

有没有人设法让它工作?

请您参考如下方法:

我遇到了同样的问题,但能够通过在 https://console.developers.google.com/ 添加正确的“授权的 JavaScript 来源”来解决它对于我的项目。我需要包含包含端口“http://localhost:3000”的 URI,而不仅仅是“http://localhost”。

从谷歌页面 - “如果您使用的是非标准端口,则必须将其包含在原始 URI 中。”