Tut (SDK 1.5) Get google account of user

928 views
Skip to first unread message

Sujay Krishna Suresh

unread,
Jun 5, 2009, 2:54:26 AM6/5/09
to android-d...@googlegroups.com
hi guys i finally got a simple method to get the google accounts in SDK 1.5...
i'm attaching the code for everyone who needs this piece of code...
This piece of code prints all methods in the class & invokes the static method getAccount...
try {
  for (Method ele : Class.forName("com.google.android.googlelogin.GoogleLoginServiceHelper").getMethods()) {
  System.out.println(ele.toString());
  try {
  if(ele.getName().equals("getAccount"))
  ele.invoke(null, this, 123, true);
  } catch (IllegalArgumentException e) {
  e.printStackTrace();
  } catch (IllegalAccessException e) {
  e.printStackTrace();
  } catch (InvocationTargetException e) {
  e.printStackTrace();
  }
  }
  } catch (ClassNotFoundException e) {
  e.printStackTrace();
  }

  } catch (ClassNotFoundException e) {
  e.printStackTrace();
  }

& jus place the foll code in ur onactivityresult
if (requestCode == 123) {
  System.out.println(resultCode);
  String key = "accounts";
  System.out.println(key + ":" + Arrays.toString(data.getExtras().getStringArray(key)));
  String accounts[] = data.getExtras().getStringArray(key);
  if (accounts[0] != null)
  usernameText.setText("You are : " + accounts[0].replace("@gmail.com", ""));
 }
in the logcat/ddms o/p u'll be able to see that there are other useful methods...
public static void com.google.android.googlelogin.GoogleLoginServiceHelper.getCredentials(android.app.Activity,int,android.os.Bundle,java.lang.String,java.lang.String,boolean)
public static void com.google.android.googlelogin.GoogleLoginServiceHelper.getCredentials(android.app.Activity,int,android.os.Bundle,boolean,java.lang.String,boolean)
public static void com.google.android.googlelogin.GoogleLoginServiceHelper.invalidateAuthToken(android.app.Activity,int,java.lang.String)


--
Regards,
Sujay
Mark Twain  - "There is no sadder sight than a young pessimist."

mscwd01

unread,
Jun 5, 2009, 7:57:01 AM6/5/09
to Android Developers
Does this code allow you to get the email address of the phones owner,
used when setting up the phone? I.e. the email address of the google
account tied to to the phone?

On Jun 5, 7:54 am, Sujay Krishna Suresh <sujay.coold...@gmail.com>
wrote:
> Mark Twain <http://www.brainyquote.com/quotes/authors/m/mark_twain.html>  -

Sujay Krishna Suresh

unread,
Jun 5, 2009, 8:06:10 AM6/5/09
to android-d...@googlegroups.com
to be more specific it gives the interface that is used by other google apps to get user credentials...
it also provides methods with which u can get the auth token or invalidate the token(logout)...
--
Regards,
Sujay
Will Rogers  - "I never expected to see the day when girls would get sunburned in the places they now do."

mscwd01

unread,
Jun 5, 2009, 9:42:29 AM6/5/09
to Android Developers
Hmm, while this could be useful isn't it a major privacy issue?
Imagine unscrupilous developers getting the auth token and then
logging into the users gmail account etc?

Or have I misunderstood something here?

On Jun 5, 1:06 pm, Sujay Krishna Suresh <sujay.coold...@gmail.com>
wrote:
> Will Rogers <http://www.brainyquote.com/quotes/authors/w/will_rogers.html>

Sujay Krishna Suresh

unread,
Jun 5, 2009, 9:56:09 AM6/5/09
to android-d...@googlegroups.com
u need certain permissions for this which tells the user that whether the app only reads the email id
or if it trys to get the auth token... so the user'll no at the time of installing the app itself...
Mark Twain  - "There is no sadder sight than a young pessimist."

ubikdroid

