2014-09-29 114 views
1

後當我發佈圖片爲我的牆由發佈用圖片facebook的頁面restFb

FacebookType publish = fc.publish("me/photos", FacebookType.class, 
         BinaryAttachment.with(attachmentName, temporaryStream), 
         Parameter.with("message", "my myssage")); 

預期後創建。我可以在消息下看到一條消息和已發佈的pictute。

但是當我想要做同樣的事情,但在一些頁面牆上,而不是用戶牆。

FacebookType publish = fc.publish(pageId + "/photos", FacebookType.class, 
         BinaryAttachment.with(attachmentName, temporaryStream), 
         Parameter.with("message", "my myssage")); 

該帖子已發佈,但我只能看到文本。不是圖片。只有當我點擊帖子的日期時才能看到圖片。我還可以通過點擊頁面頂部的照片,然後點擊「頁面名稱」照片標籤來查看此照片。

有沒有什麼辦法如何看到照片作爲帖子的一部分,當我發佈在網頁上?

回答

3

我終於找到了解決方案。要將照片發佈到具有普通「僅文字」帖子的部分,您需要將照片發佈到牆上(您的用戶牆而非頁面牆),然後獲取其鏈接,最後創建一個帶有圖片鏈接。

這裏是示例代碼:

FacebookType photo = fc.publish(pageid + "/photos" , FacebookType.class, 
      BinaryAttachment.with(photoName, photoInputStream)); 
Link photoLink = fc.fetchObject(photo.getId(), Link.class); 
FacebookType post = fc.publish(pageid + "/feed", FacebookType.class, 
      Parameter.with("message", "your message"),Parameter.with("type", "photo"), 
      Parameter.with("link", photoLink.getLink())); 

編輯:我忘了一件事。當您以後刪除帖子時,您不會刪除照片。要做到這一點,你必須手動完成。

3

你的答案工作一種享受,幫我無盡的金額,但是我發現我的解決方案,我並不需要

FacebookType post = fc.publish(pageid + "/feed", FacebookType.class, 
     Parameter.with("message", "your message"),Parameter.with("type", "photo"), 
     Parameter.with("link", photoLink.getLink())); 

,我會recive一個錯誤(我應該有複製給你看),但photoLink.getLink()在我的情況下返回null。

所以我更新的解決方案再次結束了

 FacebookType photo = client.publish(groupId + "/photos", 
      FacebookType.class, 
      BinaryAttachment.with(imageName), 
        imageAsBytes, 
        "image/png"), 
      Parameter.with("message", 
        message)); 

只是我的2美分,感謝您的幫助!

編輯

而且同時玩弄我注意到,在該解決方案,我來到了我創建一個FacebookType photo對象,這個對象沒有得到任何地方使用,你只調用這是客戶端的發佈方法在您的程序中進一步創建。

我的最後一個類是低於,(我的應用程序從計算機拍攝圖像,並給一組壁)

public void createImagePostInGroup(String message, String groupId, String imageFilePath){ 

    byte[] imageAsBytes = fetchBytesFromImage(imageFilePath); 

    DefaultFacebookClient client = 
     new DefaultFacebookClient(accessToken, Version.LATEST); 

    client.publish(groupId + "/photos", 
      FacebookType.class, 
      BinaryAttachment.with(imageFilePath.substring(imageFilePath.length()-34), 
        imageAsBytes, 
        "image/png"), 
      Parameter.with("message", 
        message)); 
} 

而我的圖像被轉換成一個字節數組在該方法中

private byte[] fetchBytesFromImage(String imageFile) { 

    ByteArrayOutputStream baos = new ByteArrayOutputStream(); 
    byte fileContent[] = null; 

    try{ 

     BufferedImage image = ImageIO.read(new File(imageFile)); 

     ImageIO.write(image, "png", baos); 
     baos.flush(); 
     fileContent = baos.toByteArray(); 

    }catch (Exception e){ 
     e.printStackTrace(); 
    } 
    return fileContent; 

} 

再次感謝您的幫助。