This tutorial is the second in a series of blog posts that explain how to integrate Mule and Social Media.

Today’s post will focus on connecting to Facebook and updating your status on .
Other posts to expect will integratie Mule and:

What is Facebook ?

http://www.facebook.com
I don’t think Facebook needs an introduction, but anyway.

Facebook is a social networking service and website launched in February 2004, owned and operated by Facebook, Inc. As of May 2012, Facebook has over 900 million active users, more than half of them using Facebook on a mobile device. Users must register before using the site, after which they may create a personal profile, add other users as friends, and exchange messages, including automatic notifications when they update their profile

Source: Wikipedia

This article describes how easy it is to integrate with the facebook api using Mule. The facebook cloud connector is deliverd by Muleforge. The connector is still in snapshot.

Mule Server and Studio versions

For this example I used Mule Studio 1.0.0 and Mule Standalone version 3.2.1. The version of the facebook cloud connector when creating this post was 2.1-SNAPSHOT.

Facebook Authenitcation

The authentication is OAuth2. The same authentication techniques as used with Linkedin. This is HTTP authentication which means that the first time you use the integration you will be redirected to the facebooks pages asking you to give permissions to the application to post messages on your feed. However, you can store the authentication information into a database (or a file, or an objectstore, …) so that the next time you need authentication, Mule will read the authentication information from that datasource and will not be redirected to the Facebook authentication page.

If you don’t want to store the authentication information you don’t have to. The authentication information is stored in memory (session) by default. But once your restart your mule server you have to authenticate again.

Sources

All the sources I used in this post can be found on Github.I will only be posing parts of the code here, just to explain you the details. You can get the full sources on Github.

The Mule flow

I have created 2 Mule flows for this. One for the authentication process and one for the publishing process. Keep on reading to get all the details.

Configuring facebook

Before we start developing something we need to configure our facebook account. These are the different steps:

Step1: Goto http://developers.facebook.com and click on the “Apps” link.

Step2: Create a new application:

 Step3: Create the application

Fill in the new app name, in this example I left the namespace field blank. It is possible that Facebook will ask you to validate the account. You will receive a validation code through text messages.

Step4: Configure the app.

After the account has been verified you should end up with a screen like the above:

3 items are important on this screen:

    • App ID
    • APP Secret
    • Site url

These items you will need to configure in your mule flow file. You can create other applications if you want but for now we will just use this one.

Step5:

If you want you can configure a whole lot more things on this screen, but this is basically enough to get you up and running with Mule integrations.

The Mule project !

Allright, so far we have just configured our Facebook account to accept integration calls from Mule. Now lets start creating the Mule flow. Actually,  you only need a few lines of XML to get it up and running.

No before we dive into the Mule flow, remember that I have talked about authentication and storing the authetnication details into a datasource ?

If you don’t want the store the authentication information you can just skip this part.
The first part is about saving the access token while the second part is about restoring the access token.

Let’s look at the xml:

Storing the authentication details

This configuration will make sure that the authentication information is stored into a database, you can also use a file or any other datasource if you want. The query uses some expression language syntax to get the authentication information from an object that is on my payload. The FacebookTransformer makes sure that this bean that holds the authentication informaiton is put on the payload. There are also other options to get the authentication information.

Restoring (loading) the authentication details

<facebook:oauth-restore-access-token>
  <enricher target="#[header:headerOAuthAccessToken]">
    <jdbc:outbound-endpoint name="loadAccessToken" exchange-pattern="request-response" queryKey="SELECT_TOKEN">
       <jdbc:query key="SELECT_TOKEN" value="select token,secret from facebook where id='1';"/>
    </jdbc:outbound-endpoint>
  </enricher>
  <component class="be.contribute.service.JdbcRowComponent" />
  <message-properties-transformer scope="invocation">
    <add-message-property key="OAuthAccessToken" value="#[groovy:payload['token']]" />
    <add-message-property key="OAuthAccessTokenSecret" value="#[groovy:payload['secret']]" />
  </message-properties-transformer>
</facebook:oauth-restore-access-token> 

The above configuration is about restoring the authenticaiton details. In short this means that every request send to our mule flow will first load the authentication information from the database and run the query. In the example you can see that I have hardcoded my where clause, you should however change this into something more meaningfull otherwise you will be selecting the same authentication over and over again.
After the query has been run I have used a Component to convert the resultset returned by the authentication query into an object. It’s better to use an implementation class of the ResultSetHandler. After the component I used a message property transformer to put the properties with the correct name on the message.
Maven support

I have build the application with maven. You just have to add a dependency to the Facebook cloud connector and also to the correct driver of the datasource. In my example I have used a mysql database.

Mule Studio

I have developed this process using Mule Studio. I find this a very good tool to on one hand visualize the process and on the other hand write the xml by hand by using the code completion. You can very easy switch between the xml layout and the design layout.

The XML you can find in the GitHub project.

 

 

 

 

 

 

Testing the application

All that is left is to build the application through maven and deploy it to your local Mule installation. If you are developing in MuleStudio you can also right click on the flow file and start an embedded Mule Server. You have to add the additional .jars into your project.

The first step is to authenticate the application, you can do this by hitting the following URL: http://localhost:8083/authorize

You cannot use curl for this but you will have to run the URL in a browser. If everything goes well you should be redirected to the facebook authentication page and you should click accept.

At this time a record should also be inserted into your database. The next step is to publish a message by hitting the following URL:

http://localhost:8083/addpost

This URL will add the message as configured in our XML flow. It would be better of course to pass this value as a parameter to your mule flow.

If you publish the same message twice to your facebook feed, you will get an exception in your flow.

This is another guest post from Mule community member Tom Stroobants. Thank you Tom! (we’ll be sending you some more swag).  If anyone else in the Mule community would like to write a guest post, please email us.

No related posts.


7 Responses to “Mule School: Integration with Social Media: Part II – Facebook”

Lupita October 18th, 2012, 10:48 am

I’m using Mule Studio CE 1.3.1 and Mule Standalone 3.3.0.
Some exceptions are thrown where the groovy expression is accessed. I also tried #[groovy:payload.token] with no success.
Could you please help me?

Dinosaur November 23rd, 2012, 8:14 am

getting same error at the message:

aztral December 6th, 2012, 1:00 am

This tutorial doesnt work….. I have downloaded the project from git. Im using mulestudio 1.3.1

“required attribute name is not defined in message-properties-transformer,” is what error i get.

Tom Stroobants December 7th, 2012, 12:44 am

Hello Aztral,

I made this example in an earlier version of Mule Studio. I will check this in the newer version and change the example.

Best regards,

Tom.

aztral December 10th, 2012, 12:52 am

Yes please.. need this example for exam. So far i have fixed the erros by giving message-properties-transformer a name. (name=”test”> ) However i get many errors when i try to run the project.

Error creating bean with name ‘ref:config1.19′ is the first error of many.

StudentThor December 10th, 2012, 1:13 am

How do I know how to configure the database? I don’t know which columns etc to create/have? Please reply. Thanks.

Mule up your holiday’s pics: from Facebook to Box | MuleSoft Blog December 26th, 2012, 10:31 am

[...] So, we’ll need to consume both the facebook API and the Box API. This requires to register new applications on both services, get api keys, etc… I will not go over these topics in this post since they have already been covered previously. There’s a great post by Tom Stroobants about how to set a facebook application and connecting to it using Mule’s Facebook cloud connector. If you are familiar with it you may continue reading, otherwise I suggest to take a look at this post. [...]

Leave a Comment