Integrating Facebook with Mule ESB
Mule ESB provides an amazing number of cloud connectors and it is the only middleware product to support such a wide variety. Let’s see how easily we can integrate with Facebook using Mule.
And what do we need to integrate with Facebook?
- A Facebook account
- Facebook access tokens from http://developers.facebook.com
- The Mule Facebook connector
- And obviously the Mule Studio with its embedded Mule ESB
That’s it. Let’s get on with it…
If you have a Facebook account you can login to developers.facebook.com with the same credentials. After you login toggle to the ‘Apps’ tab and select ‘Create new app ‘. You should be able to see a window similar to the snapshot in Figure 1.
You should have a Facebook application through which Mule can communicate with Facebook. Provide an application name and submit the captcha verification and you should be able to view your application dashboard. Here you can collect your API ID and API SECRET which are the access tokens to authenticate your Mule ESB application.
Figure 2 The Mule application developer’s access tokens
We are done with Facebook. Now let’s configure Mule.
The current version of Mule Studio doesn’t have the Facebook connector built in like Twitter, Salesforce, twilio . But Mule ESB Server supports it, so we need to manually add the connector to Mule Studio. (That is, you cannot find the Facebook icon in Mule Studio)
Create a new Mule Project and Open the flow configuration editor. Add the Facebook schema references to the exiting Mule code. Now your new configuration should typically look like this:
<code><mule xmlns="http://www.mulesoft.org/schema/mule/core" xmlns:doc="http://www.mulesoft.org/schema/mule/documentation" <strong><span style="color: #ff0000;">xmlns:facebook="http://www.mulesoft.org/schema/mule/facebook"</span></strong> xmlns:spring="http://www.springframework.org/schema/beans" version="EE-3.2.2" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd http://www.mulesoft.org/schema/mule/core http://www.mulesoft.org/schema/mule/core/current/mule.xsd<strong><span style="color: #ff0000;"> http://www.mulesoft.org/schema/mule/facebook http://www.mulesoft.org/schema/mule/facebook/current/mule-facebook.xsd</span></strong>"> </code>
Let’s download and add the Mule Facebook connector to the build path. Download the Mule-Facebook-Module from the Mule Module Snapshot Repository. Download the jar file from the latest build, which is currently 2.0.1-SNAPSHOT. Next right click the Mule project you created and select ‘ Build path -> Configure Build path ‘. Select “Add external JARs” from the Libraries tab and locate the jar file you downloaded.
Now you’re all set for creating the Mule flow. Let’s post an update to your Facebook wall. You need to find out the unique id of your Facebook profile. You can find it using this link http://apps.facebook.com/wimfbpid/ (Make sure you are logged in to your Facebook account).
Step 1: Configure the Facebook connector with the App ID and App Secret received from Facebook. Add the following code to your mule flow.
<code><facebook:config name="FB_Connector" appId="App_ID" appSecret="App_Secret" /></code>
Step 2: Create an HTTP endpoint to initiate the mule flow
<code><http:inbound-endpoint exchange-pattern="request-response" host="localhost" port="8081" path="wall" doc:name="HTTP"/></code>
Step 3: Use facebook:publish-message to publish the publish the update to the wall
<code><facebook:publish-message config-ref=”FB_Connector” profile_id="Profile_ID" msg="#[header:INBOUND:update]"></code>
Mule Facebook Connector provides many operations to connect with Facebook. The list of operations is available at:
Your complete flow configuration should look like the code below: