我已经开始编写一个安卓应用程序,它实现了一个ListView和一个简单的数据库,并提供了从特定的谷歌账户接收任务,然后将它们添加到ListView的机会。
我使用这个Tutorial和Tasks API来实现代码,但它不起作用,网上也没有太多其他的教程。这是我的Start-Activity的一个片段。
我在手机上只创建了一个账号,所以我省略了‘选择账号’-对话框:
googleAccountManager = new GoogleAccountManager(RememberMe.this);
Account[] accounts = googleAccountManager.getAccounts();
account = accounts[0];
googleAccountManager.manager.getAuthToken(account, AUTH_TOKEN_TYPE, null, this, new AccountManagerCallback<Bundle>()
{
public void run(AccountManagerFuture<Bundle> future)
{
try
{
// If the user has authorized your application to use the tasks API
// a token is available.
String token = future.getResult().getString(AccountManager.KEY_AUTHTOKEN);
HttpTransport transport = AndroidHttp.newCompatibleTransport();
GoogleAccessProtectedResource googleAccessProtectedResource = new GoogleAccessProtectedResource(token);
service = new Tasks(transport, googleAccessProtectedResource, new JacksonFactory());
service.setKey("AIzaSyDAnO-UGa_zJnqftSVTHnvoHDp8Tfrmtko");
service.setApplicationName("Remember Me");
receivingTasks();
// Now you can use the Tasks API...
}
catch (OperationCanceledException e)
{
// TODO: The user has denied you access to the API, you should handle that
Log.w(TAG, "synchronize - Catch OperationCanceled Exception");
}
catch (Exception e)
{
Log.w(TAG, "synchronize - Catch Exception e");
}
}
}, null);这里是'receiveTasks'-Method:
public void receivingTasks()
{
try
{
TaskLists taskLists = service.tasklists.list().execute();
for (TaskList taskList : taskLists.getItems())
{
Log.w(TAG, "" + taskList.getTitle());
}
}
catch (IOException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
}一切都很正常-我得到了一个令牌,有一个服务对象和一个帐户对象-但当我们
TaskLists taskLists = service.tasklists.list().execute();它抛出一个异常,什么也没有发生,所以我猜TaskLists对象没有初始化,但是我不知道为什么“它无法响应”(LogCat)。
下面是LogCat:
11-29 15:57:58.848: W/DefaultRequestDirector(2878): Authentication error: Unable to respond to any of these challenges: {authsub=WWW-Authenticate: AuthSub realm="https://www.google.com/accounts/AuthSubRequest" allowed-scopes="https://www.googleapis.com/auth/tasks,https://www.googleapis.com/auth/tasks.readonly"}
11-29 15:57:58.858: W/System.err(2878): com.google.api.client.http.HttpResponseException: 401 Unauthorized
11-29 15:57:58.858: W/System.err(2878): at com.google.api.client.http.HttpRequest.execute(HttpRequest.java:669)
11-29 15:57:58.868: W/System.err(2878): at com.google.api.services.tasks.Tasks$RemoteRequest.execute(Tasks.java:1571)
11-29 15:57:58.868: W/System.err(2878): at com.google.api.services.tasks.Tasks$Tasklists$List.executeUnparsed(Tasks.java:1277)
11-29 15:57:58.868: W/System.err(2878): at com.google.api.services.tasks.Tasks$Tasklists$List.execute(Tasks.java:1262)我也想知道教程和API之间的不同之处,如果有人能帮助我就太好了,因为我不知道我还能在哪里找到关于这个主题的帮助。非常感谢。
发布于 2011-12-08 17:46:43
对于每个遇到相同问题的人,请尝试将令牌保存在共享首选项中,并在每次启动应用程序时使其无效,并由于帐户管理器的错误而生成一个新令牌;我的问题可以通过以下方式解决:请参阅此:https://groups.google.com/group/google-tasks-api/browse_thread/thread/6d65116019baf122/81293191c46ebdc6?lnk=gst&q=401#81293191c46ebdc6
https://stackoverflow.com/questions/8312984
复制相似问题