Testing the Graph API of Facebook

Submitted by Jochus on Thu, 21/02/2013 - 23:56 | Posted in: Java


I just tested the "new" (already in Facebook since June 2011) Graph API of Facebook. Using the RestFB Java client (http://www.restfb.com/), I was able to get a list of my friends, update my status, ... etc, etc. For testing, I retrieved an access token using the Graph Explorer tool (https://developers.facebook.com/tools/explorer).

Java project setup (pom.xml)

<project>
	<modelVersion>4.0.0</modelVersion>
	<groupId>be.jochenhebbrecht</groupId>
	<artifactId>facebook-test</artifactId>
	<version>1.0.0-SNAPSHOT</version>
 
	<dependencies>
		<dependency>
			<groupId>com.restfb</groupId>
			<artifactId>restfb</artifactId>
			<version>1.6.11</version>
		</dependency>
	</dependencies>
</project>

Getting a list of friends

import com.restfb.Connection;
import com.restfb.DefaultFacebookClient;
import com.restfb.FacebookClient;
import com.restfb.types.User;
 
public class FacebookTest {
 
	private static final String MY_ACCESS_TOKEN = "##REPLACE_ME##";
 
	public static void main(String[] args) {
		FacebookClient facebookClient = new DefaultFacebookClient(MY_ACCESS_TOKEN);
		Connection<User> myFriends = facebookClient.fetchConnection("me/friends", User.class);
 
		for (User u : myFriends.getData()) {
			System.out.println(u.getName());
		}
        }
}

Updating my status

import com.restfb.DefaultFacebookClient;
import com.restfb.FacebookClient;
import com.restfb.Parameter;
import com.restfb.types.FacebookType;
 
public class FacebookTest {
 
	private static final String MY_ACCESS_TOKEN = "##REPLACE_ME##";
 
	public static void main(String[] args) {
		FacebookClient facebookClient = new DefaultFacebookClient(MY_ACCESS_TOKEN);
		FacebookType publishMessageResponse = facebookClient.publish("me/feed", FacebookType.class, Parameter.with("message", "Test from Graph API"));
		System.out.println("Published message ID: " + publishMessageResponse.getId());
        }
}

Add new comment

The content of this field is kept private and will not be shown publicly.

Full HTML

  • Lines and paragraphs break automatically.
  • You can caption images (data-caption="Text"), but also videos, blockquotes, and so on.
  • Web page addresses and email addresses turn into links automatically.
  • You can enable syntax highlighting of source code with the following tags: <code>, <blockcode>, <bash>, <cpp>, <css>, <html5>, <java>, <javascript>, <php>, <sql>, <xml>. The supported tag styles are: <foo>, [foo].
CAPTCHA
This question is for testing whether or not you are a human visitor and to prevent automated spam submissions.