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:
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.