谷歌sign_in:如何在javascript api中使用redirect_uri

P.h*_*ter 2 javascript redirect google-api firebase-authentication google-signin

我正在尝试将谷歌登录集成到我的网站中,并且我使用firebase进行身份验证,然后我授权用户访问他们的谷歌驱动器。

在访问他们之后,Google drives我希望页面重定向到另一个页面。(localhost:8080/afterlogin),其中将显示“ WELCOME

但是用户没有到达所需的页面,我已在开发人员控制台中添加了确切的 redierct_uri 但没有帮助。

这是我用于初始化 auth 对象的代码。

gapi.load('auth2', function () {
        // Retrieve the singleton for the GoogleAuth library and set up the client.
        auth2 = gapi.auth2.init({
            client_id: 'MYID.apps.googleusercontent.com',
            cookiepolicy: 'single_host_origin',
            redirect_uri: 'http://localhost:8080/afterlogin'

            // Request scopes in addition to 'profile' and 'email'
            //scope: 'additional_scope'
        });
Run Code Online (Sandbox Code Playgroud)

我正在使用 javascript,并且我在文档中读到 javascript api 不使用redirect_uris

但是在这里的文档中

据说我们可以通过使用gapi.auth2.ClientConfig而不是添加redirect_uris gapi.auth2.init

但是,使用gapi.auth2.ClientConfig而不是gapi.auth2.init给我带来错误。所以我添加了redirect_uri后一个,如上面的代码所示。也许这就是它不起作用的原因?

或者我在使用时做错了什么gapi.auth2.ClientConfig

有什么猜测吗?

End*_*loy 5

文档说您需要使用ux_mode='redirect'覆盖默认的redirect_uri。有时 Google 的文档比需要的要复杂得多。我认为您正在处理您发布的代码所产生的承诺。我会尝试这个:

     gapi.load('auth2', function () {
        auth2 = gapi.auth2.init({
            client_id: 'MYID.apps.googleusercontent.com',
            cookiepolicy: 'single_host_origin',
            ux_mode: 'redirect',
            redirect_uri: 'http://localhost:8080/afterlogin'

            // Request scopes in addition to 'profile' and 'email'
            //scope: 'additional_scope'
        });

        auth2.signIn().then(function() {
          var profile = auth2.currentUser.get().getBasicProfile();
          console.log('Full Name: ' + profile.getName());
        })
Run Code Online (Sandbox Code Playgroud)

来自文档,在gapi.auth2.ClientConfig下

在此输入图像描述

=编辑 - 我解决了标志的问题

  • 它应该是 ux_mode:'redirect' 而不是 '='。我试图修改它,但我无法做到这一点,因为它更改的字符少于 6 个。 (2认同)