2015-07-03 21 views
2

我试图在网站上实施标准的Google登录选项,因为他们在此处建议:https://developers.google.com/identity/sign-in/web/试图为网站实施Google登录,但该按钮未显示

作为一个开始,我还以为我只是遵循的基本指令(https://developers.google.com/identity/sign-in/web/devconsole-project),我仅仅使用他们的示例代码更多:

<html lang="en"> 
    <head> 
    <meta name="google-signin-scope" content="profile email"> 
    <meta name="google-signin-client_id" content="YOUR_CLIENT_ID.apps.googleusercontent.com"> 
    <script src="https://apis.google.com/js/platform.js" async defer></script> 
    </head> 
    <body> 
    <div class="g-signin2" data-onsuccess="onSignIn" data-theme="dark"></div> 
    <script> 
     function onSignIn(googleUser) { 
     // Useful data for your client-side scripts: 
     var profile = googleUser.getBasicProfile(); 
     console.log("ID: " + profile.getId()); // Don't send this directly to your server! 
     console.log("Name: " + profile.getName()); 
     console.log("Image URL: " + profile.getImageUrl()); 
     console.log("Email: " + profile.getEmail()); 

     // The ID token you need to pass to your backend: 
     var id_token = googleUser.getAuthResponse().id_token; 
     console.log("ID Token: " + id_token); 
     }; 
    </script> 
    </body> 
</html> 

(很明显,在一个替代的客户端id从他们的开发者控制台为我的网站生成)。如上所述,我在创建客户端ID时将该IP地址作为有效的JavaScript来源输入。但是,登录按钮甚至不出现。

我觉得自己像一个白痴,因为我确定有一些明显的步骤,我失踪了,但我一直无法弄清楚。任何建议?

+0

我建议你阅读这篇文章 - http://www.sanwebe.com/2012/11/login-with-google-api-php,这对初学者很好,因为它提供了一个简单的登录系统,你可以复制 - 粘贴和玩。 –

+0

我有完全相同的问题。它不会出现! – mesqueeb

回答

0

您的示例代码适用于我。为了得到它的工作:

  1. 创建一个临时文件夹,并添加一个index.html文件
  2. 编辑index.html,然后你的脚本的全部内容粘贴到
  3. 开始在本地主机上Web服务器,对于我的测试,我用内置的Web服务器的蟒蛇,python -m SimpleHTTPServer 8000
  4. 导航到http://localhost:8000

在运行过程中运行代码的Web服务器,我看到按钮以蓝色显示。有几件事情值得尝试,如果这还不为你工作:

  • 看看内容在另一个浏览器渲染,我更喜欢和铬
  • 使用浏览器的开发者控制台测试,以获取有关JavaScript错误信息
  • 确保你已经正确配置您在Google Developer Console
1

我有同样的问题设置客户端,它是由禁用Adblock的解决。它阻止了脚本正确加载。

0

可能来不及回复,但这里是解决方案。

从加载platorm.js中删除“async defer”。