Search code examples
androidgoogle-tasks-api

Google Tasks Authentication error


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.


Solution

  • 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