Post Image on Facebook using java api to publish on Wall

First Follow the listed Steps from my Previous post http://blog.theunical.com/facebook-integration/facebook-java-api-example-to-publish-on-wall/ then in last program include the below code to publish a photo and also meta data on a facebook wall.

In the same way we can even publish MP3, Videos, Flash on facebook.

Publishing a Post Containing an Image, Action Links and Custom Metadata.

package com.unical;

import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.HttpState;
import org.apache.commons.httpclient.NameValuePair;
import org.apache.commons.httpclient.methods.GetMethod;
import org.apache.commons.httpclient.methods.PostMethod;
import org.apache.commons.httpclient.params.HttpClientParams;

import com.google.code.facebookapi.Attachment;
import com.google.code.facebookapi.AttachmentMediaImage;
import com.google.code.facebookapi.FacebookException;
import com.google.code.facebookapi.FacebookJsonRestClient;
import com.google.code.facebookapi.FeedFacebookPhoto;
import com.google.code.facebookapi.Permission;
import com.google.code.facebookapi.TemplatizedAction;

public class SendtoFacebook {

	public static void main (String a[]) throws FacebookException{ 

	SendtoFacebook sfb=new SendtoFacebook();
	sfb.send("From My App: publish steven on facebook");
	}
	public void send(String message)throws FacebookException{

		String FB_APP_API_KEY = new String("YOUR APIKEY");
		String FB_APP_SECRET = new String("YOUR SECRET");
		String FB_SESSION_KEY = new String("YOUR SESSION KEY");
		FacebookJsonRestClient facebook = new FacebookJsonRestClient( FB_APP_API_KEY, FB_APP_SECRET, FB_SESSION_KEY );

		//FacebookJsonRestClient facebookClient2 = (FacebookJsonRestClient)facebook.getFacebookRestClient();
		FacebookJsonRestClient facebookClient = (FacebookJsonRestClient)facebook;
		Attachment attachment = new Attachment();
		attachment.setCaption("Caption for attachment");
		attachment.setDescription("Description for attachment");
		attachment.setHref("http://blog.unical.com");
		attachment.setName("Hopefully this works");

		AttachmentMediaImage  attach_media = new AttachmentMediaImage( );
		attach_media.addImage("http://profile.ak.fbcdn.net/v229/868/86/q518210788_2085.jpg", "http://blog.theunical.com");

		attachment.setMedia(attach_media);

		facebookClient.stream_publish(message, attachment, null, null, null);

			System.out.println("successfully updated");
	}
}


6 thoughts on “Post Image on Facebook using java api to publish on Wall”

  1. Thanks a lot for the post… however I cant see the image in the post, but apart from the picture everything gets updated successfully on my wall… what could be the problem?

  2. You need to give a public URL and not your local system URL. try some url from flickr or something else.

  3. i am attaching video in the same way using AttachmentMediaVideo.
    But it is uploading only the image and details. when i click on that it wont play the video. right click shows movie not loaded.. any help!!!

Leave a Reply

Your email address will not be published. Required fields are marked *

*
To prove you're a person (not a spam script), type the security word shown in the picture. Click on the picture to hear an audio file of the word.
Anti-spam image