unread,
Jun 5, 2009, 4:54:07 PM6/5/09
to Android Developers
I tried to use the auth token as a SID to login to
http://www.google.com/reader/api/0/unread-count from a widget and it
didn't work. I had to resort to using the ClientLogin API to get a
valid SID.

Sujay Krishna Suresh

unread,
Jun 9, 2009, 1:08:05 AM6/9/09
to android-d...@googlegroups.com
this tut is actually to help u get those credentials without having to worry abt anything...
if u hv enough time to explore then there's definitely a very simple & more efficient time using this...
rather than forming ur own http requests...
--
Regards,
Sujay
Josh Billings  - "Every man has his follies - and often they are the most interesting thing he has got."

André Charles Legendre

unread,
Jun 9, 2009, 1:11:58 AM6/9/09
to android-d...@googlegroups.com
Hi Sujay
What tut are you talking about ?

Sujay Krishna Suresh

unread,
Jun 9, 2009, 1:15:46 AM6/9/09
to android-d...@googlegroups.com
I started this discussion to provide a tut for ppl interacting with google services frm their app...
plz chk out my 1st post....

On Tue, Jun 9, 2009 at 10:41 AM, André Charles Legendre <andre.l...@gmail.com> wrote:

Hi Sujay
What tut are you talking about ?





--
Regards,
Sujay
Dave Barry  - "Camping is nature's way of promoting the motel business."

ubikdroid

unread,
Jun 9, 2009, 4:10:25 AM6/9/09
to Android Developers
Tell me more! I'm not sure what you mean by "very simple & more
efficient time using this". I'm trying to create a widget that gets
the number of unread items from the Google Reader API. The auth token
from android doesn't seem to be valid for this. I'm having to resort
to asking the user to input their password (your post helped me get
the username) before obtaining a SID from the Google ClientLogin API
which isn't ideal.
Any help much appreciated.
Mark

On Jun 9, 6:08 am, Sujay Krishna Suresh <sujay.coold...@gmail.com>
wrote:
> this tut is actually to help u get those credentials without having to worry
> abt anything...
> if u hv enough time to explore then there's definitely a very simple & more
> efficient time using this...
> rather than forming ur own http requests...
>
> --
> Regards,
> Sujay
> Josh Billings<http://www.brainyquote.com/quotes/authors/j/josh_billings.html>

Sujay Krishna Suresh

unread,
Jun 9, 2009, 4:18:49 AM6/9/09
to android-d...@googlegroups.com
as i already said u need to explore the getcredentials method...
in the params of that method u need to specfy wat u need n for which service... 
if this is done then u can get the sid with ease...
Josh Billings  - "Every man has his follies - and often they are the most interesting thing he has got."

ubikdroid

unread,
Jun 9, 2009, 7:58:15 AM6/9/09
to Android Developers
Woot! I got it working. I tried before with the service set to
"reader" but I got a SecurityException thrown even though I had the
permission in the manifest.

However, if you leave the service null in the getCredentials call and
add the permission
com.google.android.googleapps.permission.GOOGLE_AUTH.OTHER_SERVICES to
the manifest then it works!

Thanks for the help Sujay. I was working at this for a long time!


On Jun 9, 9:18 am, Sujay Krishna Suresh <sujay.coold...@gmail.com>
wrote:
> as i already said u need to explore the getcredentials method...
> in the params of that method u need to specfy wat u need n for which
> service...
> if this is done then u can get the sid with ease...
>
>

Agung Priyono

unread,
Jun 9, 2009, 4:21:24 AM6/9/09
to android-d...@googlegroups.com
Dear Android,
 
Thank for your info.

 

kostmo

unread,
Jul 15, 2009, 6:10:38 PM7/15/09
to Android Developers
Hi ubikdroid,
Would you be able to tell me more about how you used getCredentials?

I'm trying this variation: getCredentials
(android.app.Activity,int,android.os.Bundle, java.lang.String,
java.lang.String, boolean)

Called from my activity, it is
getCredentials(this, 345, null, email, null, false);

