Facebook Pages the Java way

If you’re ready to jump on the social bandwagon, you might consider creating a Facebook Page to extend your social reach. Creating a Facebook Page couldn’t be easier, but posting up content can be a drag.. so why not automate it?

The content on our Facebook Page – http://facebook.com/giglondon – is fully automated, from creating daily events to posting updates of the best gigs each month. The first obstacle that we face in connecting with Facebook is that we need an API Key/Secret and a Session Key.

Facebook API Key/Secret and Session Key

  • For the API Key and Secret, go to http://www.facebook.com/developers and click on the ‘Set Up New Application’ button, which gives you a unique API Key and Secret for your application.
  • To get a Session Key, do the following:
    1. Copy & paste the following URL into a browser, remembering to replace <API Key> with your unique API Key:
      http://www.facebook.com/login.php?api_key=<API Key>&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&session_key_only=true&req_perms=create_event,publish_stream,offline_access
    2. Login with the Facebook account that is an Admin user of your Facebook Page (important!).
    3. Click on ‘Allow’ until you get to the Success page, whereby the redirect URL will be something like:
      http://www.facebook.com/connect/login_success.html?session={”session_key”:<Session Key>,”uid”:<User Id>,”expires”:0,”secret”:<Session Secret>,”sig”:<Signature>}
    4. Copy the <Session Key> and <User Id> values from that URL and keep them safe.

If you have any difficulty with what’s been outlined above, I encourage you to look at this great blog, which will take you step by step through how to generate the Facebook API Key/Secret and Session Key.

Facebook Java API

The Facebook Java API is a Facebook client that can be used to push content over to a Facebook Page.

The easiest option is to include the client library as a dependency in your Maven pom.xml file. If Maven isn’t your preferred option, download the library from here.

<dependency>
  <groupId>com.google.code.facebookapi</groupId>
  <artifactId>facebook-java-api</artifactId>
  <version>3.0.2</version>
</dependency>

Posting Your First Facebook Page Update

...
import com.google.code.facebookapi.*;
...
  // Replace the values with your unique API Key/Secret and Session Key!
  public final static String FACEBOOK_API_KEY = "API KEY";
  public final static String FACEBOOK_API_SECRET = "API SECRET";
  public final static String SESSION_KEY = "SESSION KEY";

  // Replace the User Id with the Admin User Id of the Facebook Page
  public final static String USER_ID = "USER ID";

  // Replace the Facebook Page Id - if you don't know your Facebook Page Id this link
  // will tell you how to find it: http://support.wibiya.com/entries/187061-how-to-find-your-facebook-fan-page-id
  public final static String FACEBOOK_PAGE_ID = "FACEBOOK PAGE ID";

  public void myFirstPost() throws Exception {
    // First we create the client with the API Key/Secret and Session Key
    FacebookJsonRestClient client = new FacebookJsonRestClient(FACEBOOK_API_KEY, FACEBOOK_API_SECRET, SESSION_KEY);

    // Image attachment to make our first post more eye catching (note: you can add as many images as you like)
    Attachment attachment = new Attachment();
    // The first URL is the image URL, the second is the URL that the user is taken to when they click the image
    attachment.addMedia(
      new AttachmentMediaImage(
        "http://blog.niteviva.com/wp-content/uploads/2010/06/cool.jpg",
        "http://blog.niteviva.com/wp-content/uploads/2010/06/cool.jpg"));

    // Post your first post!
    client.stream_publish(
      "Snoopy is too cool for school",
      attachment, null, new Long(FACEBOOK_PAGE_ID), new Long(USER_ID));
  }

If all goes well then you will see the post on the wall of your Facebook Page .. sweet!

facebook post

Facebook Page Events

We can do so much more with the Facebook API .. here’s a code snippet on creating an event:

  public void newEvent() throws Exception {
    FacebookJsonRestClient client = new FacebookJsonRestClient(FACEBOOK_API_KEY, FACEBOOK_API_SECRET, SESSION_KEY);

    // Convert the event date/time to seconds
    Date eventDateTime = new SimpleDateFormat("yyyy-MM-dd HH:mm").parse("2010-12-25 21:00");
    String eventTimeInSeconds = new Long(eventDateTime.getTime() / 1000).toString();

    // Create event info map
    Map<String, String> event = new HashMap<String, String>();
    event.put("start_time", eventTimeInSeconds);
    event.put("name", "Snoopy & Friends Xmas Party");
    event.put("location", "Charlie Brown's Crib");
    event.put("page_id", FACEBOOK_PAGE_ID);
    event.put("privacy_type", "OPEN");
    event.put("description", "FYI Snoopy loves root beer and pizza!");

    // Post new event
    client.events_create(event);
  }

snoopy-xmasparty

Sadly the Facebook Java API client library doesn’t (at the time of writing) support adding images to events out of the box. The only option is to extend the client library code, which we’ve managed to do successfully. Please leave a comment below if you want more information on that specific issue.

That’s all folks .. and whatever you do, don’t forget to like our Facebook Page – http://facebook.com/giglondon ;)

Tags: , , ,

This website uses IntenseDebate comments, but they are not currently loaded because either your browser doesn't support JavaScript, or they didn't load fast enough.

4 Responses to “Facebook Pages the Java way”

  1. tristan_b says:

    Is there a way to post to a Page's feed as the Page (instead of posting as a specific user)?

  2. tristan_b says:

    To answer my own question:
    client.stream_publish( "Snoopy is too cool for school", attachment, null, Long.valueOf(FACEBOOK_PAGE_ID), Long.valueOf(FACEBOOK_PAGE_ID));

  3. chico says:

    thx tristan, good to see you’ve worked it out so others can benefit, including me :)

  4. niteviva says:

    thx tristan, good to see that you worked out! now other people can benefit, including me :)

Leave a Reply