integration with facebook using mule esb
TRANSCRIPT
Integration with Facebook using Mule ESB
Facebook is a social networking website launched in February 2004. A user must register before they can use Facebook. After registering users can create a user profile, add other users as friends, exchange messages, post status updates and photos, share videos and receive notifications when others update their profiles. In this blog post, a step by step process of integrating with Facebook using Mule ESB Facebook connector is shown.
Overview
In this blog post, we are using◦ Facebook Connector 2.3.4◦ Anypoint Studio◦ Mule ESB Server 3.5.2
To use this application in the project, we will need a Facebook account for testing
We need to create a new Facebook app under Facebook developer apps to be able to communicate with the Facebook account from Mule ESB application
We will be using OAuth2 authentication to connect to Facebook using Mule ESB Facebook Connector
Versions and Assumptions
Go to https://developers.facebook.com/ Click on My Apps Click on Create a New App button Add a unique display name, namespace and
select a category Click on Create App Id Click on Show button and make a note of
the App ID and App Secret. The App ID and App Secret will be used later in Mule Facebook Connector configuration
Configuring Facebook App
A sample screen shot for the Facebook app is given below.
Create a new Mule Application Click on Global Elements tab in your main flow Click on Create Button Select Facebook under connector configuration group Add Facebook App ID value to consumer key text box Add Facebook secret key value to consumer secret
text box Leave the scope text box empty. The default value for
scope is “email,read_stream,publish_stream” which covers most of the access permissions
Click on Oauth tab and specify domain, local port and remote port values.
Configuring Facebook Connector
A sample screen shot and XML configuration is given below.
<facebook:config-with-oauth name="Facebook"
consumerKey="${ConsumerKey}" con-sumerSecret="${ConsumerSecret}"
doc:name="Facebook"> <facebook:oauth-callback-config
domain="localhost" localPort="1100" remotePort="1100" /> </facebook:config-with-oauth>
Configure a flow as shown below
Mule Flow for Facebook Authentication
<flow name="AutorizeFacebook" doc:name="AutorizeFacebook"> <http:inbound-endpoint exchange-pattern="request-response" host="localhost" port="5555" doc:name="HTTP"
path="facebookupdate" /> <facebook:authorize config-ref="Facebook" doc:name="Authorize" /> <choice doc:name="Choice"> <when expression="flowVars.OAuthAccessTokenId != null"> <logger message="Authorization Successful" level="INFO" doc:name="Logger" /> <set-payload value="Facebook Authorization Successful" doc:name="Set Payload" /> </when> <otherwise> <logger message="Authorization Failed" level="INFO"
doc:name="Logger" /> <set-payload value="Facebook Authorization Failed" doc:name="Set Payload" /> </otherwise> </choice> </flow>
If the app authorizes in to Facebook successfully, the connector returns two flow variables to Mule ESB flow which are “_oauthVerifier” and “OAuthAccessTokenId”
Using Choice router, we can check whether the flow received an “OAuthAccessTokenId” or not and based on the response we would be able to determine the success or failure of the Facebook authorization.
Deploy the app Hit the following URL on the browser http://localhost:1111/facebookauthorize Facebook will ask us to authorize the new app to
connect to Facebook. This authorization will be needed only on the first time when running the application.
Click on Okay button and we should see a message on the browser as “Facebook Authorization Successful”
In order to publish or post a message on Facebook, we will need to submit the app for review and request for “publish_stream” access on developer app.
Testing and Observation