Created
December 30, 2014 03:31
-
-
Save anonymous/54ff3baac52c12f81b9c to your computer and use it in GitHub Desktop.
Sample usage of Facebook API using facebook4j
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.io.BufferedWriter; | |
import java.io.File; | |
import java.io.FileWriter; | |
import java.io.IOException; | |
import facebook4j.Facebook; | |
import facebook4j.FacebookException; | |
import facebook4j.FacebookFactory; | |
import facebook4j.Post; | |
import facebook4j.ResponseList; | |
import facebook4j.conf.Configuration; | |
import facebook4j.conf.ConfigurationBuilder; | |
public class FacebookImpl { | |
public static void main(String[] args) throws FacebookException { | |
// Make the configuration builder | |
ConfigurationBuilder confBuilder = new ConfigurationBuilder(); | |
confBuilder.setDebugEnabled(true); | |
// Set application id, secret key and access token | |
confBuilder.setOAuthAppId("64105717933175"); // Get from developers.facebook.com by just creating a new app | |
confBuilder.setOAuthAppSecret("9eac12c6627691de9384022df628c5d"); // Get from developers.facebook.com by just creating a new app | |
confBuilder.setOAuthAccessToken("64157179353175|cFl365CzuLRXIUp3FSFsCyinfF0"); // Get from developers.facebook.com by just creating a new app | |
// Set permission | |
confBuilder.setOAuthPermissions("email,publish_stream, id, name, first_name, last_name, generic"); | |
confBuilder.setUseSSL(true); | |
confBuilder.setJSONStoreEnabled(true); | |
// Create configuration object | |
Configuration configuration = confBuilder.build(); | |
// Create facebook instance | |
FacebookFactory ff = new FacebookFactory(configuration); | |
Facebook facebook = ff.getInstance(); | |
try { | |
// Create file and write to the file | |
File file = new File("E:\\Facebook\\File\\test.txt"); | |
if (!file.exists()) | |
{ | |
file.createNewFile(); | |
FileWriter fw = new FileWriter(file.getAbsoluteFile()); | |
BufferedWriter bw = new BufferedWriter(fw); | |
bw.write(getFacebookPostes(facebook)); | |
bw.close(); | |
System.out.println("Writing complete"); | |
} | |
} catch (IOException e) | |
{ | |
// TODO Auto-generated catch block | |
e.printStackTrace(); | |
} | |
} | |
public static String getFacebookPostes(Facebook facebook) throws FacebookException { | |
// Get posts for a particular search | |
ResponseList<Post> results = facebook.getPosts("Reebok"); | |
Post post = results.get(0); | |
return post.getMessage(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment