Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android: using Google sign in to get access token

After reading the last Google + news at here and this. How do I get access token after I complete the sign in?

like image 825
Ilya Gazman Avatar asked Feb 26 '13 20:02

Ilya Gazman


People also ask

How can I get access token login?

To get the Client Access Token for an app, do the following: Sign into your developer account. On the Apps page, select an app to open the dashboard for that app. On the Dashboard, navigate to Settings > Advanced > Security > Client token.

Where are Android access tokens stored?

Android KeyStore should be used for long term storage and retrieval of cryptographic keys which will be used to encrypt our tokens in order to store them in e.g. SharedPreferences or a database. The keys are not stored within an application's process, so they are harder to be compromised.


2 Answers

To answer doubts about oauth scope (just to be useful for googlers):

To fully understand, Google-it some about authentication and authorization concepts.

Check if user/password exists is about authentication part.

Scope is required to authorization part: what you are authorized to do or receive in behalf of user. To get a list of scopes allowed, check the OAuth service documentation.

From Google and G+, most common scopes can be found on: https://developers.google.com/+/api/oauth?hl=pt-ZA

For example, to get all possible information from user, you can use the scope:

"openid profile email https://www.googleapis.com/auth/plus.login https://www.googleapis.com/auth/plus.me"

(the first word refer to protocol, followed by words that ask for fields on response, and desired scopes can be declared toghether with a space separator)

Note: Later, if you try use your access token to request or do anything that you don't asked before with a scope, the service can return an authorization error.

For Google, a good tool you can use to learn about his OAuth service and scope is the OAuth Playground: https://developers.google.com/oauthplayground/

like image 78
Renascienza Avatar answered Sep 27 '22 18:09

Renascienza


Did you have a look at the API reference?

The class you are probably looking for is com.google.android.gms.auth.GoogleAuthUtil.

It provides, amongst others, the following method:
static String getToken(Context context, String accountName, String

Description:
Authenticates the user and returns a valid Google authentication token, or throws an exception if there was an error getting a token.

Usage:

String token;
try {
    token = GoogleAuthUtil.getToken(context, accountName, scope);
} catch (GooglePlayServicesAvailabilityException playEx) {
    Dialog dialog = GooglePlayServicesUtil.getErrorDialog(
        playEx.getConnectionStatusCode(),
        Activity.this,
        AUTH_REQUEST_CODE);
    // Use the dialog to present to the user.
} catch (UserRecoverableAutException recoverableException) {
    Intent recoveryIntent = recoverableException.getIntent();
    // Use the intent in a custom dialog or just startActivityForResult.
} catch (GoogleAuthException authEx) {
    // This is likely unrecoverable.
    Log.e(TAG, "Unrecoverable authentication exception: " + authEx.getMesssage(), authEx);
} catch (IOException ioEx) {
    Log.i(TAG, "transient error encountered: " + ioEx.getMessage());
    doExponentialBackoff();
}
like image 31
jenzz Avatar answered Sep 27 '22 18:09

jenzz