Accessing Facebook API using C#

Another quick video tutorial from me. Here, we will focus on implementing our own “SDK” responsible for handling the Facebook Graph API using C# and .NET Core (of course you can achieve the same result on the full .NET platform).


If you’re just like me targeting the .NET Core, most likely you realized that Facebook SDK for .NET is not really being supported, at least currently, based on the latest date of the master branch update. And of course the same does apply to the NuGet packages.

Thus, we’re kinda on our own, however, it turns out that accessing the Graph API is quite easy. All we need to do is to make use of the HttpClient or any other type that is responsible for handling the HTTP requests. Let’s jump right into the code:

The Account basically represents the user account in the Facebook. Please note that you may include there as many more properties as you wish.

The FacebookClient is responsible for handling the POST and GET requests to the Facebook API. Quite straightforward code, no magic here, just keep in mind to add a reference to the Newtonsoft.Json package.

Our FacebookService is where the true “business logic” lays in. It does make use of the underlying FacebookClient in order to execute the HTTP requests to the specified endpoints using some additional arguments.

And finally, our actual application makes use of the FacebookService in order to get our Facebook account and post a message onto our wall. Just remember to get your access token here and mark the needed permissions. And as you may have already noticed, it’s a trivial console application, therefore the Task.WaitAll() is being invoked.

It wasn’t that difficult after all, was it? You can download the source code of this sample application by clicking here.

23 Comments Accessing Facebook API using C#

  1. Pingback: Accessing Facebook API using C# (video tutorial + article) - How to Code .NET

  2. Pingback: Dew Drop - February 6, 2017 (#2415) - Morning Dew

  3. Pingback: Compelling Sunday – 16 Posts on Programming and QA

  4. Pingback: Dew Drop – February 6, 2017 (#2415) (by Alvin A.) -

  5. Jesper Urban

    Hi Piotr,

    Nice to meet you and thanks for the nice blog. We are soon starting to make an facebook integration and I was wondering whether you would be interested in helping us on this task on a project basis?

    I am looking forward to hearing back from you.

    All the best
    Jesper Urban
    + 45 31 41 11 07

  6. Pham Dat

    Hi, thanks for the tutor.
    Can you tell me how to add this source code into 1 project 😀 😀 😀

  7. Pingback: Accessing Number of likes and comments per post on a fb page using C# and Facebook Graph API [on hold] - ExceptionsHub

  8. Garik Gevorgyan

    Hi Piotr, thanks for the tutor.
    I have question.Why You use FaceBook 7.0.6 NuGet package and why. In whis app you never use it.
    I uninstall this package and app continued to work properly.

    Thanks in advance

  9. Joy

    Thanks for your video, I can post text to my wall now.
    But do you know how to send messages to friends for fan page people?

    Thanks in advanced.

  10. Pingback: 2017 summary | Piotr Gankiewicz

  11. DiNguyen

    Hi Bro,

    Thank you so much for your great tutorial. It gives me the perfect basic step in Facebook Graph API.

  12. Tommy

    Thank you.

    I hope you can help.

    I want to create a photo album and upload photos to it.

    So far I have been able to create the photo album with this

    public async Task PostOnWallAsync(string accessToken, string message)
    => await _facebookClient.PostAsync(accessToken, “me/albums”, new {name=”Test album”});

    But now I am stuck, how do I upload photos to the album I created?

  13. Lindsay

    Great blog and code. All implements into Xamarin proj seamlessly.
    One issue now … once it gets to this:

    var response = await _httpClient.GetAsync($”{endpoint}?access_token={accessToken}”);

    It waits and waits and waits…

    Do you know if anything has change on the GB graph endpoint?
    Anything else to add / amend?

    I implemented as-is and already retrieve the access_token. Just want account details.


    1. BRENT

      change the method signature to async

      public async Task GetAsync(string accessToken, string endpoint, string args = null)

  14. omer

    thank you.

    I tried with VS .Net platform (not Core)
    I didn’t arrange project.json
    error : Your project.json doesn’t have a runtimes section. You should add ‘”runtimes”: { “win”: { } }’

    Can you help me?

    “frameworks”: {
    “net461”: {
    “runtimes”: { “win”: {} }

  15. Shubhang Sharma

    Hey Piotr,

    I have an app( website) registered for me and have app access token for it. Using graph explorer, I can create user access token, however as per facebook documentation, we shouldn’t hard code the user access token and should always go through their SDK or so.
    Any idea, if we can generate user access token programmatically using app access token?

  16. Rafael Vetrone

    Hello Piotr, your tutorial is good. But I’m having some trouble making it work. I created a ASPNET MVC 4.5.2 application. But HttpClient GetAsync never return! I read at stackoverflow some people having the same issues with MVC 4.5.2. I do these calls in a MvcController Index method. Perhaps I will change to using WebRequests for the REST calls.


Leave A Comment

Your email address will not be published. Required fields are marked *