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

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?

  • 写回答

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'];
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 寻一个支付宝扫码远程授权登录的软件助手app
  • ¥15 解riccati方程组
  • ¥15 display:none;样式在嵌套结构中的已设置了display样式的元素上不起作用?
  • ¥30 用arduino开发esp32控制ps2手柄一直报错
  • ¥15 使用rabbitMQ 消息队列作为url源进行多线程爬取时,总有几个url没有处理的问题。
  • ¥15 Ubuntu在安装序列比对软件STAR时出现报错如何解决
  • ¥50 树莓派安卓APK系统签名
  • ¥65 汇编语言除法溢出问题
  • ¥15 Visual Studio问题
  • ¥20 求一个html代码,有偿