我在我的网站上使用google api从google plus api获取用户详细信息。我点击登录按钮登录,然后点击(查看配置文件信息的权限)接受。一切正常,但浏览器控制台显示一个错误。
"未找到名为"signinCallback"的回调函数"cb=gap.loaded_0:492
返回回调函数不起作用。如何解决这个问题?
我的谷歌加api代码:
(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 signinCallback(authResult) {
alert("login success");
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');
var request = gapi.client.plus.people.get({
'userId' : 'me'
});
request.execute(function(resp) {
var email = '';
if(resp['emails'])
{
for(var i = 0; i < resp['emails'].length; i++)
{
if(resp['emails'][i]['type'] == 'account')
{
email = resp['emails'][i]['value'];
}
}
}
alert("email ="+email);
getUserMail(email);
console.log('ID: ' + resp.id);
console.log('Display Name: ' + resp.displayName);
console.log('Image URL: ' + resp.image.url);
console.log('Profile URL: ' + resp.url);
});
} else {
alert("login unsuccessful");
// 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']);
}
}
Html代码:
<span id="signinButton">
<span class="g-signin"
data-callback="signinCallback"
data-clientid="*******.apps.googleusercontent.com"
data-cookiepolicy="single_host_origin"
data-scope="profile">
</span>
</span>
</span>
您在span元素中错误地定义了Google参数。
您应该指定您的谷歌登录参数如下:
<meta name="google-signin-clientid" content="xxxxxxxxxxxxxx.apps.googleusercontent.com" />
<meta name="google-signin-cookiepolicy" content="single_host_origin" />
<meta name="google-signin-callback" content="signinCallback" />
<meta name="google-signin-requestvisibleactions" content="https://schema.org/AddAction" />
<meta name="google-signin-scope" content="https://www.googleapis.com/auth/plus.login" />
<div class="g-signin"><!-- Your button here --></div>
请参阅此处Google+文档中的示例(请参阅步骤4)