FacebookGraphHandler

FacebookGraphHandler offers a proxy for the Facebook Graph API. Here is an example:

url:
  facebook:
    pattern: /facebook/(.*)
    handler: FacebookGraphHandler
    kwargs:
      # Visit https://developers.facebook.com/apps/ to get these keys
      key: ... # App ID
      secret: ... # App Secret
    redirect:
      header: Referer
      url: /$YAMLURL/

Follow the steps for Facebook auth to get the keys above.

Now, follow these steps:

  1. Click on /facebook/me
  2. The first time you click on this, you get an “access token missing” error
  3. So visit /facebook/ to log into Facebook
  4. Now re-visit /facebook/me
  5. This shows the logged-in user’s profile in JSON

The request below will show your name once you log in. To log in, visit /facebook/:

$.get("facebook/me"); // OUTPUT

After the OAuth login, users can be redirected via the redirect: config documented the redirection configuration.

The FacebookGraphHandler is very similar to the TwitterRESTHandler in many ways:

See this sample application and its source for examples of usage.

Facebook Persist

If your app needs to persist the user’s access token, add access_token: persist to the kwargs. The first time, the user is asked to log in. Thereafter, the user’s credentials are available for all future requests.

This is typically used to show the latest posts / photos of a user or page on every visit. Typically, such requests are cached as well. Here is a sample configuration:

url:
  facebook-persist:
    pattern: /persist/(.*)
    handler: FacebookGraphHandler
    kwargs:
      key: "..."
      secret: "..."
      access_token: persist # Persist the access token after first login
    cache:
      duration: 300 # Cache requests for 5 seconds

Here is a sample response:

$.get("persist/me"); // OUTPUT

The first time, you get an access_token error. Visit /persist/ to log in. Thereafter, your access_token will be stored and used for future requests until it expires, or a user logs in again at /persist/.