RestFB is a simple Facebook Graph API which also supports Old Rest APIs.
It is an open source software. Using RestFB api you can connect to Facebook and properly integrate with Facebook. RestFB is a lightweight implementation of Facebook Graph API.
There are certain steps in order to use RestFB apis :-
1. Installation
Download RestFB jar restfb-1.6.14.jar and add it as an external library in your project.
Or if you are using Maven, then add RestFB dependency in you pom.xml as
<dependency>
<groupId>com.restfb</groupId>
<artifactId>restfb</artifactId>
<version>1.6.14</version>
</dependency>
2. Create Facebook Developer application.
Go to the link https://developers.facebook.com/ and login using facebook login credentials.
Login using your Facebook login credentials.
Create a New App
After your app is created you will be redirected to a page like this :-
You can view App ID and App Secret keys (App Secret key can be viewed by clicking on Show button as shown in the image above).
Please save those keys, as you will be using those keys in your program.
3. Get Access Token
Go to this link
https://developers.facebook.com/tools/explorer?method=GET&path=me%3Ffields%3Did%2Cname
It will open a page like this.
Click on ""Get access token" button.
Then select all the permissions you want to get and click on "Get Access Token" button.
Then you can view Access Token as given in the image below.
Save the Access Token.
4. Getting FaceBook data
In your program, you can fetch facebook data as follows :-
private String ACCESS_TOKEN = "your access token";
FacebookClient facebookClient = new DefaultFacebookClient(ACCESS_TOKEN);
// Fetch your user name
User user = facebookClient.fetchObject("me", User.class);
System.out.println(user.getName());
// Fetch like count of a page
Page page = facebookClient.fetchObject("cocacola", Page.class);
System.out.println(page.getLikes());
// Fetch friends list
Connection<User> myFriends = facebookClient.fetchConnection("me/friends", User.class);
for(User u : myFriends.getData()) {
System.out.println(u.getName());
}
You can also get Access Token as :-
AccessToken accessToken = new DefaultFacebookClient().obtainExtendedAccessToken(MY_APP_ID,
MY_APP_SECRET);
String token = accessToken.getAccessToken();
FacebookClient facebookClient = new DefaultFacebookClient(token);
5. Error Scenario
You can see the result in console when you run this program.
But if you get an error like :
com.restfb.FacebookNetworkException: A network error occurred while trying to communicate with Facebook: Facebook POST failed (HTTP status code null)
Then it means, you need to set the proxy properties before the URL instance is created.
(e.g you are using those apis from a system in your company or workstation, and your company's proxy is preventing this request).
You can set proxy in the code as follows. Add the following code in the very beginning of your program and before you are creating instance of FacebookClient.
System.setProperty("http.proxySet", "true");
System.setProperty("http.proxyHost", "your proxy site");
System.setProperty("http.proxyPort", "port no.");
e.g (please give correct proxy site and port no.)
System.setProperty("http.proxySet", "true");
System.setProperty("http.proxyHost", "webcache.mydomain.com");
System.setProperty("http.proxyPort", "8080");
but if the exception still continues, then use the following piece of code :-
(I am using https instead of http here)
System.setProperty("https.proxySet", "true");
System.setProperty("https.proxyHost", "your proxy site");
System.setProperty("https.proxyPort", "port no.");
Hope, this blog helps you in using RestFB APIs.
All the best.
It is an open source software. Using RestFB api you can connect to Facebook and properly integrate with Facebook. RestFB is a lightweight implementation of Facebook Graph API.
There are certain steps in order to use RestFB apis :-
1. Installation
Download RestFB jar restfb-1.6.14.jar and add it as an external library in your project.
Or if you are using Maven, then add RestFB dependency in you pom.xml as
<dependency>
<groupId>com.restfb</groupId>
<artifactId>restfb</artifactId>
<version>1.6.14</version>
</dependency>
2. Create Facebook Developer application.
Go to the link https://developers.facebook.com/ and login using facebook login credentials.
Login using your Facebook login credentials.
Create a New App
After your app is created you will be redirected to a page like this :-
You can view App ID and App Secret keys (App Secret key can be viewed by clicking on Show button as shown in the image above).
Please save those keys, as you will be using those keys in your program.
3. Get Access Token
Go to this link
https://developers.facebook.com/tools/explorer?method=GET&path=me%3Ffields%3Did%2Cname
It will open a page like this.
Click on ""Get access token" button.
Then select all the permissions you want to get and click on "Get Access Token" button.
Then you can view Access Token as given in the image below.
Save the Access Token.
4. Getting FaceBook data
In your program, you can fetch facebook data as follows :-
private String ACCESS_TOKEN = "your access token";
FacebookClient facebookClient = new DefaultFacebookClient(ACCESS_TOKEN);
// Fetch your user name
User user = facebookClient.fetchObject("me", User.class);
System.out.println(user.getName());
// Fetch like count of a page
Page page = facebookClient.fetchObject("cocacola", Page.class);
System.out.println(page.getLikes());
// Fetch friends list
Connection<User> myFriends = facebookClient.fetchConnection("me/friends", User.class);
for(User u : myFriends.getData()) {
System.out.println(u.getName());
}
You can also get Access Token as :-
AccessToken accessToken = new DefaultFacebookClient().obtainExtendedAccessToken(MY_APP_ID,
MY_APP_SECRET);
String token = accessToken.getAccessToken();
FacebookClient facebookClient = new DefaultFacebookClient(token);
5. Error Scenario
You can see the result in console when you run this program.
But if you get an error like :
com.restfb.FacebookNetworkException: A network error occurred while trying to communicate with Facebook: Facebook POST failed (HTTP status code null)
Then it means, you need to set the proxy properties before the URL instance is created.
(e.g you are using those apis from a system in your company or workstation, and your company's proxy is preventing this request).
You can set proxy in the code as follows. Add the following code in the very beginning of your program and before you are creating instance of FacebookClient.
System.setProperty("http.proxySet", "true");
System.setProperty("http.proxyHost", "your proxy site");
System.setProperty("http.proxyPort", "port no.");
e.g (please give correct proxy site and port no.)
System.setProperty("http.proxySet", "true");
System.setProperty("http.proxyHost", "webcache.mydomain.com");
System.setProperty("http.proxyPort", "8080");
but if the exception still continues, then use the following piece of code :-
(I am using https instead of http here)
System.setProperty("https.proxySet", "true");
System.setProperty("https.proxyHost", "your proxy site");
System.setProperty("https.proxyPort", "port no.");
Hope, this blog helps you in using RestFB APIs.
All the best.