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

标签 javascript google-api google-plus google-api-java-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>

最佳答案

您在 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)

关于javascript - 使用 google api 登录我的网站 [onsignincallback not found error],我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25883944/

相关文章:

android - 当我从其他计算机上的存储库克隆项目时 Ionic2 出错

google-plus - 创建 Google+ 应用程序

javascript - Google map `region` 参数是自动确定的吗?

javascript - 如何将来自 json API 响应的数据存储到 ReactJS 中的数组中?

javascript - 使用异步方法返回的对象

android - 如何开发使用 Google Calendar API 的 Android 应用程序

javascript - CometD v3.0.9 - 服务器端断开连接未在消息上设置成功标志( channel /元/断开连接)

javascript - 在 Node.js glob 结果中加入数组循环

python - 在 python 中执行 oauth2 headless 对抗 google api

wordpress - 使用 ADDTHIS 分享我的 Google+ 信息页上的内容