Google JS API: gapi.auth.signIn 回调函数问题



当我调用gapi.auth.signIn时,它被调用两次:在打开登录弹出窗口之前和用户单击登录按钮之后。但在这两种情况下authResponse参数都不会改变。

这是我的代码示例:

gapi.auth.signIn({
  scope: 'https://www.googleapis.com/auth/plus.login',
  callback: function(authResponse) {
    console.log(authResponse);
  }
)};

这就是对象在这两种情况下authResponse样子

{
  client_id: /* my client id */
  cookie_policy: undefined
  error: "immediate_failed"
  error_subtype: "access_denied"
  expires_at: "1422353634"
  expires_in: "86400"
  g_user_cookie_policy: undefined
  issued_at: "1422267234"
  response_type: "token"
  scope: "https://www.googleapis.com/auth/plus.login"
  state: ""
  status: 
       {
         google_logged_in: false
         method: null
         signed_in: false
       }
 }

编辑:在登录之前,我尝试检查用户是否已经在谷歌中授权,这是以下代码:

gapi.auth.authorize({
      client_id: _googleClientId,
      immediate: true,
      scope: 'https://www.googleapis.com/auth/plus.login'
    }, function(response) {
      if (response.status.signed_in) {
        connectGoogleSuccess(response);
      } else {
        gapi.auth.signIn({
          scope: 'https://www.googleapis.com/auth/plus.login',
          callback: function(authResponse) {
            console.log(authResponse);
          }
        )};
      }
    }
 );

用户单击"登录"按钮后如何正确更改身份验证响应对象?

任何帮助不胜感激)

Okey,这是我得到的解决方法。

  1. 起初,我使用参数"immediate"=true调用gapi.auth.authorize方法,因此不会显示登录弹出窗口。

    gapi.auth.authorize({ client_id: _googleClientId, immediate: true, scope: 'https://www.googleapis.com/auth/plus.login' }, function(response) { if (response.status.signed_in) { connectGoogleSuccess(response); } else { connectGoogle(); } });

  2. 然后我再次调用gapi.auth.authorize方法"immediate"=true,以便用户可以输入他的凭据。

    connectGoogle() { gapi.auth.authorize({ client_id: _googleClientId, immediate: false, scope: 'https://www.googleapis.com/auth/plus.login' }, function(response) { if (response.status.signed_in) { connectGoogleSuccess(response); } }); };

希望这对某人有所帮助!

最新更新