gpt4 book ai didi

android - Google Tasks 身份验证错误

转载 作者:太空宇宙 更新时间:2023-11-03 13:35:05 26 4
gpt4 key购买 nike

我已经开始编写一个 Android 应用程序,它实现了一个 ListView 和一个简单的数据库,并提供了从特定 Google 帐户接收任务并将其添加到 ListView 的机会。

我用了这个TutorialTasks API实现代码,但它不起作用,网络上也没有很多其他教程。这是我的开始 Activity 的 fragment 。

我在手机上只创建了一个帐户,所以我省略了“选择帐户”对话框:

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”方法:

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

一切正常 - 我得到一个 token ,有一个服务对象和一个帐户对象 - 但是当我们到达时

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 之间的区别在哪里,如果有人能帮助我,那就太好了,因为我不知道我还能在哪里找到关于这个主题的帮助。非常感谢。

最佳答案

遇到同样问题的各位,尝试将token保存在Shared Preferences中,每次启动应用时将其失效,并由于Account Manager的bug而生成一个新的;我的问题可以这样解决:看到这个: https://groups.google.com/group/google-tasks-api/browse_thread/thread/6d65116019baf122/81293191c46ebdc6?lnk=gst&q=401#81293191c46ebdc6

关于android - Google Tasks 身份验证错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8312984/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com