sooraj g
sooraj g

Reputation: 143

Posting image to facebook page

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.

Upvotes: 13

Views: 1188

Answers (2)

3m0l3y
3m0l3y

Reputation: 46

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

Upvotes: 0

tbodt
tbodt

Reputation: 17007

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

Upvotes: 2

Related Questions