10

I am not a programmer, but I need to do this myself. I need some help.

I have been looking for the solution for the last two days and I cannot find any.

Ok. I am writing Android Native App. My first goal is to achieve possibility of login through Google Account (which is already set on the phone).

So I am using AccountManager to get the "com.google" account, I am getting an auth token this way:

Account[] mAccounts = mAccountManager.getAccountsByType("com.google"); 
AccountManagerFuture<Bundle> response = 
    mAccountManager.getAuthToken(mAccounts[0], "android", null, this, null, null);

Bundle authTokenBundle;
String authToken;

try {
    authTokenBundle = response.getResult();
    authToken = authTokenBundle.getString(AccountManager.KEY_AUTHTOKEN).toString();
} catch (OperationCanceledException e) {
    Log.e(TAG, e.getMessage());
} catch (AuthenticatorException e) {
    Log.e(TAG, e.getMessage());
} catch (IOException e) {
    Log.e(TAG, e.getMessage());
}

And my question is - what should be my next step? How I can go further with this authentication process? How should I use this token?

I have found some resources, but most of them are using OAuth or are web-based. I only need to authenticate and (if it is possible) get the name of the user (I already have the e-mail address), I don't need to access any Google services.

Thank You in advance.

TheJohnny
  • 370
  • 1
  • 5
  • 17

1 Answers1

8

Actually, OAuth 2 is what you want, rather than OpenID -- OpenID is inherently web-based, so you'd need to jump through some hoops with WebView or the browser. OAuth 2 allows you to use the token from AccountManager with Google APIs right from the app.

In your call to getAuthToken(), the authTokenType parameter is the OAuth 2 scope, which you want to be userinfo.profile and userinfo.email to authenticate the email address (you already have it, but you haven't verified it; it could in theory be spoofed) and to get the name of the user.

Here's what I use for the full scope in a similar situation:

private static final String OAUTH2_SCOPE =
    "oauth2:" +
    "https://www.googleapis.com/auth/userinfo.profile" +
    " " +
    "https://www.googleapis.com/auth/userinfo.email";

Of course, you could just use the whole string literal inline, but I prefer to build it up and be clear, and it makes it easier to change later if necessary.

In my case, I use getAuthTokenByFeatures(), something like this:

am.getAuthTokenByFeatures("com.google", OAUTH2_SCOPE, null, this, null, null,
                          new AccountManagerCallback<Bundle>()
{
    public void run(AccountManagerFuture<Bundle> future) {
        try {
            Bundle bundle = future.getResult();
            System.out.println("Got Bundle:\n" +
                               " act name: " +
                               bundle.getString(AccountManager.KEY_ACCOUNT_NAME) +
                               "\n act type: " +
                               bundle.getString(AccountManager.KEY_ACCOUNT_TYPE) +
                               "\n auth token: " +
                               bundle.getString(AccountManager.KEY_AUTHTOKEN));
        } catch (Exception e) {
            System.out.println("getAuthTokenByFeatures() cancelled or failed:");
            e.printStackTrace();
        }
    }
}, null);

but you can apply the same idea to your code. You can then use the OAuth token with Google User Info API, as described in Using OAuth 2.0 for Login to verify the email and get the user's name.

Darshan Rivka Whittle
  • 32,989
  • 7
  • 91
  • 109
  • Thank You very much for this answer. I will sure try Your solution. It was some time ago and I think I managed to do it somehow, but I will try this approach now. It seems easy. – TheJohnny Jun 06 '12 at 16:00
  • what you have tried @TheJohnny , bocoz I am too facing the same problem for my application also ......... – Vipin Sahu Sep 26 '12 at 07:36
  • @Darshan Computing I have seen getting auth access using the browser. ie the user can use any account other than those signed into in his phone.Just like in color note app etc. How can i achieve this ? – Ajith M A Jul 19 '13 at 12:14
  • @AjithMemana That's outside the scope of this question. You should see if there's already a question about it or a tutorial online somewhere. If you can't figure it out and there isn't already a question, you can create a new one. – Darshan Rivka Whittle Jul 19 '13 at 20:58