登录我的网站使用google api [onsignincallback not found error]

时间:2014-09-17 06:42:55

标签: javascript google-api google-plus google-api-java-client google-api-client

我在我的网站上使用google api从google plus api获取用户详细信息。点击登录按钮登录并点击(查看个人资料信息的权限)接受。一切正常,但浏览器控制台显示一个错误。

"回调函数名为" signinCallback"找不到" CB = gapi.loaded_0:492

返回回调函数无法正常工作。如何解决该问题?

我的google plus 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>

1 个答案:

答案 0 :(得分:0)

您在span元素中错误地定义了Google参数。

您应该按照以下方式指定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+文档here中的示例(请参阅步骤4)