Dan Scott
Dan Scott

Reputation: 51

Facebook C# SDK Fan Page Post From ASP.NET

I know this has been asked many times and I apologize if the answer is readily available but I have not been able to find the answer.

I'm using VS 2010 with the newest version of Facebook C# SDK. I installed it using the "Install-Package Facebook" command from my package manager in VS.

After spending quite a bit of time researching how to use the SDK, I am authenticating with FB through the Facebook Javascript SDK. Actually I followed the steps from the Getting Started Tutorial. And was quite successful in my endeavors. I now have a "Login" button on my aspx page, which brings up the login window and allows me to authenticate via that. I was also able to store my access token in a session variable and have it for use.

I have tested this by doing a Get on my FacebookClient object that I created with my access token and was able to retrieve data about the Fan Page.

So having said all this, in order to let you know that I have had some success. I was ready to start posting to the Fan Page via my web site and found this link Simplest way to post to a facebook fan page's wall with C#!

I thought - great! This is exactly what I need. After copying the code from this post, I am now getting an "Unknown Type" on the ExpandoObject.

For the sake of convenience, here is the code that I copied.

if (Session["AccessToken"] != null)
{
    var accessToken = Session["AccessToken"].ToString();

    dynamic messagePost = new ExpandoObject();
    messagePost.access_token = "[YOUR_ACCESS_TOKEN]";
    messagePost.picture = "[A_PICTURE]";
    messagePost.link = "[SOME_LINK]";
    messagePost.name = "[SOME_NAME]";
    messagePost.caption = "{*actor*} " + "[YOUR_MESSAGE]"; //<---{*actor*} is the user (i.e.: Aaron)
    messagePost.description = "[SOME_DESCRIPTION]";

    FacebookClient app = new FacebookClient(accessToken);

    app.Post("/" + [PAGE ID] + "/feed", messagePost);
}

So my questions are really about the ExpandoObject. Is this object still in the SDK? If so, what am I missing and if not, how should I be making this call?

And while I'm asking, is there something else I need to do when requesting the access token in order to get the proper permissions to post on the Fan Page? As stated above, I followed the Getting Started page on Facebook C# SDK Site to get my access token. The reason I ask is because throughout my research I have been coming across different posts talking about making sure you have the proper permissions in order to do different things in Facebook.

Any help is greatly appreciated, Dan

Upvotes: 1

Views: 3107

Answers (1)

Dan Scott
Dan Scott

Reputation: 51

I guess I just needed to do a little more poking around in StackOverflow to find my answer. I will go ahead and post it here for anyone else that might be having the same issue.

As it turns out with the latest version of Facebook C# SDK, the way to post anything is by using the FacebookClient object and then calling the Post method on that object. Pretty easy actually. Here is the code that I used.

if (Session["AccessToken"] != null)
{
    var accessToken = Session["AccessToken"].ToString();
    FacebookClient app = new FacebookClient(accessToken);

    dynamic result = app.Post("/[ID]/feed", new Dictionary<string, object> { { "message", "This Post was made from my website" } });
}

Session["AccessToken"] is my authentication token that was obtained from the user logging into Facebook on the web page, and [ID] is the Id of my Fan Page.

Upvotes: 2

Related Questions