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.
With OpenID, your password is only given to your identity provider, and that provider then confirms your identity to the websites you visit. Other than your provider, no website ever sees your password, so you don't need to worry about an unscrupulous or insecure website compromising your identity.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With