2017-05-08 59 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中打开它。 –