当我调用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对象?
感谢任何帮助)
答案 0 :(得分: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();
}
});
然后我再次使用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);
}
});
};
希望这会对某人有所帮助!