Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Google+ API - get User ID by e-mail?

It's just a few moments Google published its API for Google+ social network

But how can I get numeric ID of user ? Or am I forced to use oAuth?

I know it's written there in URL when you access your/foreign profile page, but how to do it programmaticaly with

info:
http://developers.google.com/+/api/latest/people/get
http://developers.google.com/+/api/oauth

like image 317
Marek Sebera Avatar asked Sep 15 '11 21:09

Marek Sebera


2 Answers

(I'm using the Python API)

When using OAuth you can use the string 'me' instead of userId, that way you can retrieve the public content of the authenticated user:

print service.activities().list(userId='me',collection='public').execute()

I assume you are using PHP API (looking at your tags), try putting 'me' instead of userId.

Edit: When retrieving the authenticated users' profile, you can get the userId from the "id" field (JSON response taken from http://developers.google.com/+/api/):

{
  "kind": "plus#person",
  "id": "118051310819094153327",
  "displayName": "Chirag Shah",
  "url": "https://plus.google.com/118051310819094153327",
  "image": {
    "url": "https://lh5.googleusercontent.com/-XnZDEoiF09Y/AAAAAAAAAAI/AAAAAAAAYCI/7fow4a2UTMU/photo.jpg"
  }
}
like image 129
dusan Avatar answered Oct 06 '22 00:10

dusan


This is what you can do in php

$client->setScopes(array('https://www.googleapis.com/auth/plus.me','https://www.googleapis.com/auth/userinfo.email'));

    $url = 'https://www.googleapis.com/oauth2/v1/userinfo';
    $request = $client->getIo()->authenticatedRequest(new apiHttpRequest($url)); 
    if ($request->getResponseHttpCode() != 200) {
        throw new apiException("http code: " . $request->getResponseHttpCode() . ", response body: " . $request->getResponseBody());
    } 
    $temp = json_decode($request->getResponseBody(), true);
    print_r($temp);
like image 40
Navneet Pandey Avatar answered Oct 06 '22 00:10

Navneet Pandey