Advertisements

Accessing Facebook using Java Program.

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.

Advertisements

Fetching a Webpage using Java

This is a simple java code to fetch a webpage.


public class Test {

public static void main(String[] args) {
 URL url;
 InputStream is = null;
 BufferedReader br;
 String line;

try {
 url = new URL("http://www.wikipedia.org/");
 is = url.openStream(); // throws an IOException
 br = new BufferedReader(new InputStreamReader(is));

while ((line = br.readLine()) != null) {
 System.out.println(line);
}
 } catch (MalformedURLException mue) {
 mue.printStackTrace();
 } catch (IOException ioe) {
 ioe.printStackTrace();
 } finally {
 try {
 is.close();
 } catch (IOException ioe) {
 // nothing to see here
 }
 }
 }
 }