Java Reference
In-Depth Information
Accessing Facebook
try it out
This exercise demonstrates how to use the REST service of Facebook. It assumes that you already have
a Facebook account at http://www.facebook.com . Facebook developer's documentation can be found
at https://developers.facebook.com/ . Information on its REST API (which Facebook now calls the
“Graph API”) can be found at https://developers.facebook.com/docs/graph-api/reference/ ,
and includes a list of all the resources you can access.
1.
To access the Facebook Graph API, you'll first need to register as a developer. This can be done by
navigating to https://developers.facebook.com/ an d selecting Register as a Developer under
Apps in the upper menu.
2.
Next, just as for Twitter, you need to create a set of keys under https://developers.facebook
.com/tools/accesstoken/ . Create a new app, give it a unique name, and select a category.
3.
After creating your app, you'll be brought to your app's “Dashboard,” which contains a wealth of
information and settings to play with. See Figure 10-24.
figure 10-24  
4.
Be sure to note your “App ID” and “App Secret” keys. Click + Add Platform, choose website, and
enter http://www.example.com as the site URL, which you'll also use as a callback URL. Even if
you're not the owner of this domain, it'll work fine to extract the callback URL (together with the
access token) and copy it in your application. Don't forget to save your changes.
5.
Facebook uses a complicated login flow with the possibility to modify many options and permis-
sions, inspired by OAuth but not exactly the same. The page on manually building a login flow,
found at https://developers.facebook.com/docs/facebook-login/manually-build-a-
login- flow , provides more details if you're interested (there is a lot to read through).
6.
Create a single class, called FacebookTest , using the following code:
package com.facebook.graph;
import java.awt.Desktop;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.URI;
import java.net.URISyntaxException;
import java.util.HashMap;
Search WWH ::




Custom Search