Google JavaScript API returns 客户端初始化错误

Google JavaScript API returns error on client init

我的同事创建了一个新的 repo,在他们的机器上加载 Google JS API 加载没有问题,但是当我尝试 运行 gapi.client.init 它 returns下面的错误。

{
  "error" : "invalid_request",
  "error_description" : "invalid login_hint."
}

当我查看网络上的请求时,它发送一个字符串作为查询字符串参数 login_hint 的值,但我的代码库中没有该字符串。

login_hint:AJDLj6...iriMjPL4JkmR_A

如果我从 Google API 控制台创建一组新的凭据并使用新的客户端 ID,它会像我预期的那样工作。但是使用现有的一组凭据会导致错误,而无需更改任何其他内容。

我的代码如下所示:

export class GoogleApiService {

    CLIENT_ID: string = '<removed>.apps.googleusercontent.com';
    DISCOVERY_DOCS = ['<removed>'];
    SCOPES = '<removed>';

    constructor() {
        window['initGapi'] = (ev) => {
            this.handleClientLoad();
        };
        this.loadScript();
    }

    loadScript() {
        let script = document.createElement('script');
        script.src = 'https://apis.google.com/js/api.js?onload=initGapi';
        script.type = 'text/javascript';
        document.getElementsByTagName('head')[0].appendChild(script);
    }

    handleClientLoad() {
        gapi.load('client:auth2', this.initClient.bind(this));
    }

    initClient() {
        gapi.client.init({
            discoveryDocs: this.DISCOVERY_DOCS,
            clientId: this.CLIENT_ID,
            scope: this.SCOPES
        }).then(() => {
          console.log('loaded');
        },
        (err) => console.error(err));
    }
}

我没有指定 login_hint,那么为什么在请求中将其发送到 Google?为什么它不正确以及如何防止错误消息的发生?

为了它的价值,我最终注销了我的 Google 帐户,在我的应用程序中单击注册 link,然后在弹出窗口 window 中,登录到我的Google 帐户,现在一切正常,我无法重现。不确定最初是什么原因造成的,但这是我的解决方案。

希望我的用户永远不会遇到这个问题。