0

I want to use google task api and want to get tasklist,update,delete,add etc.. and I found this link https://developers.google.com/google-apps/tasks/oauth-and-tasks-on-android where step by step procedure is given on that link the library which are given are deprecated.

That's why I have downloaded latetst library google-api-java-client-1.12.0-beta from here http://code.google.com/p/google-api-java-client/downloads/detail?name=google-api-java-client-1.12.0-beta.zip&can=2&q= and google-api-services-tasks-v1-rev5-java-1.12.0-beta from here http://code.google.com/p/google-api-java-client/wiki/APIs#Tasks_API and try the code given and similar to it but no luck not get anything i am successfully get accesstoken but not get anything and in the latest libs most of method are changes so how to inialize the Tasks and get TaskList,create,delete etc...... Not a single document i found related to updated library.

Hope for your regards. Thanks.

Khan
  • 7,585
  • 3
  • 27
  • 44

1 Answers1

0

This solution is for Server to server communication using OAuth 2.0 It is a three step process

  1. Authenticate using OAuth 2.0
  2. Get the com.google.api.services.tasks.Tasks service object
  3. Get the required Task or TaskList

In this sample code it uses the domain id "abc.com" and the user is "user1@abc.com". For gmail users, please provide the gmailid (abc@gmail.com) as consumerkey and leave "xoauth_requestor_id" as gmailid

import com.google.api.client.http.*;
import com.google.api.client.http.javanet.NetHttpTransport;
import com.google.api.client.json.jackson.JacksonFactory;
import com.google.api.services.tasks.*;
import com.google.api.client.auth.oauth.OAuthHmacSigner;
import com.google.api.client.auth.oauth.OAuthParameters;

public class GoogleConnection {
public Tasks setup() throws Exception {
    com.google.api.services.tasks.Tasks tasks = null;
    HttpRequestFactory httpRequestFactory = null;
    HttpRequestInitializer httpRequestInitializer = null;
    OAuthHmacSigner signer = new OAuthHmacSigner();
    HttpTransport httpTransport = new NetHttpTransport();
    OAuthParameters oauthParameters = new OAuthParameters();
    final ArrayMap<String, Object> customKeys = new ArrayMap<String, Object>();

    customKeys.add("xoauth_requestor_id", "user1@abc.com");
    signer.clientSharedSecret = "secret_key_received_from_google";
    oauthParameters.version = "2.0";
    oauthParameters.consumerKey = "abc.com";
    oauthParameters.signer = signer;
    httpRequestFactory = createRequestFactory(httpTransport, oauthParameters, "20000", "20000");
    httpRequestInitializer = httpRequestFactory.getInitializer();

    tasks = new  com.google.api.services.tasks.Tasks.Builder(httpTransport,  new JacksonFactory(), httpRequestInitializer)
            .setTasksRequestInitializer(new TasksRequestInitializer() {
              @Override
              public void initializeTasksRequest(TasksRequest<?> request) throws IOException  {
                @SuppressWarnings("rawtypes")
                TasksRequest tasksRequest =  (TasksRequest) request;
                tasksRequest.setUnknownKeys(customKeys);
                tasksRequest.setKey("keyapi_received_from_google_by_registering_your_app");
              }
            })
            .setApplicationName("")
            .build();

    return tasks;
  }
 }

Getting Tasks from a Task List Instantiate GoogleConnection class

public List<com.google.api.services.tasks.model.Task> getTasksFromTaskList(String taskListId) throws Exception {
com.google.api.services.tasks.Tasks tasksService = googleConnection.setup();
com.google.api.services.tasks.model.Tasks result = tasksService .tasks().list(taskListId).execute();
return result.getItems();

}