我想使用google task 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下载了后期库 google-api-java-client-1.12.0-beta 的原因-rev5-java-1.12.0-beta 从这里http://code.google.com/p/google-api-java-client/wiki/APIs#Tasks_API并尝试给出的代码和它类似,但没有运气没有得到任何东西我成功获得accessstoken但没有获得任何东西和最新的libs大多数方法都是更改,所以如何初始化任务并获取TaskList,创建,删除等......没有找到与更新库相关的单个文档。
希望您的问候。 谢谢。
答案 0 :(得分:0)
此解决方案适用于使用OAuth 2.0的服务器到服务器通信 这是一个三步过程
在此示例代码中,它使用域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();
}