OAuth is an authentication method which allows the user to grant your application access to private information in a controlled way.
It is recommended to go over Google documentation:
Using OAuth 2.0 for Installed Applications - Authentication and Authorization for Google APIs - Google Code
In order to access private information you need to get an
access token.
There are several steps required (the attached code takes care for most of the steps):
- Register your application with Google. You will get "client id" and "client secret" values.
In order to run this example you will need to first register a new application and fill ClientId and ClientSecret variables. It takes about two minutes to register an application:
https://code.google.com/apis/console
- In your application you should show a WebView that handles the authentication.
(the user default language is used in this dialog)
The scope parameter should be set based on the required service.
After the user approves the access the WebView is redirected to a different page. The authorization code appears in the page title. We extract it using
WebViewExtender library. After extracting the code the WebView is removed.
- Another Http call is required to get the access token.
Once we have the access token we can send requests that access the user's private data.
In the attached example we retrieve the user Gmail contacts list.
OAuth 2.0 is a popular authentication method. It shouldn't be difficult to use this code with other web services such as Twitter, Facebook and others. It is much simpler than OAuth 1.0.
The following libraries are required: HTTP, JSON (to parse the access token request) and WebViewXtended (to get the authorization code from the WebView title).
The attached example also requires XmlSax for the contacts feed parsing.
You will need to register a new application with Google in order to run the example. Once registered you should fill CliendId and ClientSecret variables.
It takes two minutes to
register a new application.