本文介绍了谷歌登录两次?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我通过 JS 使用 Google 登录,看起来我的代码正在获取数据两次.我不确定为什么会发生这种情况.

I'm using Google Login via JS and it appears my code is getting data twice. I'm not sure why this is occurring.

当我单击使用 Google 登录"按钮时,它会为用户输出 (console.log(result)) 数据.然后出现一个提示,要求我选择一个我的帐户(我登录了几个谷歌帐户).当我单击我想要的帐户时,代码会再次吐出该用户数据.

When I click my "Log In with Google" button, it spits out (console.log(result)) data for the user. THEN a prompt occurs asking me to choose an account of mine (I'm logged into several google accounts). When I click the account I'd like, the code then spits out that user data again.

为什么会出现这种情况?这是一个问题,因为在我吐出数据的地方,我想进行 ajax 调用来验证用户,然后重定向它们.所以本质上,它试图这样做两次——这并不酷,如果我不想使用谷歌第一次传回的凭据登录怎么办?

Why is this occurring? It's a problem because where I spit out the data, I'd like to make a ajax call to verify the user and then redirect them. So in essence, it's trying to do this twice -- which is not cool, what if I don't want to login using the credentials google passes back on the first go around?

(function() {
   var po = document.createElement('script');
   po.type = 'text/javascript'; po.async = true;
   po.src = 'https://apis.google.com/js/client:plusone.js';
   var s = document.getElementsByTagName('script')[0];
   s.parentNode.insertBefore(po, s);
 })();

function googleLogin() {
    var additionalParams = {
        'callback': googleCallback
    };

    gapi.auth.signIn(additionalParams);
}

function googleCallback(authResult) {
    if (authResult['status']['signed_in']) {
        gapi.client.load('oauth2', 'v2', function() {
            gapi.client.oauth2.userinfo.get().execute(function(resp) {
                console.log(resp);
            })
        });
    } else {
        console.log('Sign-in state: ' + authResult['error']);
    }
}

更新:如果我退出我的所有 Google 帐户(只有一个除外),对 google 的调用仍然是重复的.这次它登录,我看到 console.log() 输出数据两次.访问令牌是相同的.

Update: If I sign out of all my Google accounts (with the exception of one and only one), the call to google is still duplicated. This time it logs in and I see console.log() outputting data twice. Access tokens are identical.

更新 2: console.log(resp) 输出两次

Update 2: console.log(resp) is outputting twice

更新 3:更多说明:

推荐答案

您遇到了对console.log(resp);"的两次调用在您的googleCallback"函数中,因为:

You are encountering two calls to "console.log(resp);" within your "googleCallback" function because:

您为登录回调定义的函数将被调用每次用户登录状态发生变化时

此引述摘自监控用户的会话状态" 网页.

正如你在文章中看到的,授权结果对象有三个不同的状态方法"值:

As you can see in the article, the authorization result object has three different status "method" values:

  • 提示
  • 自动

因此,当出现登录提示(PROMPT")和出现欢迎回来"横幅(AUTO")时,您的回调代码将被触发.

So your callback code is being triggered when the login prompt appears ("PROMPT") and when the "Welcome back" banner appears ("AUTO").

要阻止回调代码处理每个触发事件,您可以按如下方式更改代码:

To stop your callback code from dealing with each trigger event you could change your code as follows:

function signinCallback(authResult) {
    if (authResult['status']['signed_in']) {
        // Update the app to reflect a signed in user
        // Hide the sign-in button now that the user is authorized, for example:
        // document.getElementById('signinButton').setAttribute('style', 'display: none');

        if (authResult['status']['method'] == 'PROMPT') {
            console.log(authResult['status']['method']);

            gapi.client.load('oauth2', 'v2', function () {
                gapi.client.oauth2.userinfo.get().execute(function (resp) {
                    console.log(resp);
                })
            });
        }
    } else {
        // Update the app to reflect a signed out user
        // Possible error values:
        //   "user_signed_out" - User is signed-out
        //   "access_denied" - User denied access to your app
        //   "immediate_failed" - Could not automatically log-in the user
        console.log('Sign-in state: ' + authResult['error']);
    }
}

如果用户已登录并且触发回调的事件类型为PROMPT",则此代码只会调用gapi.client.oauth2.userinfo.get()"函数.

This code will only call the "gapi.client.oauth2.userinfo.get()" function if a user is signed-in and the event which triggered the callback is of type "PROMPT".

这篇关于谷歌登录两次?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-01 11:54