share
Stack OverflowEasy way to authenticate POST requests from a Google Android client to Google App Engine?
[+9] [2] ehfeng
[2009-07-09 05:36:57]
[ android google-app-engine authentication ]
[ http://stackoverflow.com/questions/1101992/easy-way-to-authenticate-post-requests-from-a-google-android-client-to-google-ap ]

I'd like to be able to send a POST request from an Android app to App Engine and have it linked to the user's Google account. I read that you need to obtain an authentication token and send it with the POST request. Does Android provide a way to request this token? And how would GAE process it?

I feel like this should be easy and I'm missing something obvious.

Thanks!

[+9] [2009-07-09 12:52:02] Nick Johnson [ACCEPTED]

See my blog post [1] on how to authenticate with an App Engine app using credentials stored in the phone.

It is possible to authenticate users programmatically. In the Python SDK, the appengine_rpc module [2] performs this function. In a nutshell, the procedure is this:

  1. Use ClientLogin [3] to get a one-use authentication token given the user's username and password.
  2. Make a POST request to yourapp.appspot.com/_ah/login, with the arguments continue=http://localhost/&auth=authtoken (where authtoken is the one-use token you got from step 1).
  3. Intercept the 302 response returned and capture the returned Google cookie.
  4. Supply the cookie on all subsequent requests.

For excruciating detail, see the source of appengine_rpc.py, linked above.

[1] http://blog.notdot.net/2010/05/Authenticating-against-App-Engine-from-an-Android-app
[2] http://code.google.com/p/googleappengine/source/browse/trunk/python/google/appengine/tools/appengine_rpc.py
[3] http://code.google.com/apis/accounts/docs/AuthForInstalledApps.html

sweet! thanks so much nick. - ehfeng
I'm new to Android.. and with a quick look.. this should still do the trick after 2 years.. right?! - Lipis
1
[+2] [2010-04-05 04:59:48] Roman Nurik

As of Android 2.0, you can use AccountManager to request an auth token for accounts of type com.google. You can then authenticate the user to an App Engine app by hitting the url:

http://[yourapp].appspot.com/_ah/login?auth=[theauthtoken]

The cookies set in the response can be piggybacked onto future requests to your app to authenticate the user against your app.

In the absence of sample code that does exactly this, you can check out the Sample Sync Adapter [1] code (bundled with the SDK) for a general idea about requesting auth tokens.


EDIT: Just realized Nick wrote about the second part, but the AccountManager#getAuthToken bit is new as of Android 2.0.

[1] http://developer.android.com/resources/samples/SampleSyncAdapter/index.html

2