Thursday, September 19, 2013

Read Data From Facebook

In this post I want to show how the requests from the Graph Explorer, which were introduced in the previous post, can be implemented in C#.
As already mentioned this is not hard but just a simple realization of the requests as HTTP GET requests.

In this post I want to exemplary show, how to read ID, name and gender of an arbitrary user (as far as this information is public).
In the Graph Explorer this is done by the request useridentifier?fields=id,name,gender. This we can copy in C#, we just have to know the URL to which to send this request, which is http://graph.facebook.com. As for any GET request we then set the desired parameters in the URL. The following program code returns ID, name and gender of the Facebook founder Marc Zuckerberg (reachable under the user name zuck) - the principle I explained already in the linked posts:

            string RequestURL;
            RequestURL = "http://graph.facebook.com/zuck?fields=id,name,gender";

            WebRequest GetRequest;
            GetRequest = WebRequest.Create(RequestURL);

            Stream ResponseStream;
            ResponseStream = GetRequest.GetResponse().GetResponseStream();
            StreamReader ResponseReader = new StreamReader(ResponseStream);

            string TempLine = "";
            int i = 0;
            string Result = "";
            while (TempLine != null)
            {
                i++;
                TempLine = ResponseReader.ReadLine();
                if (TempLine != null)
                    Result += TempLine;
            }

No comments:

Post a Comment