如何从Google检索用户的电子邮件 [英] How do I retrieve user's email from Google

查看:75
本文介绍了如何从Google检索用户的电子邮件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用PHP中的Google客户端库.
我已成功通过身份验证.
缺少一件简单的事情(我添加了正确的作用域).我该如何找回完成身份验证过程后,用户的电子邮件.
以下是我所拥有的:

I am using the Google client library in PHP.
I am successfully authenticated.
Missing a simple thing (I added the right scope). How do I retrieve the user's email after I finish the auth process.
Below is what I have:

$client = new Google_Client();
$client->setClientId(MYCLIENTID);
$client->setClientSecret(MYSECRET);
$client->setRedirectUri(SOMEURLINMYSYSTEM);
$service = new Google_Service_Oauth2($client);
$client->addScope(Google_Service_Oauth2::USERINFO_EMAIL);
$client->authenticate($_GET['code']);//I have the right code, and I am being authenticated

//TODO Get from google the user's email ?????????

我在这里使用PHP库: https://code.google.com/p/google-api-php-client/wiki/OAuth2

I am using the PHP library here: https://code.google.com/p/google-api-php-client/wiki/OAuth2

推荐答案

简单得多,您可以这样做:

Much more simple, you can do it like this:

$user = $service->userinfo->get();

echo $user->name;
echo $user->id;
echo $user->email;
echo $user->link;
echo $user->picture;

这篇关于如何从Google检索用户的电子邮件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