This tutorial is the second in a series of blog posts that explain how to integrate Mule and Social Media.
What is Facebook ?
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
Mule Server and Studio versions
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.
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.
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.
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
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.
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.
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:
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.