Getting More Detailed with Permissions

Aside from Publicly Accessible Information, the Facebook users who visit your Web site have complete control over the information they give your Web site access to. To get a user's birthday or e-mail address, for example, you must explicitly prompt him for permission to access his birthday or e-mail address before you can access it.

You can prompt the user for this permission in several ways. Here's the simplest way to prompt the user for permission:

  1. Set up your init block. If you've already added the init JavaScript (see Chapter 7), you're halfway there. If not, do it.
  2. Add the perms key/value pair as an object at the end of your FB.login call. The FB.login call will both log in the user if she isn't already logged in and prompt her for the appropriate permissions if she is. To assign what permissions to get, just add a {perms} block (like the following) to the end of the call:
    FB.login(function(response) {
      if (response.session) {
        if (response.perms) {
          // user is logged in and granted some permissions.
          // perms is a comma separated list of granted permissions
        } else {
          // user is logged in, but did not grant any permissions
        }
      } else {
        // user is not logged in
      }
    }, {perms: 'email,user_birthday'});

    Adding the {perms} block to the end prompts the user for access to his birthday and e-mail address. After you add this code, when the user logs in through Facebook on your site, he is prompted with a message that looks like the one shown in Figure ...

Get Facebook® Application Development For Dummies® 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.