Where 'email' is obtained from getAccount beforehand. I have added
com.google.android.googleapps.permission.GOOGLE_AUTH.OTHER_SERVICES to
the manifest. As opposed to the getAccount method, which returned an
array containing the email address, the getCredentials method called
with the above arguments returns null. With what arguments are you
calling getCredentials?

The only docs I can find relating to this (http://
www.androidjavadoc.com/m5-rc15/com/google/android/googleapps/GoogleLoginServiceHelper.html)
seem to be outdated (wrong number of arguments).

Thanks,
Karl

On Jun 9, 6:58 am, ubikdroid <markst3v...@googlemail.com> wrote:
> Woot! I got it working. I tried before with the service set to
> "reader" but I got a SecurityException thrown even though I had the
> permission in the manifest.
>
> However, if you leave the service null in the getCredentials call and
> add the permission
> com.google.android.googleapps.permission.GOOGLE_AUTH.OTHER_SERVICES to
> the manifest then it works!
>
> Thanks for the help Sujay. I was working at this for a long time!
>
> On Jun 9, 9:18 am, Sujay Krishna Suresh <sujay.coold...@gmail.com>
> wrote:
>
> > as i already said u need to explore the getcredentials method...
> > in the params of that method u need to specfy wat u need n for which
> > service...
> > if this is done then u cangetthe sid with ease...

Micah

unread,
Jul 22, 2009, 10:21:20 AM7/22/09
to Android Developers
Bump. Star. Tag. Etc.

While I imagine this will eventually be "fixed" in a future release we
can only hope that Google gives developers some alternative (ie:
better) way to get a UID for a given user so we can save profiles,
etc. for our apps online and keep them associated with a user without
them having to login separately to our application (which is a much
bigger security problem in my eyes). Even better would be if there
was a secure, reliable and supported way for an application to get
authenticated tokens so our application could interact with their
Google data (ie: get inbox count, save to Google Docs, connect to
Jabber network, etc.)

On Jun 4, 11:54 pm, Sujay Krishna Suresh <sujay.coold...@gmail.com>
wrote:
> Mark Twain <http://www.brainyquote.com/quotes/authors/m/mark_twain.html>  -

ubikdroid

unread,
Jul 25, 2009, 6:26:14 PM7/25/09
to Android Developers
Hi Karl,
I call getCredentials in my Activity like this:

GoogleLoginServiceHelper.getCredentials(this, 54321,
null,
GoogleLoginServiceConstants.REQUIRE_GOOGLE, //
require google
null, // This means we get the credentials for
GOOGLE_AUTH.OTHER_SERVICES
false);

then override onActivityResult:

@Override
protected void onActivityResult(int requestCode, int resultCode,
Intent intent){
super.onActivityResult(requestCode, resultCode, intent);
if(requestCode == 54321){
authToken = intent.getStringExtra
(GoogleLoginServiceConstants.AUTHTOKEN_KEY);
}
}

Hope that helps.


