Question

I'm working on a Google Tasks App with the new API, but I can't get the authorization to work. I'm using the Android AccountManager to get an authToken, the weird thing is I'm getting an error stating I'm using wrong credentials:

{
    "error": {
        "errors": [
            {
                "domain": "global",
                "reason": "invalid",
                "message": "Invalid Credentials",
                "locationType": "header",
                "location": "Authorization"
            }
        ],
        "code": 401,
        "message": "Invalid Credentials"
    }
}

My code:

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    transport = AndroidHttp.newCompatibleTransport();
    transport.defaultHeaders = new GoogleHeaders();

    gotAccount(false);
}

private void authenticated(String authToken) {
    ((GoogleHeaders) transport.defaultHeaders).setGoogleLogin(authToken);
    Tasks service = new Tasks(transport, new GsonFactory());

    try {
        TaskLists taskLists = service.tasklists.list().execute();
    } catch (Exception e) {
        handleException(e);
    }

}

private void gotAccount(final AccountManager manager, final Account account) {
    new Thread() {

        @Override
        public void run() {
            try {
                final Bundle bundle = manager.getAuthToken(account, AUTH_TOKEN_TYPE, true,
                        null, null).getResult();
                runOnUiThread(new Runnable() {

                    @Override
                    public void run() {
                        if (bundle.containsKey(AccountManager.KEY_INTENT)) {
                            Intent intent = bundle.getParcelable(AccountManager.KEY_INTENT);
                            int flags = intent.getFlags();
                            flags &= ~Intent.FLAG_ACTIVITY_NEW_TASK;
                            intent.setFlags(flags);
                            startActivityForResult(intent, REQUEST_AUTHENTICATE);
                        } else if (bundle.containsKey(AccountManager.KEY_AUTHTOKEN)) {
                            authenticated(bundle.getString(AccountManager.KEY_AUTHTOKEN));
                        }
                    }

                });
            } catch (Exception e) {
                handleException(e);
            }
        };
    }.start();
}

private void gotAccount(boolean tokenExpired) {
    AccountManager manager = AccountManager.get(this);
    Account account = manager.getAccountsByType("com.google")[0];
    if (tokenExpired) {
        manager.invalidateAuthToken("com.google", mAuthToken);
    }
    gotAccount(manager, account);
}

I've tried to use a HttpRequestInitializer and apply the authToken in a HttpExecuteInterceptor instead of using the (deprecated) HttpTransport.defaultHeaders but that doesn't work either.

new Tasks(transport, initializer, new GsonFactory());

What am I doing wrong? My credentials are obviously correct, GMail is working fine.

Was it helpful?

Solution

I found the problem: I was using the authTokenType goanna_mobile, which I found in another app. Even though the AccountManager asks me to allow my app to access Google Tasks, it is the wrong authTokenType.

The correct one is cl for Google Calendar.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top