发布图片到Facebook页面

13

我希望能够将我的图片发布到我的Facebook页面。下面是我尝试的代码,它来自于RestFB手册页面。但是它显示了一些无法理解的错误。此外,在publish("me/photos")中,我应该传递我的用户名而不是写入me吗?另外,图片bird.jpg存在于我的项目文件夹中。有人能帮帮我吗?任何形式的帮助都将不胜感激。

FacebookClient facebookClient = null;
FacebookType publishPhotoResponse = facebookClient.publish("me/photos",  FacebookType.class,
BinaryAttachment.with("bird.jpg",  FaceBookUpload.class.getResourceAsStream("/bird.jpg")),
              Parameter.with("message", "Test cat"));

System.out.println("Published photo ID: " +  publishPhotoResponse.getId());

这是我遇到的错误

Exception in thread "main" java.lang.IllegalArgumentException: Binary attachment data cannot be null.
at com.restfb.BinaryAttachment.<init>(BinaryAttachment.java:68)
at com.restfb.BinaryAttachment.with(BinaryAttachment.java:113)
at pkg.am.ncrb.shot.FaceBookUpload.main(FaceBookUpload.java:31)

我也尝试了下面的代码

但是它们都没有起作用

InputStream is = new FileInputStream(new File("bird.jpg"));
    FacebookType publishVideoResponse =facebookClient.publish("me/photos",FacebookType.class,
            BinaryAttachment.with("bird.jpg", is),
            Parameter.with("message", "MY PHOTO POST"));    

而且它正在发生异常

Exception in thread "main" java.lang.NullPointerException
at pkg.am.ncrb.shot.FaceBookUpload.main(FaceBookUpload.java:35)

我只需要一个示例代码,将我的图片推送到Facebook。我不知道出了什么问题。


2
“同时,我的项目文件夹中存在着名为bird.jpg的图像” - 但/bird.jpg看起来更像是对服务器根目录的引用。 - CBroe
1
也许你应该使用调试器检查是否可以找到图像。NPE 看起来像是文件系统中的图像位置不正确。 - Norbert
对于“me/photos”问题,您可以使用它。为了使此请求起作用,您必须使用启用publish_pages权限的页面访问令牌(而不是用户访问令牌或应用程序访问令牌)。请参见https://developers.facebook.com/docs/graph-api/reference/page/photos#Creating。 - curveorzos
2个回答

2
  1. In Java, you can't use an object that is set to null. You'll get a NullPointerException.

    The object that is null is the FacebookClient. You should initialize it:

    FacebookClient facebookClient = new DefaultFacebookClient();
    
  2. Where are you putting the photo? If it's embedded in the jar file, use FaceBookUpload.class.getResourceAsStream("/bird.jpg"). If it's in the current directory (probably), use new FileInputStream(new File("bird.jpg")).


0

来自Facebook页面:

REST API

之前已弃用的REST API在v2.1中已被完全删除,所有仍在使用它的应用程序必须迁移到使用Graph API。

如果您的移动应用程序正在使用REST API中的未记录的auth.ExtendSSOAccessToken端点来扩展长期访问令牌,则需要升级您的应用程序以使用完整的iOS或Android SDK。SDK将自动处理扩展访问令牌。

来源 Facebook开发者文档


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接