Here is a simple java application for accessing facebook. This is done by using restfb, which is a simple and flexible Facebook Graph API and Old REST API client written in Java.
For this we need an access token which we can generate using graph API explorer in facebook developers website.
Download the restfb jar.
Get an access token with necessary permissions.
package com.amal.fb; import com.restfb.DefaultFacebookClient; import com.restfb.FacebookClient; import com.restfb.Parameter; import com.restfb.types.FacebookType; import com.restfb.types.User; public class FacebookAppnew { public static void main(String[] args) { System.setProperty("https.proxyHost", "PROXY"); System.setProperty("https.proxyPort", "PORT"); FacebookClient fbClient = new DefaultFacebookClient("MY_ACCESS_TOKEN"); User user = fbClient.fetchObject("me", User.class); // Getting the Details from FB System.out.println("UserName :"+user.getName()); System.out.println("Birthday :"+user.getBirthday()); System.out.println("Bio :"+user.getBio()); System.out.println("Email :"+user.getEmail()); //Making a post in Facebook FacebookType publishMessageResponse = fbClient.publish("me/feed", FacebookType.class, Parameter.with("message", "Good Evening")); } }
Similarly we can post photos, videos etc.
That’s a really useful tip. Thanks Amal 🙂
hello, you can post one method for obtain access token automatically using Post or other method?
Following lines return null only getname works.
System.out.println(“Birthday :”+user.getBirthday());
System.out.println(“Bio :”+user.getBio());
System.out.println(“Email :”+user.getEmail());
This was written before 2 years and was working perfectly at that time. Not sure about the changes happened to the api. One more thing. If security is enabled in the profile, the code will not be able to fetch the data.