Implementing OAuth 2 on Android follows the exact flow as is on web. Taking Github as an example provider, the OAuth [web] flow is this -
Redirect users to GitHub for authentication
The user clicks a button (say Login) on your website and you redirect him to https://github.com/login/oauth/authorize?client_id={id}&scope={scope}. The page shows a login form.
GitHub redirects back to your callback URL with a “code” URL parameter
Once he logs in with the form above and allows your app, Github redirects to your callback URL. The callback URL is a URL you have configured with Github in your application settings. You can also pass one via the redirect_uri parameter to the authorization URL in 1 like this https://github.com/login/oauth/authorize?client_id={id}&scope={scope}&redirect_uri=http://example.com/github. Github then redirects back to http://example.com/github?code=1234
You post the “code” to Github in exchange for the access token
Using cURL or similar library, you POST the code to https://github.com/login/oauth/access_token. The response will be something like this access_token=e72e16&scope=user%2Cgist&token_type=bearer. Extract the access_token parameter.
Access the API with the access token
(You can find the Github’s OAuth documentation here developer.github.com/v3/oauth/. This flow is basically the same with all OAuth 2 providers.)
While this is a supposed web flow, it is not a hard thing to replicate in an Android app. Android has a WebView class that allows you embed your own browser and interact with web pages. That solves 1 easily. So within our app, we can embed a webview that loads Github’s login page for the user to authenticate himself.
However, since this is a mobile application and not a web application, the callback URL in step 2 does us no good. Remember, what we need is the code parameter. So we want to be able to intercept the code parameter from the webview as Github tries to redirect back to the callback URL. Interestingly, there are a couple of ways to do that in the WebView class. But first, we have to let the WebView know we want to receive notifications and requests related to the view via a WebViewClient. So we set a WebViewClient and overwrite the onPageStarted method. onPageStarted is called when a page starts loading. We can use this to know when the callback URL is loading and intercept the code parameter.
Now we have our code, step 3, send it to Github in exchange for an access token.
As you can see, getting the access_token wasn’t difficult. Now we just save it and use it to make subsequent requests to the API.
OAuth 1 on the other hand is more challenging. The flow is more complex and involves lots of crypto and signing kungfu. Your best bet is to find a library that has done the heavy lifting for you. And there are. I hope to write about that in the next piece.
Looking for a simple marketing automation tool to automate your customer onboarding, retention and lifecycle emails? Check out Engage and signup for free.
My name is Opeyemi Obembe. I build things for web and mobile and write about my experiments. Follow me on Twitter—@kehers.