NetMe Integration with Facebook


How NetMe is interacting with Facebook


 NetMe a BigData analytics tool is based on analysis of tweets posted in Twitter and posts published in Facebook. NetMe was created with an intention to give one single platform to the viewer to view all latest discussion corresponding to a keyword. In the current version of NetMe we are giving the ability of searching a particular keyword in Twitter or Facebook. We are enabling user to see all the discussion pertaining to his LinkedIn profile, and most importantly, out of thousands of daily news we are recommending user to read most popular news which are getting discussed in the social platforms.

Why Facebook interaction is needed? 


Facebook is getting adopted crazily. Its exponential popularity is making it a favorite platform to interact personal and professional related messages. I feel Facebook did a paradigm shift after introducing the page feature in its platform. Now companies are including this feature in their marketing checklist. As NetMe is tracking cutting edge technologies, it became inevitable for us to consider the interaction with Facebook.
NetMe interaction with Facebook was focused on Search and Fetch Data features. We also have an offline service to publish post automatically to Facebook. We did it for http://www.facebook.com/CloudBazaar . After a few customizations, NetMe will have this service under its subscription plan.
NetMe Facebook Search had added the value to the website. We are recording users tracking movies, politicians etc. through the search feature. The posts are fetched not pertaining to one’s individual account instead it does a global search for that keyword.

What APIs NetMe is using for this? 


NetMe is using Facebook Graph API to fetch and post data from Facebook platform. Facebook Graph API is a REST based OAUTH authentication API which enables developer to query different apis on a https secure channel.

How NetMe is using Facebook Graph API? 


NetMe is hosting 5 Azure Mobile Services for its features. One of the Azure Mobile Service has been allocated to data fetched from Facebook.

Step by Step approach to implement a Facebook Graph API implementation through Node.JS in Azure Mobile Service

1.       Create an app for Facebook. Go to https://developers.facebook.com/apps


After successful creation of the app, the administrator will get an App ID and an App Secret. We will have to make a note of these 2 keys which will help us to get the access token. 

2.       Click on Tools from the Top Menu Bar. Click on Graph API Explorer link

 
3.       In the Graph API Explorer, change the Application name to the previously created app and click on Get Access Token.




4.       On clicking the Get Access Token a popup will appear which will ask you to give permissions. For our use NetMe requires

a.       publish_actions under “User Data Permissions”,

b.      status_update, manage_pages, share_item, publish_stream under “Extended Permissions”

Click “Get Access Token” button



5.       Challenge: I faced my first challenge after this. On clicking Get Access Token, I got an Access Token which I thought can be directly implemented in my http request to get the desired response. Access_Token are required with every API request because this token decides what permission should be given to the app. I was successful in getting the response in first one hour, but after 1 hour it started throwing error. This was annoying because prior one hour everything was fine. Then I figured out that this access_token given is a short term access token.  

Earlier Facebook use to give a never die access token, which developers used as static variables in their API request, but they have stopped that service. After few googling I figured out that there was a way to get a long term access token. This long term access token is valid till 60 days, after which the developer has to again renew the access token.
 
Node.JS Script to request a long term access token

var req = require('request');             

        req.get({

                 method: 'GET',

                 url: 'https://graph.facebook.com/oauth/access_token?client_id=<<app_id>>&client_secret=<<app_secret>&grant_type=fb_exchange_token&fb_exchange_token=<<short term access token>>,

                 },function(err_access, resp_access, body_access){

                     req.get({

                           method: 'GET',

                            url: 'https://graph.facebook.com/search?q='+request.parameters.topic+'&type=post&'+body_access,

                           },function(err_getFeed, resp_getFeed, body_getFeed){                               

                               var body = JSON.parse(body_getFeed);

                               console.log(body);                              

                              request.respond(200,body);

                 });
                 });   

Above is a Node.JS script implemented in Azure Mobile Service which queries for a long term access token with help of app id, app secret and short term access token and uses the same to do a Facebook Graph API Search query.

Note next time if this script is run with the same short term access token, it will give the same long term access token. The life span of this will continue till 60 days. 

6.       Following is the AMS script snapshot that is running for Facebook Search query


7.       To call this script from client side I wrote 2 lines of code in the aspx page

HelperClass _helperClass = new HelperClass("https://facebookpost.azure-mobile.net/tables/facebookcompute", <<Azure Mobile Service Key>>, Guid.NewGuid().ToString());

                string response = _helperClass.ReadFacebookPost(topic);

Note in the above call with the service url you also need to send the key associated with the service. This key you will get at Azure Mobile Service portal by clicking on the service and then clicking on manage keys option.




 
8.       I have clubbed all the Netme API request modules in a class called Helper Class
Search Facebook code in the Helper class
public string ReadFacebookPost(string topic)

        {

            string key = String.Empty;

            string fullUrl = _baseurl+"?topic="+topic ;

            string tweet = String.Empty;

            char[] arr = new char[] { '\"', ',', ' ' };

            List<string> tweets = new List<string>();

            try

            {

                HttpWebRequest _azureRequest = WebRequest.Create(fullUrl) as HttpWebRequest;

                _azureRequest.AllowWriteStreamBuffering = false;

                _azureRequest.Method = "GET";

               

                _azureRequest.Headers = new WebHeaderCollection{

                    {"X-ZUMO-APPLICATION", Guid.NewGuid().ToString()},

                    {"X-ZUMO-MASTER", _appKey},

                };

                using (HttpWebResponse response = (HttpWebResponse)_azureRequest.GetResponse())

                {

                    Stream stream = response.GetResponseStream();

                    using (StreamReader readStream = new StreamReader(stream, Encoding.UTF8))

                    {

                        key = readStream.ReadToEnd();

                        key = key.TrimStart(arr);

                        key = key.TrimEnd(arr);

                    }

                }

            }

            catch (WebException ex)

            {

                using (WebResponse response = ex.Response)

                {

                    HttpWebResponse httpResponse = (HttpWebResponse)response;

                    using (Stream data = response.GetResponseStream())

                    {

                        string text = new StreamReader(data).ReadToEnd();

                    }

                }

            }

            return key;

        }

 

Here I am creating the API request using the service url and sending the search topic as a GET parameter. One important thing here is you need to mention X-ZUMO-APPLICATION and X-ZUMO-MASTER.

 

X-ZUMO-APPLICATION could be any GUID but X-ZUMO-MASTER will have to be the Azure Mobile Service key.

Challenge: Challenges never cease do they? J

Here comes the second challenge. Once all this is done and I executed the code. I was getting nothing when querying for request.parameters.topic at the server side. Ideally this should have the search parameter with it. Now came the second glitch. Apart from X-ZUMO APPLICATION and X-ZUMO-MASTER there is another parameter that has to be provided in the header. It is X-ZUMO-APPKEY. Now this APP KEY is different from Azure Mobile Service Key, and it is complex to retrieve. One can use a proxy such as Burp or Paros to retrieve this APP key.

An easy way to overcome this challenge was go to the portal and click on Azure mobile Service. Go to the Data Tab and click on Permissions. Edit the permission to “Everyone” where you need to execute the script without any App Key.



That’s it.
Our Facebook Graph integration is done. The response is in a JSON format which we are parsing and showing it on our page.
Try searching for some text in NetMe. One of my favorite category is movie and often I search for movie reviews with request such as this http://netme.cloudapp.net/SearchFBPost.aspx?topic=diehard5

     

J J

Comments

Popular posts from this blog

Firebase authentication with Ionic creator

Big Data - SWOT Analysis

LINKEDIN api call using NODE.JS OAUTH module