Post Image on Facebook using java api to publish on Wall

November 8, 2009
Filed under: FaceBook Integration, Featured, JAVA 

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


Comments

6 Comments on Post Image on Facebook using java api to publish on Wall

  1. arin on Wed, 25th Nov 2009 4:02 pm
  2. 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?

  3. Steven Robert on Wed, 25th Nov 2009 9:45 pm
  4. You need to give a public URL and not your local system URL. try some url from flickr or something else.

  5. Satish on Mon, 30th Nov 2009 8:01 am
  6. Nice post i was looking for this for so many days , thanks :)

  7. meelo on Tue, 24th Aug 2010 9:12 pm
  8. waaaaaaaaa thanks guy… i can do it with ur code.. thanks so much!!!!!!!!

  9. Sekh on Thu, 14th Oct 2010 2:44 am
  10. Thank you very much. Its very useful.

    But i am not able to get the session key by following the steps given in the http://blog.theunical.com/facebook-integration/facebook-java-api-example-to-publish-on-wall/

    Can you please kindly guide me how to get the session key to post some comment on the facebook?

    Thanks in advance.

  11. archana on Mon, 27th Feb 2012 4:16 am
  12. 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!!!

Tell me what you're thinking...
and oh, if you want a pic to show with your comment, go get a gravatar!





*
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