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

时间:2015-01-26 10:20:18

标签: javascript google-api

当我调用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);
          }
        )};
      }
    }
 );

如何在用户点击“登录”按钮后正确更改authResponse对象?

感谢任何帮助)

1 个答案:

答案 0 :(得分:1)

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

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

    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); } }); };

  3. 希望这会对某人有所帮助!