13

I want to post my images to my facebook page. Below is the code I tried which is from RestFB manual page. But it's showing some kind of error which is not getting understood. Also in publish("me/photos") should I pass my username instead of writing me?. Also the image bird.jpg exists in my project folder. Can anyone help me? Any kind of help will be appreciated

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());

This is the error which I encountered

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)

Also I tried the below code

But none of it is working

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"));    

and it's getting exception

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

I just need a sample piece of code that push my image into facebook. I don't know what went wrong.

sooraj g
  • 143
  • 1
  • 12
  • 2
    _“Also the image bird.jpg exists in my project folder”_ – but `/bird.jpg` rather looks like a reference to the server root folder. – CBroe Apr 29 '15 at 07:18
  • 1
    Perhaps you should check with a debugger if the image can be found. The NPE looks like the image is not at the correct position in the filesystem. – Norbert May 05 '15 at 12:35
  • For the "me/photos" question, you can use that. In order for this request to work, you have to use a Page Access Token (instead of a User Access Token or an Applicaiton Access Token) with publish_pages permission enabled. See https://developers.facebook.com/docs/graph-api/reference/page/photos#Creating – curveorzos May 05 '15 at 14:54

2 Answers2

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")).

tbodt
  • 16,609
  • 6
  • 58
  • 83
0

From the Facebook page:

REST API

The previously deprecated REST API has been completely removed in v2.1, and all apps still using it must migrate to using Graph API.

If your mobile app is using the undocumented auth.ExtendSSOAccessToken endpoint from the REST API to extend long-lived access tokens, you need to upgrade your app to use the full iOS or Android SDK. The SDKs will automatically handle extending access tokens.

Source Facebook Developers Docs

3m0l3y
  • 46
  • 4