Welcome back!

Forgot your password?

Master Facebook's OAuth process in Android

Here we'll show you how to go through the Facebook OAuth process, which lets any Facebook user log in to Facebook and grant your Android app access to their account. Our simple app simply logs users in and displays some info about their Facebook profile.

4Confirm that your project has <uses-permission android:name="android.permission.INTERNET"/> in its manifest.

5 Create a new Facebook app via the Facebook developer console using the Apps menu at the top of the page. Once you've created a new App, click the Settings tab on the left, select + Add Platform, and choose the Website option. Set up your Temboo callback URL by specifying the following URL as your Site URL:

https://ACCOUNT_NAME.temboolive.com/callback/

6Go to the example code you downloaded earlier, and in FacebookOAuthHelper.java, substitute in your Facebook app details over the default values shown below:

8 Deploy and run the app. After a few moments you should be redirected to Facebook where you can log in and grant the application access. Once you've done so you should be redirected back to your application where you'll see your user profile information in JSON format. That's it!

Taking a closer look at the code

This example includes an OAuth helper class for Facebook and a WebView to display the Facebook login and grant-access screens. The FacebookOAuthHelper class has a few components:

First, we generate a secure random state token and create a Temboo session object. Creating a state token can be handy in a couple ways:

When beginning the OAuth process, the InitializeOAuth Choreo will always return a CallbackID used to retrieve OAuth callback data after the user redirects back to your app's page. In some cases, it can be desirable to be able to predict this CallbackID by generating a state token and passing it as the CustomCallbackID in the InitializeOAuth Choreo and as part of the URL you provide for the ForwardingURL.

Your application can use the state token to finish the OAuth process and retrieve the user's access token.

After generating this token, we can execute the InitializeOAuth Choreo in the doInBackground method of the FacebookInitOAuthTask internal class. This is the point in the code where the user is redirected to Facebook in order to log in an grant the application access:

Once the user logs in and authorizes the application, Facebook attempts to redirect the user to the forwarding URL specified in the initialize OAuth flow. Our WebView watches for this redirect, and when it identifies our forwarding URL as the target it prevents the redirect and instead uses Temboo to finalize the OAuth process. This is
handled in our MainActivity class:

The FacebookOAuthHelper class's getUserInfo method in turn runs the FinalizeOAuth choreo. Upon success of this choreo we can retrieve the Facebook access token. This functionality occurs in the FacebookFinalizeOAuth internal class:

Kick off the OAuth process by adding new FacebookInitOAuthTask().execute(); to the end of your MainActivity's onCreate method.

We're all finished! This Android application executes the OAuth flow, and retrieves information about your app's user. We also have OAuth support for many other APIs in our Choreo Library.

Once you've got your code up and running, you're ready to move on and do more. From monitoring your running applications, to moving your generated Temboo code to your preferred development environment and sharing it with colleagues, collaborators and friends - we've got you covered.