Welcome back!

Forgot your password?

Master Facebook's OAuth process in PHP

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 app access to their account. Our simple app simply logs users in and displays some info about their Facebook profile.

In order to follow this example, you'll need a web host running PHP 5 (or later). Unlike our other examples, this one cannot be run from inside your IDE or from the command line, as the OAuth process depends on your script having a valid URL.

Run our Facebook OAuth Example

1 Log in to Temboo. If you don't already have an account, you can register for free.

2 Make sure you've downloaded the Temboo PHP SDK and that you've added it to your development environment as described in our getting started tutorial.

3 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/

4 Create a new PHP script and copy in the code below, making sure to substitute in your Temboo account details, as well as your new Facebook app's App ID and App Secret. You may also want to set TEMBOO_EXAMPLE_URI to the location where you'll upload this script to your web host.

5 Upload the entire project, including your newly created script and the php-sdk folder, to your web host.

6 Visit your script in a web browser. Click the Facebook Login link and go through the OAuth process.

7 Once you've been redirected to Facebook, you can log in and grant the application access. At this point in the example, you should be redirected back to your application where you'll see the user profile information in JSON format. That's it!

Taking a closer look at the code

This example includes a pair of OAuth helper functions for Facebook and a simple page template for initializing the OAuth flow and displaying user information.

First, we execute the InitializeOAuth Choreo. This is the point in the code where the user is redirected to Facebook in order to log in and grant the application access:

Note that we pass the script's own address in the ForwardingURL. When the InitializeOAuth Choreo completes, we store the CallbackID it returns in a PHP session for later use, then redirect the user to the AuthorizationUrl returned by Facebook.

The last step is to run the FinalizeOAuth Choreo and store the returned AccessToken so the Facebook > Reading > User Choreo can use it to retrieve your user's profile information. The important thing to note here is that the CallBackID we saved to the PHP session in the previous step is now passed back to the FinalizeOAuth Choreo:

What's Next?

We're all finished! This PHP 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.

Commit Code to GitHub

Commit Temboo-generated code directly to GitHub and share with the world. Learn more.

Need help?

We're always happy to help. Just email us at support@temboo.com, and we'll answer your questions.