Google+登录与JavaScript回调问题 [英] Google+ Sign-In with JavaScript callback issue

查看:270
本文介绍了Google+登录与JavaScript回调问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发一项功能,允许用户使用自己的Google帐户登录我的网站。

I am working on a function which allows users to sign in on my website with their Google account.

我的代码基于 Google文档(其他signIn()选项位于元标记中)。

My code is based on the Google documentation (others signIn() options are in meta tags).

function login() {
  gapi.auth.signIn({'callback':
    function (authResult) {
      if (authResult['status']['signed_in']) {
        console.log('Okay');
      }else {
        console.log('Error');
      }
    }
  });
}

当我调用login()时,会出现Google弹出式窗口,

When I call login(), a Google pop up appears, I approve the terms of my application and everything works fine.

但是回调被调用了两次:

But the callback is called twice :


  • 第一种情况:如果我从未批准应用程序权限,则回调将在弹出窗口打开时调用,当我批准权限时。

  • 第二种情况:如果我已经批准了权限,则会写两次Okay。

我向signIn()函数添加了'approvalprompt':'force'选项。回调函数不再调用两次,但它强制用户批准应用程序的权限,即使之前已批准。

I added the option 'approvalprompt': 'force' to the signIn() function. The callback function is no longer called twice but it forces the user to approve the app's permissions, even if previously approved. So it's not user friendly.

有没有一个友好的用户方式批准应用程序的权限一次没有两个回调?

Is there a friendly user way to approve the app's permissions one time without having two callback ?

谢谢。

推荐答案

我在这里面临同样的问题,但我通过按钮点击调用gapi.auth.signIn处理程序。回调仍然被调用两次。我注意到两个authResult对象之间的一个事情是,authResult.status.method在第一次调用(在弹出窗口出现之前)为AUTO,并且在第二次调用中的PROMPT在窗口被自动关闭后由于上一次授权。

I'm facing this same issue here, but I'm calling gapi.auth.signIn() via a button click handler. The callback is still called twice. One thing I noticed between the two authResult objects was that authResult.status.method is 'AUTO' in the first call (before the popup window appears) and is 'PROMPT' in the second call after the window is auto-dismissed due to previous authorisation.

我现在探索的解决方案是忽略AUTO实例,只处理回调的PROMPT实例。不确定在我撤销Google中的权限后,此操作将如何工作,原因是文档中的状态对象缺少详细信息。

The solution I'm exploring now is to ignore the AUTO instance and only process the PROMPT instance of the callback. Not sure how this will work once I revoke the permissions within Google due to the lack of details in the docs on the 'status' object.

这篇关于Google+登录与JavaScript回调问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