Automatic GAE login from Android

57 views
Skip to first unread message

Faber Fedor

unread,
Feb 24, 2009, 9:39:50 AM2/24/09
to Google App Engine
I'm trying to get my Android app to login to my GAE app and download some data.  I have the name and password of the user stored in the Android app.  I can't find the docs that tell me the process for my Android app to authenticate with my GAE app using my Google Accounts.

Can someone show me where they are or tell me how to do what I want to do?

--

Faber Fedor
Linux New Jersey
http://linuxnj.com
faberfedor.blogspot.com

lenza

unread,
Feb 24, 2009, 11:54:16 AM2/24/09
to Google App Engine
Hi Faber,

Here is the information on how you generically log into Google
services and GAE app programatically:

http://code.google.com/apis/accounts/docs/AuthForInstalledApps.html
http://stackoverflow.com/questions/101742/how-do-you-access-an-authenticated-google-app-engine-service-from-a-non-web-pyt/499124

Here is the code I wrote for Android:

Log.d(TAG, "Num cookies before login: " + httpclient.getCookieStore
().getCookies().size());

//Setup Google.com login request parameters
List <NameValuePair> nvps = new ArrayList <NameValuePair>();
nvps.add(new BasicNameValuePair("Email", username));
nvps.add(new BasicNameValuePair("Passwd", password));
nvps.add(new BasicNameValuePair("service", "ah"));
nvps.add(new BasicNameValuePair("source", "YOUR-PROJECT-NAME")); //
used by google for accounting
nvps.add(new BasicNameValuePair("accountType", "GOOGLE")); //using
HOSTED here will do bad things for hosted accounts

//Login at Google.com
HttpPost httpost = new HttpPost("https://
www.google.com/accounts/ClientLogin");
httpost.setEntity(new UrlEncodedFormEntity(nvps, HTTP.UTF_8));
HttpResponse response = httpclient.execute(httpost);
Log.i(TAG, "Google.com Login Response: " + response.getStatusLine
());

//Find authkey in response body to pass to
Appspot.com
ByteArrayOutputStream ostream = new ByteArrayOutputStream();
response.getEntity().writeTo(ostream);
String strResponse = ostream.toString();
Log.v(TAG, strResponse);
StringTokenizer st = new StringTokenizer(strResponse, "\n\r=");
String authKey = null;
while(st.hasMoreTokens()) {
if(st.nextToken().equalsIgnoreCase("auth")) {
authKey = st.nextToken();
Log.d(TAG, "AUTH = " + authKey);
break;
}
}

//Do a GET with authkey to get cookie from
Appspot.com
HttpGet httpget = new HttpGet("YOURAPP.APPSPOT.COM /_ah/login?
auth=" + authKey + "&continue=" + URL_OF_PAGE_YOU_WANT);
response = httpclient.execute(httpget)
Log.i(TAG, "Appspot.com Login Response: " + response.getStatusLine
());
Log.d(TAG, "Num cookies after login: " + httpclient.getCookieStore
().getCookies().size());

Let me know if this works for you. I pulled it together from
different parts of my code so I might have messed something up. For
simplicity I left out the error checking.

Lenza
http://blog.lenza.org

Faber Fedor

unread,
Feb 24, 2009, 2:58:29 PM2/24/09
to google-a...@googlegroups.com
Thanks!  I just finished getting my Android app to download the "public" data on my GAE app so your code will come in very handy!

On Tue, Feb 24, 2009 at 11:54 AM, lenza <le...@lenza.org> wrote:

Hi Faber,

Here is the information on how you generically log into Google
services and GAE app programatically:
<snip>
Reply all
Reply to author
Forward
0 new messages