2
votes

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. 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 :

Sign In with Google for retrieving Contacts

<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. 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.

$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. How i can got user email id using access token.

1
Hello, welcome to SO, please take a look at how to ask a question: stackoverflow.com/help/how-to-ask Please provide some code (not links), what you have tried and where you have failed! Thank you.AJT82

1 Answers

4
votes
$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.