Friday, September 20, 2013

Publish Facebook Posts Using C#

In this post I want to show, how to publish posts on the own Facebook page or the page of someone else (of course, only if this is allowed).

Therefor we create a new app in the developer app section. Then we provide ourself with an access token and set the right publish_action, after which we have to log on.
(Note: I recommend reading this new post regarding access tokens, since Facebook decreased their validity a lot.)
The app then quasi remembers our account, when executing it uses it and is able to do everything according to the obtained rights. The right publish_action describes, that the app may publish posts etc.
For publishing we have to use the HTTP POST method. The address, to which to send the data, is https://graph.facebook.com/useridentifier/feed/.
useridentifier thereby has to be replaced by the identifier of the target user, as for example ID or unique name. me is an abbreviation for the own account.
If we want to publish a post, we have to send the access token after the parameter access_token as well as the content of the post after the parameter message and then start a POST request.

The following sample code writes the text "Hallo Welt" on the own page:


            string URL;
            URL = "https://graph.facebook.com/me/feed/";

            WebRequest MyRequest;
            MyRequest = WebRequest.Create(URL);
            MyRequest.Method = "POST";

            Stream DataStream = MyRequest.GetRequestStream();
            Byte[] Data = Encoding.UTF8.GetBytes("access_token=xyz123&message=Hallo Welt");
            DataStream.Write(Data, 0, Data.Length);
            DataStream.Close();

            MyRequest.GetResponse();

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;
            }

Wednesday, September 18, 2013

Access to Facebook With C#

In the next time I want to post about how to access Facebook with C#. In contrast to the previous WhatsApp posts this connection is explicitely desired by Facebook, which is the reason why the documentation for and support of developers by Facebook is really excellent and highly recommended to read.

At the moment I have two posts in mind which will hopefully go online though, enjoy them. Now a short theoretical introduction, before I continue with the code in the next posts. Facebook offers many APIs for developers. For now we will use the simply but still powerful Graph API. Again here the advice, the documentation is very good and easy to understand, take a look at the link and maybe at the video.

The Graph API is an API which sends requests and commands via HTTP to Facebook, which means that we can access it very easy with HTTP Requests in C#.
The data of facebook are organized as a graph, there is the Graph Explorer with which these can be traversed. I want to present this shortly, since the implementation in C# is an exact copy of these graph actions.

In the Graph Explorer requests can be asked (GET) and changes be induced (POST).
All dates are nodes in the graph, as also the own account. Its ID is written by default in the input line, the request "123456789fields=id,name" then says, that the fields id and name of the node described by 123456789 (which is here the id) should be requested.
Only with this input line much can be done.

But important is also the term access token. This describes rights of single applications (apps). For the previous request we implicitely used the application Graph Explorer. But also own applications can be created, which can be done by the button "Create New App" in the app section of the development page.
Back in the Graph Explorer there is the button "Get Access Token", where the application can selected, for which the token is to be valid for.
When clicking on it a new window opens where rights can be selected, which the app should posses, as for example that it is allowed to post in the name of the user (for that and maybe other rights too a user has to be selected).

Important: I recommend reading this new post regarding access tokens, since Facebook decreased their validity a lot.

Kickoff for the English Version

I have just started to translate my German C# Blog into English. While doing so, I just translate every post 1:1 into the English language and also adopt the structure of the German version by setting the publish dates. So right now when I translate the posts and publish them here, they get really old dates, starting 2010. Know though that I am still active here right now and try to post often right now. To everyone, who prefers English over German, I wish an enjoyable read. As I am not a native speaker of English, I beg your pardon for possible mistakes.

Tuesday, September 17, 2013

HTTP GET and POST with C#

GET and POST are two different methods to send data to a HTTP server. In this post I briefly want to explain the differences between them and how to implement them in C#.
For GET the data is just appended to the URL, e.g. example.com/?field=id. A direct consequence is, that the length of these data is limited by the maximal URL length.
For POST a data block is transfered without the use of the URL.
Advantages of GET are now for example the clearness (which could also be interpreted as unsecureness though) and the little overhead, of POST the unlimited transfer size as well as that file upload is only doable with POST.

Both methods we can now use e.g. with a System.Net.WebRequest. For the following sample codes System.Net and System.IO have to be included.
In POST and GET we first create the request, note that here for GET the URL needs to contain already all data.

WebRequest MyRequest;
MyRequest = WebRequest.Create(URL);
MyRequest.Method = "POST"; // "GET" for GET
In the case of POST the request now has to be submitted, which can be done as follows:

Stream DataStream = MyRequest.GetRequestStream();
Byte[] Data = Encoding.UTF8.GetBytes("exampledata");
DataStream.Write(Data, 0, Data.Length);
DataStream.Close();
In both cases a possible answer of the server can be read-out:

            Stream ResponseStream;
            ResponseStream = MyRequest.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;
            }