Hazem Farahat
Hazem Farahat

Reputation: 3790

Using Google Play services with Google Tasks API

I am trying to integrate my app with a Google API, say Google Tasks,

Before Google Play Services I would use the Google APIs Client Library for Java and the AccountManager to retrieve and access the OAUTH token for the API.

The pros of this methos is that I can:

the cons is that:

I guess 'Google play services' came to change all that:

But the ease of access to the Java Client library API methods is lost!!! I have to create all the requests by myself.

The question is What is the best way to authenticate and request the OAUTH token using play services and after that continue to work with the Client library APIS to access the actual API methods?

Upvotes: 3

Views: 1857

Answers (1)

Roman Nurik
Roman Nurik

Reputation: 29745

You should be able to pass the result of GoogleAuthUtil.getToken (from Google Play Services) directly to GoogleCredential.setAccessToken (from the Google APIs Client Library).

An example of the latter can be found in TasksSample.java. In fact, since the AccountManager.getAuthToken and GoogleAuthUtil.getToken APIs are so similar, save for the asynchronous vs. synchronous difference, you should be able to modify that Tasks sample to use Google Play Services without too much difficulty.

Some very rough pseudocode would be:

...
// this should all be in a separate thread (e.g. AsyncTask)
final String token = GoogleAuthUtil.getToken(context, email, scope);
GoogleCredential credential = new GoogleCredential();
credential.setAccessToken(token);

Tasks service = new Tasks.Builder(transport, jsonFactory, credential)
    .setApplicationName("Google-TasksAndroidSample/1.0")
    .setJsonHttpRequestInitializer(new GoogleKeyInitializer(ClientCredentials.KEY))
    .build();

List<String> result = new ArrayList<String>();
Tasks.TasksOperations.List listRequest = service.tasks().list("@default");
listRequest.setFields("items/title");
List<Task> tasks = listRequest.execute().getItems();
...

Upvotes: 3

Related Questions