谷歌一键式登录不会返回idToken

d--*_*--b 3 javascript google-authentication google-identity

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

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"
    }
  ]
});
Run Code Online (Sandbox Code Playgroud)

我在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); });
Run Code Online (Sandbox Code Playgroud)

credential对象看起来像这样:

{
  authDomain: "http://localhost:3000",
  authMethod: "https://accounts.google.com",
  displayName: "testName",
  id: "testEmail@gmail.com"
}
Run Code Online (Sandbox Code Playgroud)

有没有人设法让这个工作?

小智 9

我有同样的问题,但能够通过https://console.developers.google.com/为我的项目添加正确的"授权JavaScript来源"来解决它.我需要包含URI,包括端口" http:// localhost:3000 "而不仅仅是" http:// localhost ".

在Google页面中 - "如果您使用的是非标准端口,则必须将其包含在原始URI中."