Google身份验证 - 登录

时间:2012-12-10 21:03:37

标签: php google-authentication

我建立了一个网站并打算用户使用他们的谷歌帐户登录,他们可以使用我们网站上的谷歌电子邮件地址发表评论。

我通过googles文档工作,现在很困惑。

https://developers.google.com/accounts/

我设法让用户授权使用他们的详细信息,google在查询字符串中返回访问令牌等:

的access_token

token_type =承载

expires_in = 3600

我的问题是如何使用访问令牌获取用户的电子邮件地址。我意识到这意味着要求谷歌。但是如何?

我只是希望能帮助一些代码来google并使用我的php页面获取用户的电子邮件。只是最简单的php示例,如果有人有一个?

我一直在寻找几个小时,现在我不知所措。

由于

1 个答案:

答案 0 :(得分:1)

在我的经历中,谷歌客户端不会向用户发送电子邮件,但您可以使用oath2响应来获取它。

一旦拥有访问令牌,就可以执行此操作。希望这有帮助

require_once'path_to_your_google_library / Google_Client.php';

require_once'path_to_your_google_library / contrib / Google_Oauth2Service.php';

$ google_instance = new Google_Client();

$ oath2 = new Google_Oauth2Service($ google_instance);

$ oauth_response = $ oauth2-> userinfo-> get();

$ email_of_user = $ oauth_response ['email'];