2017-05-08 58 views
-1

嗨我想從Facebook頁面上的帖子獲取特定的列,然後最終讓他們輸出格式與文本文件。這是我到目前爲止嘗試的使用Facebook4j獲取帖子

import java.io.BufferedWriter; 
import java.io.File; 
import java.io.FileWriter; 
import java.io.IOException; 

import net.sf.json.JSONArray; 
import net.sf.json.JSONObject; 
import facebook4j.Facebook; 
import facebook4j.FacebookException; 
import facebook4j.FacebookFactory; 
import facebook4j.Post; 
import facebook4j.Reading; 
import facebook4j.ResponseList; 
import facebook4j.conf.Configuration; 
import facebook4j.conf.ConfigurationBuilder; 

public class fB { 

    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(""); 
     confBuilder.setOAuthAppSecret(""); 
     confBuilder.setOAuthAccessToken(""); 


     // 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 { 
      // Get facebook posts 
      String results = getFacebookPosts(facebook); 
      //String responce = stringToJson(results); 



      // Create file and write to the file 
      File file = new File("C:\\Facebook\\test.txt"); 
      if (!file.exists()) 
      { 
       file.createNewFile(); 
      } 
       FileWriter fw = new FileWriter(file.getAbsoluteFile()); 
       BufferedWriter bw = new BufferedWriter(fw); 
       bw.write(results); 
       bw.close(); 
       System.out.println("Writing complete"); 

      } catch (IOException e) { 
       // TODO Auto-generated catch block 
       e.printStackTrace(); 
       } 
     } 



public static String getFacebookPosts(Facebook facebook) throws FacebookException { 
    // Get posts for a particular search 
    ResponseList<Post> results = facebook.getFeed("Rebook"); 
    //System.out.println(facebook.getId()); 
    //System.out.println(facebook.getName()); 
    //System.out.println(facebook.getFeed()); 

    return results.toString(); 
    } 

public static String stringToJson(String data) 
{ // Create JSON object 
    JSONObject jsonObject = JSONObject.fromObject(data); 
    JSONArray message = (JSONArray) jsonObject.get("message"); 
    System.out.println("Message : "+message); 
    return "Done"; 
    } 

} 

這是給我所有的列,但我只對ID,消息和創建日期感興趣。誰能幫我 ?

+0

實際上您只有一個要求的權限存在。在提出API請求時,您需要詢問您想要的_fields_。 https://developers.facebook.com/docs/graph-api/using-graph-api/#fields(如何/如果這是可能的使用facebook4j,你必須找出你自己,它應該在文檔中。 ) – CBroe

回答

1

爲了指定您希望從Facebook API返回數據的方式,您需要使用Reading class。例如,您可以構造指定要返回這樣的字段閱讀:

Reading reading = new Reading().fields("id", "created_time", "message"); 
facebook.getFeed("Rebook", reading); 

你可以找到更多的facebook4j的官方documentation

+0

非常感謝你..我沒有進入記事本,能否請你給我建議我怎樣才能得到它與一些格式很好 –

+0

非常感謝你..我現在進入記事本,你能告訴我如何我可以通過一些格式來獲得它嗎 –

+0

您可以使用一個CSV寫作庫([例如Apache的Commons CSV](https://commons.apache.org/proper/commons-csv/))將數據寫入CSV然後在Excel中打開它。 –