According to facebook Wiki posting on a wall is very confusing and very difficult to write a program to publish on a wall. And it Don’t even support or document any methods on how to publish on wall using JAVA, and thanks to Google for giving a JAVA library to post on facebook.
Download Library from http://code.google.com/p/facebook–java–api/
Don’t confuse and don’t use old libraries only use facebook-java-api-3.0.1-bin.zip or greater.
I spent around 2 weeks reading many articles and many documents, Wiki on facebook but in vein and tried many ways and finally I got one workable model. Below are simple steps to publish on a Facebook wall using JAVA.
Step1: Login to Facebook
First login to Facebook and goto the url http://www.facebook.com/developers/ then
Click on “+ Set Up New Application” button to start creating the application as shown below.
Provide the application name, click agree for facebook terms and click on “Create Application” button.
Fill in application Name
Now Application is created for you. Just copy the Application API Key, Application Secret and Application ID details which will be used to write the application code.
Now goto Canvas link and enter any url.
Next goto Connect URL link and enter the same url.
Then click on Save. That’s it we have created a facebook application with an Iframe.
Now Copy Application API Key, Application Secret and Application ID details which will be used to write the application code.
Now download the php library from facebook “http://blog.theunical.com/wp-content/uploads/2010/05/facebook-platform.tar1.gz”. For now we will use this library for generating one time session key.
Step2: Generate One Time Token key
How to generate One Time Token key:
Run below URL to get temporary token for the particular user by logging into Facebook.
https://login.facebook.com/code_gen.php?api_key=API_KEY&v=1.0
Note: Please replace “API_KEY” with your application API key from above page.
Then we will get a temporary token key.
This is one time token key that we can use to generate a permanent session key. Careful! Don’t try to execute this programs many times
Step3: Generate one time session key
Below is the sample JAVA code to generate one time session key.
/** * FacebookClient * */ public class FacebookClient { public static String API_KEY = “YOUR API KEY”; public static String SECRET = “YOUR SECRET”; public static void main(String args[]) { // Create the client instance FacebookRestClient client = new FacebookRestClient(API_KEY, SECRET); client.setIsDesktop(true); // is this a desktop app try { String token = client.auth_createToken(); // Build the authentication URL for the user to fill out String url = “http://www.facebook.com/login.php?api_key=” + API_KEY + “&v=1.0″ + “&auth_token=” + token; // Open an external browser to login to your application Runtime.getRuntime().exec(“open ” + url); // OS X only! // Wait until the login process is completed System.out.println(“Use browser to login then press return”); System.in.read(); // fetch session key String session = client.auth_getSession(token,true ); // obtain temp secret String tempSecret = client.getSessionSecret(); // new facebook client object client = new FacebookJaxbRestClient(API_KEY, tempSecret, sessionKey); System.out.println(“Session key is ” + session); // keep track of the logged in user id Long userId = client.users_getLoggedInUser(); System.out.println(“Fetching friends for user ” + userId); // Get friends list client.friends_get(); FriendsGetResponse response = (FriendsGetResponse) client.getResponsePOJO(); List<Long> friends = response.getUid(); // Go fetch the information for the user list of user ids client.users_getInfo(friends, EnumSet.of(ProfileField.NAME)); UsersGetInfoResponse userResponse = (UsersGetInfoResponse) client.getRepsonsePOJO(); // Print out the user information List<User> users = userResponse.getUser(); for (User user : users) { System.out.println(user.getName()); } } catch (FacebookException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } }
(OR)
For example, the full URL for logging in a user could be:
If the user is redirected to the URL specified by the next
parameter, then Facebook grants your application a session. This session is appended to the URL as a JSON-decodable object of the form:
In continuing with the above example, the redirect for a successful login would be:
If the user grants your application the offline_access extended permission, 0 gets returned for expires
and the session never expires unless the user removes the application. In this case, you should store the session key so the user doesn’t have to log in the next time he or she launches your application.
Once the browser has been redirected successfully and you have your session information, you should automatically close the browser window.
Note: The above code will execute only once. We should note down the above session key, This will be used to auto login into the application.
Note: The above code will execute only once. We should note down the above session key, This will be used to auto login into the application.
If you don’t save these session key values you should generate other token key to create one time session key.
We can use the token only once.
Step4: Setting Permission for wall
Giving Permission to your application to publish on facebook wall. To give permission just replace the your API key in below URL and execute in browser.
http://www.facebook.com/login.php?api_key=APIKEYXxxxxxxxxxxxxxxxxx&connect_display=popup&v=1.0&next=http://www.facebook.com/connect/login_success.html&cancel_url=http://www.facebook.com/connect/login_failure.html&fbconnect=true&return_session=true&req_perms=read_stream,publish_stream,offline_access
Then you must see the below 3 Screens
If you don’t see the below then your call back URL or Canvas URL must be incorrect, please check again your application settings and execute the URL again.
Then on final page, you will see a success message. That’s it you have given permission for read_stream,publish_stream and offline_access.
Step5: Publishing the message on Facebook Wall
Java Code to create facebook object with onetime session key and publishing the message on Facebook Wall.
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_ONETIMESESSIONKEY"); FacebookJsonRestClient facebook = new FacebookJsonRestClient( FB_APP_API_KEY, FB_APP_SECRET, FB_SESSION_KEY ); //FacebookJsonRestClient facebookClient2 = (FacebookJsonRestClient)facebook.getFacebookRestClient(); FacebookJsonRestClient facebookClient = (FacebookJsonRestClient)facebook; facebookClient.stream_publish(message, null, null, null, null); System.out.println("successfully updated"); } }
Now you will see the post on wall Great.
So thrilling isn’t we can also publish photos videos on wall.
http://wiki.developers.facebook.com/index.php/Stream.publish
have a great day
– Steven
Please don’t copy this content to any site. This is fully protected by TheUnical Technologies