2013-06-24 77 views
1

我想在我的网站www.bendali.co.za上添加Google +登录/注册按钮。我已经在这个代码:Google+登录/新人按钮

<!-- Place this asynchronous JavaScript just before your </body> tag --> 
    <script type="text/javascript"> 

     (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) { 
  if (authResult['access_token']) { 
    // Successfully authorized 
    // Hide the sign-in button now that the user is authorized, for example: 
    document.getElementById('signinButton').setAttribute('style', 'display: none'); 
  } else if (authResult['error']) { 
    // There was an error. 
    // Possible error codes: 
    //   "access_denied" - User denied access to your app 
    //   "immediate_failed" - Could not automatically log in the user 
    // console.log('There was an error: ' + authResult['error']); 
  } 
} 


    </script> 

<span id="signinButton"> 
    <span 
    class="g-signin" 
    data-callback="signinCallback" 
    data-clientid="712370252357.apps.googleusercontent.com" 
    data-cookiepolicy="single_host_origin" 
    data-requestvisibleactions="http://schemas.google.com/AddActivity" 
    data-scope="https://www.googleapis.com/auth/plus.login"> 

</span> 
</span> 

我从https://developers.google.com/+/web/signin/它采取。但是当我按下它时,它给了我对话框和一切,我接受它,按钮消失(所有这些都是正常的)。但它没有登录我或任何东西。我如何才能做到这一点?

在此先感谢。

回答

1

我没有看到您的代码的任何问题,并在我的机器上快速测试显示它的工作。

您可以通过打开Web浏览器的JavaScript控制台并尝试获取当前会话的访问令牌来测试您是否已登录。下面的函数:

gapi.auth.getToken(); 

将返回如果您还没有登录和这样的事情:如果您登录

Object {state: "", access_token: "ya29.lkjasdlkjasd....", token_type: "Bearer", expires_in: "3600", code: "4/.....wo8WbjRKPN4WMqTmHjyTFGPeKJnufgI"…} 

控制台还应该给你有关的任何详细信息阻止登录代码工作的错误。

如果你正在寻找做更复杂的东西,如用户登录到你的网站,你应该用Google+ Quickstarts开始得到如何通过授权用户连接服务器的要点,然后take a look at Photohunt一个更全面现场示例。

+0

你是否设法登录?我的意思是它是否告诉你,你已经登录? – DLJ

+1

你是什么意思登录你?简短版本是您在成功登录后处于“已授权”状态。一旦您处于授权状态,您的应用程序就可以代表用户向Google进行API调用(获取用户的显示名称,列出可见的人员你的应用程序,等等)。 – class

+0

要反映正在登录的用户,我们建议在登录回调中进行一次people.get API调用,以便您可以获取用户的姓名和个人资料图像来迎接他们。此时,您已经可以代表用户进行API调用了。要了解有关迎接用户的更多信息,请在今天下午2:30 PST收听我们的Google+开发人员现场节目。 (https://plus.google.com/events/cblr79ob8a042sjg64jgf93ka20) – Joanna