Thursday, March 8, 2012

Wanna Post on your Facebook wall using JAVA API...?? Here's the way..

  • Post on your/ your friend's facebook wall,
  1. Go to restfb api and download its libraries..
  2. Extract restfb-verno.jar and place in eclipse's build path,
  3. Using java program(bottom of the post) you can post/get info from your facebook profile, but first u need to get access token..
  4. Login using fb username n password...
  5. Go to https://developers.facebook.com/apps and create a new app
  6. After creating ur app, in the left side pane u can find settings, choose that and go to Auth dialog,
  7. choose default activity privacy to friends or public,
  8. In Authenticated Referrals, mention user and friend permissions to public_actions, user_status, friends_status, user_photos..etc.., and  set extended settings to publish_stream, read_stream, photo_upload, read_mailbox etc..,(based on your requirement whether u want to GET info or POST  info).
  9. save all the changes and then choose use GRAPH API explorer in the left side pane related links.
  10. Now new page with graph API explorer will open, click on Get Access Token,
  11. new window opens where u have to set User data permissions(choose user_photos, user_status..etc), Friends data permissions(friends_photo, friends_status, etc) and Extended permissions(publish_stream, read_stream,upload_photos, offline_status etc). click Get Access Token and then allow this app to use ur related data.
  12. An access token will be generated, u have to use this in the program to GET/POST data(based on the extended permissions u have set).
  13. Copy this access token and use this to post a message on your facebook wall
import java.io.*;
import com.restfb.*;
import com.restfb.types.*;
import com.restfb.util.*;

public class FacebookTest
{
    public static void main(String args[])
    {
        FacebookClient facebookClient = new DefaultFacebookClient(<ur access token>);

        // It's also possible to create a client that can only access
        // publicly-visible data - no access token required.
        // Note that many of the examples below will not work unless you supply an access token!

        User user = facebookClient.fetchObject("me", User.class);

        System.out.println("User name: " + user.getName());
        System.out.println("Last name: " + user.getLastName());
        String connection = "me/feed";
        FacebookType publishMessageResponse =
                  facebookClient.publish(connection, FacebookType.class,
                    Parameter.with("message", "helo ..:)"));
        FacebookType publishPhotoResponse = facebookClient.publish("me/photos", FacebookType.class,BinaryAttachment.with("holiMan.jpg",FacebookTest.class.getResourceAsStream("holiMan.jpg")),Parameter.with("message", "The HOLI MAN")); \\should be in same directory of source code, in this code in bin directory of eclipse IDE

                //System.out.println("Published message ID: " + publishMessageResponse.getId());
        System.out.println("Success");

        
}
}

1 comment:

  1. Hi, This Code Will Not work if we are not login in facebook. If I tried this code without login in facebook i m getting the error

    FacebookOAuthException: Received Facebook error response of type OAuthException: Error validating access token: The session is invalid because the user logged out.

    Please Give me the solution for this.

    ReplyDelete