Adrian
Adrian

Reputation: 1539

Google Contacts API - getting image

I'm having a simple problem while retrieving user's contact's images. So I make a request to

$url = 'https://www.google.com/m8/feeds/contacts/default/full?alt=json&max-results=9999&oauth_token=' . $accessToken;

And then I get huge JSON from which I have extracted full name and email. Then I tried getting contact's image, and haven't had any success.

I get with all contacts, and everyone has following structure:

entry [id, updated, category, title, link, gd$email].

In the link section, I got four links, of which first two are some kind of link to an image content but I cannot retrieve it.

Any body had any success in doing this kind of work !?

Tnx alot!

BTW: If some data from my case is missing, please report and I'll add it too!

EDIT: I have also tried to reach the image with URL

 http://profiles.google.com/s2/photos/profile/" + userid + "?sz=" + size;

but the problem is I cannot get userId from contacts JSON.

Upvotes: 5

Views: 2651

Answers (2)

db80
db80

Reputation: 4427

You must call the photo endpoint and append to it a valid access token as a query parameter.

Example:

https://www.google.com/m8/feeds/photos/media/name.surname%40gmail.com/0?access_token=ACCESS_TOKEN

Docs:

https://developers.google.com/google-apps/contacts/v3/#contact_photo_management

Upvotes: 3

Pankaj kumar
Pankaj kumar

Reputation: 1

Make a request again if the mentioned tags are available as given below.

$add = new Google_HttpRequest("https://www.google.com/m8/feeds/contacts/default/full?alt=json&v=3.0&max-results=500&access_token='.$token->access_token");
$add->setRequestMethod("GET");
$add->setRequestHeaders(array('GData-Version' => '3.0', 'content-type' => 'application/atom+xml; charset=UTF-8; type=feed'));

$submit = $client->getIo()->authenticatedRequest($add);
$sub_response = $submit->getResponseBody();

$temp = json_decode($sub_response, true);

foreach ($temp['feed']['entry'] as $image) {
  $google_contact_id = $image['link'][2]['href'];
  if (isset($image['link'][0]['href'])) {
    $photo = new Google_HttpRequest($image['link'][0]['href']);
    $photo_val = $client->getIo()->authenticatedRequest($photo);

    $photo_return = $photo_val->getResponseBody();                       
    $imgData = base64_encode($photo_return);
    $pro_image = 'data:image/jpeg;base64, ' . $imgData . '';
  }
}

Upvotes: -1

Related Questions