Google People API For Android Application

Nagashree TJ

Google People API For Android Application

Google released a new API called People API. This API is enough to retrieve user private data and can be able to replace both the existing Google+ API and Google Contacts API.Google says that this new People API should be able to help both The Web and mobile developers.
For example, let’s say you are the authenticated user and you have some list of private contacts. When your app calls people.connections.list to retrieve a list of your connections, you are presented with a consent screen asking to give the app access to the list. If you give permission, the app retrieves a list containing all the private contacts(with a resource name for each person). The app can then call people.get, passing in a resource name, to get private contact and public profile data for each person.
The People API is built on JSON and HTTP, so any standard HTTP client can send request to it and parse the response. Before sending the request you need to follow four steps.

1)Get a Google Account
2)Create a project
3)Create Credentials and Select Application Type
4)Set up your app
1. Get a Google Account
The first thing you need to do is creating Google account. If you already have an account then skip this step.

2. Create a Project
Create a project in Google API console.Click here to create a new project.

3. Select Application Type and Create Credentials
After creating a project it will only ask for creating credentials. Click on credentials and you will get some options. There you have to select OAuth Client Id. After this, you will get a screen to select application type(like Android, web application, iOS, Chrome app, PlayStation 4).There you need to select web application(you should not select android because by selecting android you will not get client secret key).Now you can get Client ID and Client secret key.

4. Set Up Your App
Since People API is used to get private contacts, it uses OAuth 2.0 to identify your application.
Click here to know about 0Auth 2.0.
The following code from google developer console will show how to configure your client and authorize requests using OAuth 2.0 for installed applications.

public static People setUp(Context context, String serverAuthCode) throws IOException {
HttpTransport httpTransPort = new NetHttpTransport();
JacksonFactory jsonFactry = JacksonFactory.getDefaultInstance();
// Go to the Google API Console, open your application’s
// credentials page, and copy the client ID and client secret.
String clientId = “YOUR_CLIENT_ID”;
String clientSecret = “YOUR_CLIENT_SECRET”;
String redirect_Uri = “urn:ietf:wg:oauth:2.0:oob”;

// Exchange auth code for access token
GoogleTokenResponse tokenResponses = new GoogleAuthorizationCodeTokenRequest(
httpTransport,
jsonFactory,
context.getString(your client id),
context.getString(your client secret),
serverAuthCode,
redirectUrl).execute();

// create a GoogleCredential object using the tokens that we got from GoogleTokenResponse
GoogleCredential credentials= new GoogleCredential.Builder()
.setClientSecrets(your client ID,your client secret key)
.setTransport(httpTransPort)
.setJsonFactory(jsonFactry)
.build();

credentials.setFromTokenResponse(tokenResponses);

// credentials can then be used to access Google services
return new People.Builder(httpTransport, jsonFactory, credential)
.setApplicationName(APPLICATION_NAME)
.build();
}

}

Retrieve Profiles and connections
When you are done with all the above steps ,you are ready to retrieve data by sending requests to the People API.

-Code to get the user’s profile:
Person profiles = peopleService.people().get(“people/me”).execute();

-Code to get the profile of a google account:
Person profiles = peopleService.people().get(“account_id”).execute();

-Code to get the user’s connections:
ListConnectionsResponse response=peopleService.people().connections().list(“people/me”).execute();
List<Person> connection = response.getConnections();
Using this connection instance we can get phone numbers, email addresses, gender,age etc., For example use following code to retrieve phone numbers

for (Person person : connection) {
if (!person.isEmpty()) {
List<PhoneNumber> phoneNumber = person.getPhoneNumbers();
if (phoneNumber != null)
for (PhoneNumber phoneNumber : phoneNumber) nameList.add(String.valueOf(person.getPhoneNumbers().get(0).getCanonicalForm()));
}
}

Like getPhoneNumbers() method you can get many methods to retrieve different user data like Photos,biographies,etc,.So this People API is the simplest way to retrieve user private data.As you can see in code part ,it will tell how to use Google People API in java(i,e for android application) but you can use this API with Python ,.Net ,PHP also. You can get code in different languages here.

Google Certified Agency

about the author

Nagashree TJ

  1. rama tulasi

    December 8, 2017

    flag

    can’t access emailAddress, others are working fine. hwo can i get emal adress?please help me