[英]How to get user email from access token google api php
This question ask many time but i have some other issue .i have got access token successfully and got contact list access fully using this google.com/m8/feeds/contacts/default api. 这个问题问了很多次,但我还有其他问题。我已经成功使用此google.com/m8/feeds/contacts/default API获得了访问令牌,并获得了联系人列表的完全访问权限。 Now i want retrieve user info using same access token and use Index.php i use this code :This is google login page where any user login using google account :
现在我想使用相同的访问令牌检索用户信息并使用Index.php我使用此代码:这是google登录页面,其中任何用户都使用google帐户登录:
Sign In with Google for retrieving Contacts 与Google登录以检索联系人
<a href="https://accounts.google.com/o/oauth2/auth?client_id=CLIENT_ID&
redirect_uri=callback.php&
**scope=https://www.google.com/m8/feeds/&response_type=code**">
<img src="images/sign1.png" alt="" id="signimg"/>
</a>
After user login with google account it is go to on callback.php below is code for callback.php . 用户使用google帐户登录后,转到callback.php,以下为callback.php的代码。 Now i will fetch contacts I got all contacts successfully using this access token
现在,我将使用此访问令牌获取联系人,我已成功获取所有联系人
$url = 'google.com/m8/feeds/contacts/default/full?
max-results=' . $max_results . '&oauth_token=' . $accesstoken;
$xmlresponse = curl_file_get_contents($url);
Now i am want fetch userinfo
like email and name of user using this Access Token But i can not fetch any details using this access token. 现在,我想使用此访问令牌获取
userinfo
如电子邮件和用户名,但我无法使用此访问令牌获取任何详细信息。
$userDetails = file_get_contents('https://www.googleapis.com/oauth2/v1/userinfo?
access_token=' . $accesstoken);
$userData = json_decode($userDetails);
echo 'userData='.$userData;
I have check also google developer console for that there is permission or not . 我也检查了谷歌开发者控制台是否有权限。 I have given permission for contact , gmail and google+ api.
我已经授予了contact,gmail和google + api的权限。 How i can got user email id using access token.
我如何使用访问令牌获取用户电子邮件ID。
$client = new Google_Client();
$client->setApplicationName("Google OAuth Login Example");
$client->setClientId($client_id);
$client->setClientSecret($client_secret);
$client->setRedirectUri($redirect_uri);
$client->setDeveloperKey($simple_api_key);
$client->setAccessType('offline');
$client->setScopes(array('https://www.googleapis.com/auth/calendar',"https://www.googleapis.com/auth/userinfo.email"));
$objOAuthService = new Google_Service_Oauth2($client);
if ($client->getAccessToken()) {
$userData = $objOAuthService->userinfo->get();
$_SESSION['access_token'] = $client->getAccessToken();
}
print_r($userData);
userdata will have info about logged in user. userdata将具有有关已登录用户的信息。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.