douyi1944
2013-07-23 06:32
浏览 654
已采纳

HTTPS GET请求https://www.googleapis.com/plus/v1/people/me

ok, here's a snippet of my code

$client = new Google_Client();
$client->setScopes(array('https://www.googleapis.com/auth/plus.me','https://www.googleapis.com/auth/userinfo.email'));
$plus = new Google_PlusService($client);

if (isset($_SESSION['token'])) {
  $client->setAccessToken($_SESSION['token']);
} else {
    $_SESSION['auth_token'] = $client->authenticate();
}

and now I've got the $_SESSION['auth_token'] which contains something like

{"access_token":"ya29.AHESblablablabalbalbalbagTUI4-MfZvwtb6A","token_type":"Bearer","expires_in":3600,"id_token":"blablablabalbalbalba.eyJpc3MiOiJhY2NvdW50cy5nb29nbGUuY29tIiwiY2lkIjoiNzAzNTg5ODA3MjMxLmFwcHMuZ29vZ2xldXNlcmNvbnRlbnQuY29tIiwiYXpwIjoiNzAzNTg5ODA3MjMxLmFwcHMuZ29vZ2xldXNlcmNvbnRlbnQuY29tIiwiYXVkIjoiNzAzNTgblablablabalbalbalbaRlbnQuY29tIiwiaGQiOiJnZW9mbGV4LmNvbS5teSIsInZlcmlmaWVkX2VtYWlsIjoidHJ1ZSIsImVtYWlsX3blablablabalbalbalbaFzei00N25nN3VoLU5NUmh3IiwiYXRfaGFzaCI6IllEeC13UXN6LTQ3bmc3dWgtTk1SaHciLCJpYXQiOjEzNzQ1NjA1NDcsImV4cCI6MTM3NDU2NDQ0N30.ek1FX-pY8BdaI8sDhA0rwmiIDyBV6pHPvBi-l7BxDJCR-YoDQGFN8y-kA1xg-vmoReiVCwhvQhHK__jc-xFSLfJDLbwk-UGyRKu_O7ZCaSviP7WupexIYvLGCyvgLBdjG3K7UIcq0o3L15mQPvQJr9LA7325gwEOZ12Pc1lAHKw","refresh_token":"1\/Y7oilVvUUutfkKMDfblablablabalbalbalbaE","created":1374560847}

my question is, how do I get the user's email? i know you can just call

https://www.googleapis.com/oauth2/v1/userinfo?alt=json&access_token=youraccess_token

but is that the 'official' way to get the users's email? because from here https://developers.google.com/accounts/docs/OAuth2Login#exchangecode it says

The Access Token that your application received during the authentication flow with Google can be used to obtain additional profile information about the user. Please be advised that Google is modifying the behavior of the different endpoints to simplify their usage. We currently recommend the following based on existing behavior. If you are using Login with Google+, you should always retrieve user profile information from the People API by sending an HTTPS GET request to https://www.googleapis.com/plus/v1/people/me using your Access Token to authenticate the request.

So i guess the best way is to perform HTTPS GET request to googleapis.com.. but I don't know how to do it. Can anyone lead the way?

图片转代码服务由CSDN问答提供 功能建议

好的,这是我的代码片段

  $ client  = new Google_Client(); 
 $ client-> setScopes(array('https://www.googleapis.com/auth/plus.me','https://www.googleapis.com/auth/userinfo。  email')); 
 $ plus = new Google_PlusService($ client); 
 
if(isset($ _ SESSION ['token'])){
 $ client-> setAccessToken($ _ SESSION ['token']  ); 
} else {
 $ _SESSION ['auth_token'] = $ client-> authenticate(); 
} 
   
 
 

现在我' 得到了$ _SESSION ['auth_token'],其中包含

  {“access_token”:“ya29.AHESblablablabalbalbalbagTUI4-MfZvwtb6A”,“token_type”:“Bearer”,“  expires_in “:3600,” id_token “:” blablablabalbalbalba.eyJpc3MiOiJhY2NvdW50cy5nb29nbGUuY29tIiwiY2lkIjoiNzAzNTg5ODA3MjMxLmFwcHMuZ29vZ2xldXNlcmNvbnRlbnQuY29tIiwiYXpwIjoiNzAzNTg5ODA3MjMxLmFwcHMuZ29vZ2xldXNlcmNvbnRlbnQuY29tIiwiYXVkIjoiNzAzNTgblablablabalbalbalbaRlbnQuY29tIiwiaGQiOiJnZW9mbGV4LmNvbS5teSIsInZlcmlmaWVkX2VtYWlsIjoidHJ1ZSIsImVtYWlsX3blablab  labalbalbalbaFzei00N25nN3VoLU5NUmh3IiwiYXRfaGFzaCI6IllEeC13UXN6LTQ3bmc3dWgtTk1SaHciLCJpYXQiOjEzNzQ1NjA1NDcsImV4cCI6MTM3NDU2NDQ0N30.ek1FX-pY8BdaI8sDhA0rwmiIDyBV6pHPvBi-l7BxDJCR-YoDQGFN8y-kA1xg-vmoReiVCwhvQhHK__jc-xFSLfJDLbwk-UGyRKu_O7ZCaSviP7WupexIYvLGCyvgLBdjG3K7UIcq0o3L15mQPvQJr9LA7325gwEOZ12Pc1lAHKw”, “refresh_token”: “1 \ / Y7oilVvUUutfkKMDfblablablabalbalbalbaE”, “创建”:1374560847} 
   
 
  

我的问题是,如何获取用户的电子邮件? 我知道你可以打电话 <前> https://www.googleapis.com/oauth2/v1/userinfo?alt=json&access_token=youraccess_token < / pre>

但这是获取用户电子邮件的“官方”方式吗? 因为从这里 https://developers.google.com/accounts/docs/OAuth2Login#exchangecode < / a>它说

您的应用程序在与Google的身份验证流程中收到的访问令牌可用于获取有关该用户的其他配置文件信息。 请注意,Google正在修改不同端点的行为以简化其使用。 我们目前根据现有行为推荐以下内容。 如果您使用“登录Google+”,则应始终通过向 https://www.googleapis.com/plus/v1/people/me 使用您的访问令牌验证请求。

所以我猜 最好的方法是对googleapis.com执行HTTPS GET请求..但我不知道该怎么做。 任何人都可以带路吗?

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

1条回答 默认 最新

  • donglanying3855 2013-07-23 06:41
    已采纳

    Create a Google_Oauth2Service object (put it under the $plus initialization), like:

    $client = new Google_Client();
    $plus = new Google_PlusService($client);
    $oauth2 = new Google_Oauth2Service($client);
    

    and after succesfully authenticating the client use:

    $userInfo = $oauth2->userinfo->get();
    $email = $userInfo['email'];
    
    点赞 打赏 评论

相关推荐 更多相似问题