简体   繁体   English

如何从访问令牌Google API PHP获取用户电子邮件

[英]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.

相关问题 如何使用谷歌访问令牌获取用户电子邮件? - How to get user email with google access token? 在 PHP 中生成访问令牌后如何获取用户 email - how to get user email after access token generate in PHP Google Ads API - 如何使用 PHP 获取访问和引用令牌 - Google Ads API - How to get the access and referesh token using PHP 如何使用Google API PHP客户端获取OAuth2访问令牌? - How to get OAuth2 access token with Google API PHP client? 如何使用 PHP 获取访问令牌并连接用户而不从 Microsoft Graph API 重定向 - How to Get Access Token and connect user without redirection from Microsoft Graph API using PHP PHP - 如何在没有 API 访问令牌的情况下从 Instagram 获取图像 - PHP - How to get images from Instagram without API Access Token PHP 从 Gotowebinar 获取访问令牌 API - PHP Get Access an Token from Gotowebinar API 一旦拥有访问令牌,如何使用php在服务器上获取Facebook用户电子邮件 - How to get Facebook user email on server with php once I already have the access token php twitter api-如何获取访问令牌? - php twitter api - How to get access token? 如何使用 PHP 获取访问令牌 Strava Api - How to get access token Strava Api with PHP
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM