Mule ESB 3.6
Integrating with Facebook using Mule ESB Facebook ConnectorPrepared By: Rupesh SinhaOverviewWhat is Facebook?Versions and AssumptionsConfiguring FacebookConfiguring Facebook ConnectorMule Flow for Facebook AuthenticationMule Flow for retrieving logged in user detailsMule Flow for publishing message in FacebookConclusion
What is Facebook?Facebook is a social networking website launched in February 2004A user must register before they can use FacebookAfter registering users can create auser profile, add other users asfriends, exchange messages, post status updates and photos, share videos and receive notifications when others update their profilesAs of February 2015 Facebook reached a market capitalization of $212 BillionFacebook is currently the most famous social networking siteVersions and AssumptionsIn this presentation, we are using Facebook Connector 2.3.4Any point StudioMule ESB Server 3.5.2To use this application in your project, you will need a Facebook account for testingYou will need to create a new Facebook app under Facebook developer apps to be able to communicate with your Facebook account from Mule ESB applicationWe will be using OAuth2 authentication to connect to Facebook using Mule ESB Facebook ConnectorConfiguring FacebookGo to https://developers.facebook.com/Click on My Apps
Click on Create a New App button and you should be given a screen as shown below
Add a unique display name, namespace and select a category.Click on Create App Id. You should get a screen similar to one shown below
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 ConnectorCreate a new Mule ApplicationClick on Global Elements tab in your main flowClick on Create ButtonSelect Facebook under Connector Configuration groupAdd your Facebook App ID value to Consumer Key text boxAdd your Facebook Secret Key value to Consumer Secret text boxLeave the scope text box empty. The default value for scope is email,read_stream,publish_stream which covers most of the access permissionsClick on Oauth tab and specify domain, local port and remote port values.A screen shot and sample XML configuration is shown in the next slide
Mule Flow for Facebook AuthenticationConfigure a flow as shown in the below picture
The XML configuration for this flow is given in the next slide
If the app authorizes to Facebook app successfully, the connector returns two flow variables to Mule ESB flow which are _oauthVerifier and OAuthAccessTokenIdUsing Choice router, we are checking weather the flow received an OAuthAccessTokenId or not and based on the response we are determining the success or failure of the Facebook authorization
Deploy your appHit the following URL on the browserhttp://localhost:1111/facebookauthorizeFacebook will ask you to authorize your new app to connect to Facebook as shown below. Below screen will be prompted only first time when you run the app. Latest execution of your app will not prompt for approval.
Click on Okay button and you should see a message on the browser as Facebook Authorization SuccessfulIn order to publish or post a message on Facebook, you would need to submit the app for review and request for publish_stream access on developer apps.
Mule Flow for retrieving logged in user detailsLets extend the previous authorize flow and use the authentication token to retrieve the logged in user details from Facebook. Change your Mule flow as per the given flow picture
The XML configuration for the new flow will be as shown below
In this flow, we have used another operation on Facebook connector to retrieve the logged in user details and print them on the browser using Object to JSON transformerDeploy your appHit the following URL on the browserhttp://localhost:1111/facebookauthorizeYou should now see the logged in user details printed on your browser in JSON format
Mule Flow for publishing message in FacebookLets extend the previous authorize flow and use the authentication token and user details to publish a message on users wall. Change your Mule flow as per the given flow picture
The XML configuration for the new flow is given in the next slide
Deploy your appHit the following URL on the browserhttp://localhost:1111/facebookauthorizeYou should now see a message on your browser as Message posted on your wall.Check your Facebook wall and you should see a message published on your wall as This is a greetings from Mule ESB appPlease note that Facebook does not allow spam. Therefore, if you try posting same message twice in a short period of time, an exception will be thrown.
ConclusionMule ESB Facebook connector provides an easy way to connect to Facebook using Facebook OAuth2 authentication and allows performing several operationsThe latest Facebook connector is simpler to use compared to the previous version of Facebook connectorsFacebook Connector is available for Mule ESB community edition
Thanks for watching