Friday, December 11, 2015

How to Use Facebook Connector with WSO2 ESB

The Facebook connector allows you to access the exposed Facebook Graph API from WSO2 ESB. Facebook is an online social networking website that allows registered users to create profiles, upload photos and videos, send messages, and keep in touch with their family, friends, and colleagues. The Graph API is the primary way to get data back and forth, from and to Facebook's social graph [1]
You can download the latest facebook connector from the store[2]

Prerequisite
  • Facebook Connector
  • WSO2 ESB 4.9.0  
  • Facebook App
You also need to enable following MessageFormatter and MessageBuilder in the axis2.xml file in the $ESB_Home/repository/conf/axis2
 <messageFormatter contentType="multipart/form-data"  
             class="org.wso2.carbon.relay.ExpandingMessageFormatter"/>  
 <messageBuilder contentType="multipart/form-data"  
             class="org.wso2.carbon.relay.BinaryRelayBuilder"/>  
By default these setting are commented on the axis2.xml file. You have to un-comment those 2.

Add Connector to WSO2 ESB
In order to install the connector to the ESB, you can go to the ESB management console page and click Add in the Connector section of the Manage menu. There you can select the facebook connector zip while which you have already downloaded and upload it.
When you successfully upload the connector, it will be available in the connectors list as below

By default connector will be in Disable status. You need to click on the Disable button in order to enable the connector.


Now you can create a simple proxy to invoke the Facebook operations via the connector. For that you can use the sample proxies that I've attached here.

To add the proxies, go to Add Proxy Services in the ESB management console. Click custom proxy.
Click switch to source view and paste the facebook proxy. Follow same steps to create another proxy for multipart operations.

Sample Operations
Here I've list down some basic facebook operations that I've tried via the connector. I've used postman REST client to call the proxies.
  • Get access token
Sample request
 curl -X POST -H "Action: urn:getAppAccessToken" -H "Content-Type: application/json" -H "Cache-Control: no-cache" -H "Postman-Token: 6f8c38a8-66e0-83d6-4641-1a33473e97d9" -d '{  
 "apiUrl":"https://graph.facebook.com",  
 "apiVersion":"v2.5",  
 "clientId":"<YOUR_CLIENT_ID>",  
 "clientSecret":"<YOUR_CLIENT_SECRET>"  
 }' 'http://localhost:8280/services/facebook'  

Sample Response
 {  
  "access_token": "940843329284937|8VL4m0zH8r0TP9lE5mB6IVlXIEY"  // This is the access token
 }  
  • Post a message to a Facebook Page
Sample Request
 curl -X POST -H "Action: urn:publishPagePost" -H "Content-Type: application/json" -H "Cache-Control: no-cache" -H "Postman-Token: 85eacc74-b6af-9171-96d0-d563ec078fb6" -d '{  
 "apiUrl":"https://graph.facebook.com",  
 "accessToken":"<YOUR_ACCESS_TOKEN>",  
 "pageId":"562960547187253",  
 "message":"This is a test message"  
 }' 'http://10.100.7.26:8280/services/facebook'  

Sample Response
 {  
  "id": "562960547187253_564344377048870"  // This is the post id
 }  

  • Post photo to a facebook page
There are 2 ways to post a photo to the facebook page. Either you can use an online photo or upload a photo.

Upload a Photo
Sample Request
 curl -X POST -H "Action: urn:addPhotoToPage" -H "Cache-Control: no-cache" 
-H "Postman-Token: 252bcb70-1459-4c36-88cd-eb0db33892ba" 
-H "Content-Type: multipart/form-data; boundary=----WebKitFormBoundary7MA4YWxkTrZu0gW" -F "source=@iumage.png" 'http://10.100.7.26:8280/services/multipart?apiUrl=https://graph.facebook.com&accessToken=<YOUR_ACCESS_TOKEN>&pageId=<PAGE_ID>&apiVersion=v2.5&caption=This is a sample uploaded photo'  
Sample Response
 {  
  "id": "114619305575881",  
  "post_id": "562960547187253_114619305575881"  
 }  

Use a online photo. Here we use photoUrl parameter to specify the online image url
Sample Request
 curl -X POST -H "Action: urn:addPhotoToPage" 
-H "Cache-Control: no-cache" 
-H "Postman-Token: 11e136d7-5f4d-102c-1520-d833ddb916f8" 
'http://10.100.7.26:8280/services/multipart?apiUrl=https://graph.facebook.com&accessToken=<ACCESS_TOKEN>&pageId=<PAGE_ID>&apiVersion=v2.5&caption=This is a sample uploaded photo&photoUrl=http://www.sweethome3d.com/blog/common/images/feed-icon-10x10.png'  


  • Post Video to a facebook page
Here again you have 2 options. Either you can use an online video or upload an local video.

Upload a video
Sample Request
 curl -X POST -H "Action: urn:addPageVideo" -H "Cache-Control: no-cache" 
-H "Postman-Token: e06bd9dd-48b3-6ada-99ac-2d4daf5dd46c" 
-H "Content-Type: multipart/form-data; boundary=----WebKitFormBoundary7MA4YWxkTrZu0gW" -F "source=@SampleVideo_1080x720_1mb.mp4" 'http://10.100.7.26:8280/services/multipart?apiUrl=https://graph.facebook.com&accessToken=<ACCESS_TOKEN>&pageId=<PAGE_ID>&apiVersion=v2.5&contentCategory=<CATEGORY>&title="Test video for upload"'  

Sample Response
{ "id": "114619305575881", }

Use Online Video to upload to page. Here we use fileUrl parameter to specify the online image url
 curl -X POST -H "Action: urn:addPageVideo" -H "Cache-Control: no-cache" 
-H "Postman-Token: 109e80dc-8b1a-049f-9c97-94277dd6beb6" 
'http://10.100.7.26:8280/services/multipart?apiUrl=https://graph.facebook.com&accessToken=<ACCESS_TOKEN>&pageId=<PAGE_ID&apiVersion=v2.5&contentCategory=<CATEGORY>&title="Test video for upload"&fileUrl=https://d3ldtt2c6t0t08.cloudfront.net/files/rhn4phpt3rh4u/2015/06/17/Z7EO2GVADLFBG6WVMKSD5IBOFI/main_OUTPUT.tmp.mp4'  


  • Get Likes of a Post
Sample Request
 curl -X POST -H "Action: urn:getLikes" -H "Content-Type: application/json" -H "Cache-Control: no-cache" -H "Postman-Token: 9770f6e5-de0f-c85e-097b-ebfac40765ed" -d '{  
 "apiUrl":"https://graph.facebook.com",  
 "apiVersion":"v2.5",  
 "accessToken":"<ACCESS_TOKEN>",  
 "resourceId":"<POST_ID>"  
 }' 'http://10.100.7.26:8280/services/facebook'  


  • Get Comments of a Post
 curl -X POST -H "Action: urn:getComments" -H "Content-Type: application/json" -H "Cache-Control: no-cache" -H "Postman-Token: f35f9ff4-b5cc-11a9-fea2-ba77ef2fd37e" -d '{  
 "apiUrl":"https://graph.facebook.com",  
 "apiVersion":"v2.5",  
 "accessToken":"<ACCESS_TOKEN>",  
 "objectId":"<POST_ID>"  
 }' 'http://10.100.7.26:8280/services/facebook'  

Facebook allow you to pass summary=true parameter to get the summary of the particular post. To use this feature you need to manually edit the connector and append the parameter to the request.

Edited facebook connector can be found at https://drive.google.com/open?id=0B-jZl7AY9hiiMTZWVVRSa0pIZFk
If you like to manually edit you can do the following.

  1. Download the Original connector from the wso2 store
  2. Unzip the connector
  3. Open facebook-likes/getLikes.xml file. Add following code segment before the <call> tag in the file

 <script language="js">  
       <![CDATA[  
       var params = mc.getPayloadJSON().summary;  
       if (params != null && params.toString().toLowerCase() === "true")  
       {  
         params = "true";  
       }  
       else  
       {  
         params = "false";  
       }  
       mc.setProperty('uri.var.summary', params);  
       ]]>  
     </script>  
and append &amp;summary={uri.var.summary} to the end of the uri-template value

You can follow the same steps to enable summary for comments as well. You need to do the same modifications in the facebook-comments/getComments.xml  file.
After the modifications, zip the connector again and redeploy the connector. You need to remove the connector first and then redeploy the new connector.

Then you can append summary=true parameter to getLikes and getCommnets functionalities and it will return response like below
 {  
  "data": [  
   {  
    "id": "108057549565823"  
   }  
  ],  
  "paging": {  
   "cursors": {  
    "after": "MTA4MDU3NTQ5NTY1ODIz",  
    "before": "MTA4MDU3NTQ5NTY1ODIz"  
   }  
  },  
  "summary": {  
   "total_count": 1,  
   "can_like": true,  
   "has_liked": false  
  }  
 }  
Note - In order to post messages you need to have publish_action and publish_pages permissions. You can assign these permission to the particular user through the facebook. Here I've used a test user which can be easily create by the facebook.

[1] - https://docs.wso2.com/display/ESBCONNECTORS/Facebook+Connector
[2] - https://storepreview.wso2.com/store/assets/esbconnector/b615c2a4-a4a6-48f0-bbb2-c516af0cdd37

Tuesday, December 1, 2015

Using Carbon Metrics with WSO2 Products

"WSO2 Carbon Metrics" provides an API for WSO2 Carbon Components to use Metrics library[1]
This has been pre installed in some of the latest wso2 products such as WSO2 MB 3.0.0, WSO2 ML and WSO2 DAS 3.0.0. Those products also extends the carbon-metrics and implemented added some new features to those products.
If you like you can install and try the carbon metrics in most of the WSO2 products. I've installed it with WSO2 ESB 4.9.0, WSO2 DSS 3.5.0, WSO2 AS 5.3.0 and WSO2 IS 5.0.0

You can easily install the feature through the ui. To install the carbon-metric feature in the product you can follow the below steps
  1. Use [2] to add features through the UI and use Wilkes repository [3]
  2. After adding the repository, deselect Group features by category and click Find features
  3. From the feature list select Metrics Group and install.
  4. Restart the product when the installation complete.
When the feature successfully installed you will see a Metrics menu item in the configuration menu. When you go to that item you will see as follows

At the moment current implementation doesn't supports multi tenancy or RBAC. Feature can be only use with super tenant and super tenant admin.

These information are stored in a H2 database. You can configure the datasource metrics-datasources.xml. Even you can use the same database for all the products. To do so you have to use the same datasource in the $Produc_Home/repository/conf/datasource/metrics-datasources.xml

By default carbon metric uses hostname as the source. Therefore if you are using same database for all the products you can un-comment the 
<Source>Carbon</Source> under JDBC section in the $Product_Home/repository/conf/metrics.xml and change source for each product

 <JDBC>  
       <Enabled>true</Enabled>  
       <!-- Source of Metrics, which will be used to  
         identify each metric in database -->  
       <!-- Commented to use the hostname  
         <Source>Carbon</Source>  
       -->  
       <!--  
         JNDI name of the data source to be used by the JDBC Reporter.  
         This data source should be defined in a *-datasources.xml  
         file in conf/datasources directory.  
       -->  
e.g. for esb <Source>WSO2ESB</Source>, for DSS <Source>WSO2DSS</Source>
 <JDBC>  
       <Enabled>true</Enabled>  
       <!-- Source of Metrics, which will be used to  
         identify each metric in database -->  
       <Source>WSO2ESB</Source>  
       <!--  
         JNDI name of the data source to be used by the JDBC Reporter.  
         This data source should be defined in a *-datasources.xml  
         file in conf/datasources directory.  
       -->  

[1] - https://github.com/wso2/carbon-metrics