views:

44

answers:

1

I have an android app I want to connect to a Google App Engine based server. I can get the auth token from the AccountManager. It seems the next thing I am supposed to do is talk to an auth page to get a cookie. Following the awesome instructions here: http://blog.notdot.net/2010/05/Authenticating-against-App-Engine-from-an-Android-app I think my url should be:

https://MYAPP.appspot.com/_ah/login?continue=http://localhost/&auth=CrAZYl000ngToken

but rather than a redirect, I get a 500 server error:

Error: Server Error

The server encountered an error and could not complete your request.
If the problem persists, please report your problem and mention this error message 
and the query that caused it.

What's up with that? What is the URL I am supposed to be going to? Or maybe I'm doing something else wrong?

A: 

OK, the URL wasn't wrong after all. The problem was that the token was expired. I was able to solve this by invalidating and re-fetching the token.

private class GetAuthTokenTask extends AsyncTask<Account, Object, String> {

    @Override
    protected String doInBackground(Account... accounts) {
        AccountManager manager = AccountManager.get(getApplicationContext());
        Account account = accounts[0];
        String token = this.buildToken(manager, account);
        manager.invalidateAuthToken(account.type, token);
        return this.buildToken(manager, account);
    }

    private String buildToken(AccountManager manager, Account account) {
        try {
            AccountManagerFuture<Bundle> future = manager.getAuthToken (account, "ah", false, null, null);
            Bundle bundle = future.getResult();
            return bundle.getString(AccountManager.KEY_AUTHTOKEN);
         } catch (OperationCanceledException e) {
                Log.w(TAG, e.getMessage());
         } catch (AuthenticatorException e) {
                Log.w(TAG, e.getMessage());
         } catch (IOException e) {
                Log.w(TAG, e.getMessage());
         }
         return null;
    }

    protected void onPostExecute(String authToken) {
        new GetCookieTask().execute(authToken);    
    }
}
Nick Orton