Skip to content

Instantly share code, notes, and snippets.

@kvorion
Created April 14, 2011 02:45
package FacebookGraph;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.URL;
import java.net.URLConnection;
import java.util.Properties;
import javax.net.ssl.HttpsURLConnection;
import javax.net.ssl.SSLContext;
import javax.net.ssl.X509TrustManager;
import com.restfb.Connection;
import com.restfb.DefaultFacebookClient;
import com.restfb.FacebookClient;
import com.restfb.types.User;
public class FacebookNetwork {
public Properties properties = new Properties();
public FacebookClient fbClient = null;
public String accessToken = null;
public FacebookNetwork()
{
try {
properties.load(new FileInputStream("fb.properties"));
this.accessToken = properties.getProperty("access_token");
this.fbClient = new DefaultFacebookClient(this.accessToken);
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
/*
* Get a list of your facebook friends and related details
*/
public void GetFriends()
{
//this is a bad hack to get the https to work. not recommended
javax.net.ssl.TrustManager[] trustAllCerts = new javax.net.ssl.TrustManager[]{
new X509TrustManager() {
public java.security.cert.X509Certificate[] getAcceptedIssuers() {
return null;
}
public void checkClientTrusted(
java.security.cert.X509Certificate[] certs, String authType) {
}
public void checkServerTrusted(
java.security.cert.X509Certificate[] certs, String authType) {
}
}
};
// Install the all-trusting trust manager
try {
SSLContext sc = SSLContext.getInstance("SSL");
sc.init(null, trustAllCerts, new java.security.SecureRandom());
HttpsURLConnection.setDefaultSSLSocketFactory(sc.getSocketFactory());
} catch (Exception e) {
System.out.println(e.getMessage());
}
Connection<User> myFriends = this.fbClient.fetchConnection("me/friends", User.class);
for (User friend: myFriends.getData())
{
String id = friend.getId();
String nodeUrl = "https://graph.facebook.com/" + id + "?access_token=" + properties.getProperty("access_token");
try {
URL graphNode = new URL(nodeUrl);
URLConnection conn = graphNode.openConnection();
BufferedReader in = new BufferedReader(new InputStreamReader(conn.getInputStream()));
String line;
BufferedWriter writer = new BufferedWriter(new FileWriter("facebook-friends/" + id + ".txt"));
while ((line = in.readLine()) != null)
{
writer.write(line);
}
in.close();
writer.flush();
writer.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
/**
* @param args
*/
public static void main(String[] args) {
FacebookNetwork fn = new FacebookNetwork();
fn.GetFriends();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment