【问题标题】:How to use Google task API ?Initialize Tasks Get TaskList etc.?如何使用谷歌任务 API?初始化任务获取任务列表等?
【发布时间】:2012-11-30 10:15:27
【问题描述】:

我想使用 google 任务 api 并想要获取任务列表、更新、删除、添加等。我找到了这个链接 https://developers.google.com/google-apps/tasks/oauth-and-tasks-on-android,其中在该链接上给出了逐步过程,给出的库已被弃用。

这就是我从这里http://code.google.com/p/google-api-java-client/downloads/detail?name=google-api-java-client-1.12.0-beta.zip&can=2&q= 和 google-api-services-tasks-v1-rev5 下载最新库 google-api-java-client-1.12.0-beta 的原因-java-1.12.0-beta 从这里http://code.google.com/p/google-api-java-client/wiki/APIs#Tasks_API 并尝试给出的代码并与它类似但没有运气没有得到任何东西我成功获得了accesstoken但没有得到任何东西并且在最新的库中大多数方法是更改因此如何初始化任务并获取任务列表,创建,删除等......我没有找到与更新库相关的单个文档。

希望您的问候。 谢谢。

【问题讨论】:

    标签: java android google-tasks-api google-tasks


    【解决方案1】:

    此解决方案适用于使用 OAuth 2.0 的服务器到服务器通信 这是一个三步过程

    1. 使用 OAuth 2.0 进行身份验证
    2. 获取 com.google.api.services.tasks.Tasks 服务对象
    3. 获取所需的Task或TaskList

    在此示例代码中,它使用域 ID“abc.com”,用户为“user1@abc.com”。对于 gmail 用户,请提供 gmailid (abc@gmail.com) 作为 consumerkey,并将“xoauth_requestor_id”保留为 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;
      }
     }
    

    从任务列表中获取任务 实例化 GoogleConnection 类

    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();
    

    }

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-09-07
      • 1970-01-01
      • 1970-01-01
      • 2016-10-15
      • 1970-01-01
      • 2021-04-14
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多