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

        
}
}

Wanna Tweet on your twitter page using JAVA API...?? Here's the way..

  • Tweet on your twitter page....
  1. First download Twitter4j java library from Twitter4j website    
  2. Since, I'm using eclipse IDE, downloaded twitter4j zip file and selected its lib file in the eclipse project's build path.(lib file is twitter4j-core.2.2.5.jar)
  3. A sample program to post on twitter is mentioned at the bottom of the post...
  4. THE MOST IMPORTANT STEP is this i.e. to get ur consumer key, consumer secret, access token and access token secret which needs to be entered in the java program.
To get those you need to follow the following steps
  • You need to have twitter account(sign up fast)...
  •  go to https://dev.twitter.com/apps 
  • and click on create new application
  • Enter app name, description, URL(any website of urs example can be ur blog) and finally agree for the agreement and click the button to proceed to next page
  • In this page you will find ur two keys(consumer key and consumer secret) you need to enter in your program...at the bottom u can find create ur access key, click on that with which u will get ur access token and access token secret but these keys are valid on for read access i.e. to GET data
  • To POST data(read and write to ur twitter) go to settings tab and set application type to Read, Write and Access direct messages and save it, ur new keys will be generated.
  • Now enter all ur keys in the program below and run the program with ur own message :)
  • Ur msg will be finally seen in ur own twitter page...
import twitter4j.*;
import twitter4j.conf.*;

public class TestTw {
public static void main(String args[]) throws TwitterException{
    // The factory instance is re-useable and thread safe.
   
    ConfigurationBuilder cb = new ConfigurationBuilder();
    cb.setDebugEnabled(true)
      .setOAuthConsumerKey(<ur consumer key>)
      .setOAuthConsumerSecret(<ur consumer secret>)
      .setOAuthAccessToken(<ur access token>)
      .setOAuthAccessTokenSecret(<ur access token secret>);
    TwitterFactory tf = new TwitterFactory(cb.build());
    Twitter twitter = tf.getInstance();
   
    Status status = twitter.updateStatus("This is the test message using twitter4j API");
    System.out.println("Successfully updated the status to [" + status.getText() + "].");
    }
   
}