Android Adds Google Play Services and OAuth Identity Tools

One of the reason I like Android so much is the amount of development that is continually done by Google to the OS to make it better and easier to use for both the user and for the developer.  There is a recent update to Google Play Services that is being pushed out to devices that are 2.2 and up which includes a lot of cool features.  One of which is the OAuth Identity Tools.  The OAuth identity tools make it easy for developers to use OAuth 2.0 in their apps alongside the user’s Google account.  Not only does it streamline writing code to handle OAuth but it provides a consistent experience to the user instead of an app specific experience as it was before these tools.

Since these services and tools are just now rolling out, if you are going to develop for them it would be a good idea to make sure that the phone has the required software to use these tools.  This can be done easily by calling the method isGooglePlayServicesAvailable(Context context).  The result codes from this method are in the ConnectionResult object.

Once you know that Play Services are available you can use the nice little method AccountPicker.newChooseAccountIntent() which will return you an Intent.  You can use that Intent to use the system account picker.  The user will be familiar with it and it will feel more secure.

To get the Google authentication tokens from here all that is needed is to call GoogleAuthUtil.getToken().  This method takes three arguments, context, email address, and scope.  Scope is is provided by each Google API and you can pass in multiple scopes for each service you need to receive a token for.  One thing to note is that this method call is not synchronous.  That being said, you should not call getToken() in the UI thread.  It should either be called in another application thread or a service running in a different thread.

Below is some sample code which shows how you could typically make a callout to get the token:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
try {
    // if this returns, the OAuth framework thinks the token should be usable
    String token = GoogleAuthUtil.getToken(this, mRequest.email(), 
                       mRequest.scope());
    response = doGet(token, this);
} catch (UserRecoverableAuthException userAuthEx) {
    // This means that the app hasn't been authorized by the user for access
    // to the scope, so we're going to have to fire off the (provided) Intent
    // to arrange for that. But we only want to do this once. Multiple
    // attempts probably mean the user said no.
    if (!mSecondTry) {
        startActivityForResult(userAuthEx.getIntent(), REQUEST_CODE);
        response = null;
    } else {
        response = new Response(-1, null, "Multiple approval attempts");
    }
catch (IOException ioEx) {
    // Something is stressed out; the auth servers are by definition
    //  high-traffic and you can't count on 100% success. But it would be
    //  bad to retry instantly, so back off
    if (backoff.shouldRetry()) {
        backoff.backoff();
        response = authenticateAndGo(backoff);
    } else {
        response =
            new Response(-1, null, "No response from authorization server.");
    }
catch (GoogleAuthException fatalAuthEx)  {
    Log.d(TAG, "Fatal Authorization Exception");
    response = new Response(-1, null, "Fatal authorization exception: " +
                       fatalAuthEx.getLocalizedMessage());
}

Like most all Google API’s you need to register your app using your keystore in order to use the API’s.  The new OAuth Identity Tools should really streamline how developers and users consume Google Services.  If you would like more information please visit the Android Developers Blog as well as sample code included in the developer documentation.

This entry was posted in Mobile, Technology and tagged , , , , , , , . Bookmark the permalink.

Leave a Reply

Your email address will not be published. Required fields are marked *

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>