I am trying to retrieve the photo of a contact using Google Contacts API according to this document. The PHP Client library used is here. I get the code and user information successfully but failed to get the user's photo data.
The response I got is :
Photo query failed - invalid contact ID
.
This is the PHP code of the callback function that I'm using to get the user's photo data :
$client = new Google_Client();
$client->setClientId(GOOGLE_APP_CLIENT_ID);
$client->setClientSecret(GOOGLE_APP_CLIENT_SECRET);
$client->setRedirectUri(GOOGLE_APP_CLIENT_REDIRECT_URI);
$client->addScope("openid profile email https://www.google.com/m8/feeds");
// Create a OAuth 2 service instance
$oauth2 = new Google_Service_Oauth2($client);
// Get the access token using code.
if (isset($_GET['code'])) {
// Authenticate the code
$client->authenticate($_GET['code']);
$_SESSION['access_code'] = $client->getAccessToken();
// Set the access token
$client->setAccessToken($_SESSION['access_token']);
$temp_obj = $client->verifyIdToken();
....
// Get the user's info
$guser_info = $oauth2->userinfo->get();
// Get the contact id
$contact_id = $guser_info['id'];
// Get the user email
$user_email = $guser_info['email'];
// Make an authenticated request to the photo
$req = new Google_Http_Request("https://www.google.com/m8/feeds/photos/media/default/$contact_id");
// For replacing above line with the following also got the same result.
//$req = new Google_Http_Request("https://www.google.com/m8/feeds/photos/media/$user_email/$contact_id");
// Make an authenticated request
$val = $client->getAuth()->authenticatedRequest($req);
print_r($val->getResponseBody());// Get "Photo query failed - invalid contact ID"
} ...
I have enabled Contacts API
in the console project and added scope https://www.google.com/m8/feeds
.
Is the retrieved id different from Contact ID of the user or is there a way to get the user's Contact ID or something wrong in my code?
Any help or comments are appreciated.
I am using Hybridauth with a similar code to get the user profile image (laravel style):
<?php
//@see http://www.mrcasual.com/on/coding/laravel4-package-management-with-composer/
public function getSocial($action = '')
{
// check URL segment
if ($action == 'auth') {
// process authentication
try {
Hybrid_Endpoint::process();
}
catch (Exception $e) {
// redirect back to http://URL/social/
return Redirect::to('login/social');
}
return;
}
try {
// create a HybridAuth object
$socialAuth = new Hybrid_Auth(app_path() . '/config/hybridauth.php');
// authenticate with provider
if($action != ''){
if(in_array($action,array('google'))){
$provider = $socialAuth->authenticate($action);
}else{
return Redirect::to('login');
}
}else{
return Redirect::to('login');
}
// fetch user profile
$userProfile = $provider->getUserProfile();
}
catch(Exception $e) {
// exception codes can be found on HybBridAuth's web site
return Redirect::to('login');
}
access user profile data
echo "Connected with: <b>{$provider->id}</b><br />";
echo "As: <b>{$userProfile->displayName}</b><br />";
echo "<pre>" . print_r( $userProfile, true ) . "</pre><br />";
echo "PhotoURL: {$userProfile->photoUrl}<br />";
die();
}
Maybe your $guser_info
already contains a link to the photo? Did you try to print_r
it?