Google 任务身份验证错误

发布于 2024-12-18 14:33:41 字数 4125 浏览 2 评论 0原文

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

我使用了这个 教程任务API 来实现代码,但它不起作用,而且网上也没有太多其他教程。这是我的开始活动的片段。

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

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

这是“接收任务”-方法:

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

它时,它会抛出异常并且没有任何反应,所以我猜任务列表 - 对象没有初始化,但我不知道为什么“它无法响应”(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 之间的区别在哪里,如果有人可以帮助我,那就太好了,因为我不知道还能在哪里找到有关此主题的帮助。 多谢。

I've started programming an Android application which implements a ListView and a simple Database and offers the opportunity to receive tasks from a specific Google Account and than add them to the ListView.

I used this Tutorial and the Tasks API to implement the code, but it doesn't work and there aren't many other tutorials on the web. Here's a snippet of my Start-Activity.

I created only one account on my phone, so I left out the 'Choose Accounts'-Dialog:

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

Here is the '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();
        }
    }

Everything's working fine - I get a token, there's a service-Object and a Account-Object - but when we get to

TaskLists taskLists = service.tasklists.list().execute();

it throws an exception and nothing happens, so I guess the TaskLists - Object isn't initialized, but I have no idea why "it is unable to respond" (LogCat).

Here's the 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)

I also wonder where the different between the tutorial and the API is, it would be great if someone could help me, because i don't know where else i could find help about this topic.
Thanks a lot.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

丶视觉 2024-12-25 14:33:41

对于遇到同样问题的每个人,请尝试将令牌保存在共享首选项中,并在每次启动应用程序时使其失效,并由于帐户管理器的错误而生成新的令牌;我的问题可以这样解决:
看这个:
https://groups.google.com/group/google-tasks-api/browse_thread/thread/6d65116019baf122/81293191c46ebdc6?lnk=gst&q=401#81293191c46ebdc6

For everybody who got the same problem, try to save the token in the Shared Preferences and invalidate it every time the application is started and generate a new one due to a bug of the Account Manager; my problem could be solved this way:
See this:
https://groups.google.com/group/google-tasks-api/browse_thread/thread/6d65116019baf122/81293191c46ebdc6?lnk=gst&q=401#81293191c46ebdc6

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文