Authenticating Users

Problem

How do I log a user into my Platform app?

Solution

Users have to be logged into Facebook in order for you to make API calls on their behalf, so Facebook provides an automated authentication process that you can initiate by redirecting users to http://www.facebook.com/login.php?api_key=1234567890&v=1.0 (where 1234567890 is your app’s API key). This URL can accept a few parameters, as documented in the Discussion.

Discussion

The process works like Figure 9-1.

User authentication flow

Figure 9-1. User authentication flow

Let’s walk through the steps. Users start off by visiting a Canvas page in your app without logging into Facebook first, as shown in Figure 9-2.

Canvas page with login message

Figure 9-2. Canvas page with login message

You can insert a standard Facebook login button anywhere you’d like with the following code:

<a href="http://www.facebook.com/login.php?api_key=1234567890
&v=1.0"><img src="http://static.ak.facebook.com/images/devsite/
facebook_login.gif"></a>

where 1234567890 is your app’s API key (which isn’t the same as your app’s ID; the API key can be found in the Facebook Developers app and is usually about 30 characters long, made up of letters and numbers). When users click on that button, they’ll get bumped over to a Facebook login page with your app’s name in it, as in Figure 9-3.

Figure 9-3. Facebook ...

Get Facebook Cookbook now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.