On Jul 15, 11:10 pm, kostmo <kos...@gmail.com> wrote:
> Hi ubikdroid,
> Would you be able to tell me more about how you used getCredentials?
>
> I'm trying this variation: getCredentials
> (android.app.Activity,int,android.os.Bundle, java.lang.String,
> java.lang.String, boolean)
>
> Called from my activity, it is
> getCredentials(this, 345, null, email, null, false);
>
> Where 'email' is obtained from getAccount beforehand.  I have added
> com.google.android.googleapps.permission.GOOGLE_AUTH.OTHER_SERVICES to
> the manifest.  As opposed to the getAccount method, which returned an
> array containing the email address, the getCredentials method called
> with the above arguments returns null.  With what arguments are you
> calling getCredentials?
>
> The only docs I can find relating to this (http://www.androidjavadoc.com/m5-rc15/com/google/android/googleapps/GoogleLo...)

Sergio

unread,
Jul 31, 2009, 5:19:28 AM7/31/09
to Android Developers
Hi there,
Does this work in the emulator?
I am always getting null in the intent data
Cheers
Sergio
> > The only docs I can find relating to this (http://www.androidjavadoc.com/m5-rc15/com/google/android/googleapps/G......)

Sergio

unread,
Jul 31, 2009, 5:22:27 AM7/31/09
to Android Developers
My intent seems to be empty for an errorcode key with a value of 0.
Cheers

On Jul 26, 10:26 am, ubikdroid <markst3v...@googlemail.com> wrote:
> > The only docs I can find relating to this (http://www.androidjavadoc.com/m5-rc15/com/google/android/googleapps/G......)

Sujay Krishna Suresh

unread,
Aug 1, 2009, 3:29:36 AM8/1/09
to android-d...@googlegroups.com

Sergio, i dont think this'd work on an emulator...
U need to have configured atleast once into any of google's services before u r able to get any results.
--
Regards,
Sujay
Mike Ditka  - "If God had wanted man to play soccer, he wouldn't have given us arms."

Sergio

unread,
Aug 1, 2009, 6:17:28 AM8/1/09
to Android Developers
Hi Sujay,
Thanks for your reply.
I configured email in the emulator and it was working properly, I
could see my emails, so I thought that the GoogleLoginServiceHelper
would be able to work.
Cheers
Sergio

On Aug 1, 7:29 pm, Sujay Krishna Suresh <sujay.coold...@gmail.com>
wrote:
> Mike Ditka <http://www.brainyquote.com/quotes/authors/m/mike_ditka.html>  -

JP

unread,
Aug 1, 2009, 11:37:29 AM8/1/09
to Android Developers


Thanks for all the code - I've been trying to toy around with this but
I was not able to locate the right imports or a library that includes
com.google.android.googleapps. Can anybody point me where I can find
that part?
> > The only docs I can find relating to this (http://www.androidjavadoc.com/m5-rc15/com/google/android/googleapps/G......)

Sergio

unread,
Aug 2, 2009, 7:32:19 AM8/2/09
to Android Developers

JP

unread,
Aug 2, 2009, 11:59:50 AM8/2/09
to Android Developers
Thanks Sergio - I did some more poking around and there seems to be a
new version, here:
http://github.com/android/platform_frameworks_opt_com.google.android/tree/master

It's got quite a yikes-factor however - from far it appears a
candidate for breakage when a new version of Android is coming out.
Putting everyone in the not-supposed-to-used-undocumented-features-and-
we-told-you-so corner, no?


On Aug 2, 4:32 am, Sergio <cur...@gmail.com> wrote:
> You need to download framework.jar from:http://github.com/android/platform_frameworks_opt_com.google.android/...

Sergio

unread,
Aug 2, 2009, 6:04:10 PM8/2/09
to Android Developers
Absolutely, however I hope the next version will have some kind of
public API...

On Aug 3, 3:59 am, JP <Joachim.Pfeif...@gmail.com> wrote:
> Thanks Sergio - I did some more poking around and there seems to be a
> new version, here:http://github.com/android/platform_frameworks_opt_com.google.android/...

JP

unread,
Aug 2, 2009, 8:11:10 PM8/2/09
to Android Developers

Looks our needs are ahead of what's available. I also checked Open
Authentication but there isn't much at this point either.

kostmo

unread,
Sep 10, 2009, 2:24:01 AM9/10/09
to Android Developers
I have been investigating whether GoogleLoginService can substitute
for ClientLogin (see http://code.google.com/apis/accounts/docs/AuthForInstalledApps.html)
on Google App Engine.

To use ClientLogin, I solicit the username and password from the user
in a dialog and exchange them on the Google server (https://
www.google.com/accounts/ClientLogin) for a 203-character string called
"Auth". This "Auth" string can then be exchanged for a cookie on the
application server (http://myapp.appspot.com/_ah/login?auth=...). The
"Value" of this obtained cookie is a 460-character string. The cookie
can be used from then on to maintain the user session.

I was able to obtain the "authtoken", aka "SID", using the
GoogleLoginService method described in this thread. The "authtoken"
obtained through this method also happened to be 203 characters.

I was hoping that the "authtoken" might be substituted for the "Auth"
string in the ClientLogin process, thus bypassing the need to solicit
the username/password. Sadly, this appears not to be the case.
ClientLogin returns a "500" Server Error rather than the "204" I
usually get with a good login.

So unless anyone out there is aware to the contrary, my conclusion is
that you must prompt the user for their password if you want an
authenticated App Engine session.

Charlie

unread,
Oct 11, 2009, 1:29:18 PM10/11/09
to Android Developers
Hi,

I've managed to get this to work for Google App Engine by specifying
"ah" for the service parameter, for example :

GoogleLoginServiceHelper.getCredentials(this,
54321,
null,
GoogleLoginServiceConstants.REQUIRE_GOOGLE,
"ah",
false);

This also requires permission:

com.google.android.googleapps.permission.GOOGLE_AUTH.ah

I don't like having to use undocumented API's, but I suppose it's
better than the alternative of having the user to enter their username
and password.


Charlie

On 10 Sep, 07:24, kostmo <kos...@gmal.com>> wrote:
been investigating whether GoogleLoginService can subsan substitute
> fo
tLogin (seehttp://code//code.goog/apis/accounts/docs/
AuthForInstalledApps.html)
> on Google App Engine.
>
> To use ClientLogin, I solicit the usehe usernameassword from theom the u
in a dialog and exchange them on the Google server (https://
www.google.com/accounts/ClientLogin) for a 203-character string called
> "Auth".  This "Auth" string can can be exchanxchafor a cookie on the
> application server (http://myapp.appspot.com/_ah/login?auth=...).  The
> "Value" of this obtained cookie is a 460-character string.  The cookie
> can be used from then on to maintain the user session.
>
> I was able to obtain the "authtoken", aka "SID", using the
> Ge
> GooglnService method described ibed in this threade "authtoken"
> obtained through this meis method happened to be 203 characters.
>
> I was hoping that the "authtoken" might be substituted for the the "Aut
string in the ClientLogin procerocethus bypassing the the need toicit
> the username/password.  Sadly, this appears nrs not t the case.
> ClientLogin returns a "500" Server Error ror rather tthe "204" I
> usually get with a good login.
>
> So unless anyone out there is ais awto the contrary, my conclusion is

kostmo

unread,
Oct 12, 2009, 2:13:30 AM10/12/09
to Android Developers
It seems tantalizingly close, but I still can't get the
GoogleLoginService method to work with Google App Engine (GAE).

I'll start with a review of the method that currently works for me.
Let's say that "<myapp>" is my GAE application name and "<authstring>"
is the 203-character string I get from ClientLogin.

After using the user/pass prompt and traditional ClientLogin method, I
can examine the cookie retrieved via curl:
curl -c cookies.txt http://<myapp>.appspot.com/_ah/login?auth=<authstring>
This command dumps HTML to the terminal and saves the cookie to
"cookies.txt". The HTML sates that the status code is "302",
but in actuality the HTTP status code is "204". Anyway, these are
characteristics of the cookie exchange working *properly*.
The URL can be used repeatedly to get the cookie (although I don't
know how long it stays valid).

Now let me describe how I attempt to accomplish the same with
GoogleLoginServiceHelper. I am using the method described originally
by Sujay where he iterates through the methods in the
GoogleLoginServiceHelper class. I look for the the "getCredentials"
method that has the correct name and argument types and call it as
follows:
m.invoke(
null, // This extra argument pertains to the "invoke" method.
this,
12345, // onActivityResult code
null,
true, // value of the GoogleLoginServiceConstants.REQUIRE_GOOGLE
constant
"ah", // This is the service parameter that supposedly works with GAE
false);

In my Manifest file, I have
<uses-permission
android:name="com.google.android.googleapps.permission.GOOGLE_AUTH"></
uses-permission>
in addition to
<uses-permission
android:name="com.google.android.googleapps.permission.GOOGLE_AUTH.ah"></
uses-permission>
.

The intent passed to onActivityResult has 3 string fields:
"callerExtras", "authtoken" (the value of the
GoogleLoginServiceConstants.AUTHTOKEN_KEY constant), and
"authAccount".
I encounter the same response as before, however: when substituting
the "authtoken" value for <authstring> in the
above curl command, the result is 500 Server error.

I am determined to get this to work! Is there possibly some
configuration I'm missing on the App Engine side?

Karl

kostmo

unread,
Oct 12, 2009, 2:27:20 PM10/12/09
to Android Developers
It seems I was the victim of an edge case. I had not done a factory
reset of my ADP1 since Android 1.1. It works now! :)

Karl

Mark Lowne

unread,
Nov 17, 2009, 4:04:42 PM11/17/09
to Android Developers
Very interesting discussion! Thanks everybody for the precious info.

Did anyone manage to get BlockingHelper to work? (or, to put it
differently: is there any way to do this without the need for an
activity that receives the result?)

Ozius

unread,
Dec 17, 2009, 8:21:22 AM12/17/09
to Android Developers
Somebody try it with Google Analytics service?

I am trying to implement this method in my program but I can't get it
to work. This is my code:

GoogleLoginServiceHelper.getCredentials(this, 54321,
null,
GoogleLoginServiceConstants.REQUIRE_GOOGLE,
"analytics",
false);

I have this permissions in my manifest:

<uses-permission
android:name="com.google.android.googleapps.permission.GOOGLE_AUTH.analytics"></
uses-permission>


<uses-permission
android:name="com.google.android.googleapps.permission.GOOGLE_AUTH"></
uses-permission>

But I got a SecurityException:
java.lang.SecurityException: caller pid 6213 uid 10044 lacks
com.google.android.googleapps.permission.GOOGLE_AUTH.analytics

I have added this permission to manifest... any idea?

With other services like Calendar Data API (cl) and YouTube Data API
(youtube) i don't have any problems.

JP

unread,
Dec 18, 2009, 1:18:53 AM12/18/09
to Android Developers

I've implemented this method to access user accounts. Sorry no answer
to your q:, but you might interested to hear how I fared with this in
the wild. There's two major issues:
- Your app will not load on Android 2.0; the JVM will choke and
complain
- As far as Android 1.5 and 1.6 are concerned. Some carriers have
modified this particular area of Android and their custom ROMs are
incompatible with straight Android. This is an issue with Sprint (US),
or the HTC Hero on a carrier in Portugal (not sure which one). I am
sure, more. As an example, I've got the following (redacted) log from
Portugal:
11-19 11:13:19.951 W/dalvikvm( 4795): Verifier rejected class
Lcom/---------/-----------/MyApp;
11-19 11:13:19.951 W/dalvikvm( 4795): Class init failed in newInstance
call (Lcom/---------/-----------/MyApp;)
11-19 11:13:19.951 D/AndroidRuntime( 4795): Shutting down VM
11-19 11:13:19.951 W/dalvikvm( 4795): threadid=3: thread exiting with
uncaught exception (group=0x40013140)
11-19 11:13:19.951 E/AndroidRuntime( 4795): Uncaught handler: thread
main exiting due to uncaught exception
11-19 11:13:19.951 E/AndroidRuntime( 4795): Uncaught handler: thread
main exiting due to uncaught exception
11-19 11:13:19.961 E/AndroidRuntime( 4795): java.lang.VerifyError:
com.---------.-----------.MyApp
11-19 11:13:19.961 E/AndroidRuntime( 4795): at
java.lang.Class.newInstanceImpl(Native Method)
11-19 11:13:19.961 E/AndroidRuntime( 4795): at
java.lang.Class.newInstance(Class.java:1472)
Again, your app won't even load.

My recommendation: Don't bother to implement this, unless you're
building a one off for research or for your personal enjoyment.

Reply all
Reply to author
Forward
0 new messages