Introduction
This tip describes how to execute some Facebook tasks directly from C#. We will guide you through the development of a small Windows Forms application that will enable you to post pictures directly from your application to your Facebook wall.
Background
This article may be useful for intermediate developers who have some basics in C# and .NET.
Using the Code
Through this paragraph, we will explain how to develop a small Windows Forms application that will enable you to automate posting pictures directly from your application to your Facebook wall. You will have to follow two majors steps:
- The first thing you have to do is to create and register your application into Facebook.
- The second consists of writing some C# code based on the C# Facebook SDK.
Facebook Configuration
- Login to https://developers.facebook.com/apps.
- Click on the register button.
- Facebook will ask you to enter some information (Phone number, etc.).
- Once the registration is complete, you can click on the menu “Apps” => “Add new App”.
- Choose “Web site” and then click on “Create App ID”.
- The “App ID” will be used later on in the Config File in the C# project, in our case, we will create an application called “
CodeProject
”, we will use this application to develop a small utility that will enable us to post pictures on Facebook.
Once you complete all these steps, it’s time to write some code. :)
Working with Visual Studio
- Create a Windows Forms application.
- Add reference to Facebook.dll from here: https://github.com/facebook-csharp-sdk/facebook-csharp-sdk
- Add the Control “Web Browser” and name it “
webBrowser
”
- Open “App.config” and add these lines:
<appSettings>
<add key="ApplicationId" value="The Application ID" />
<add key="ApplicationUrl" value="" />
<add key="ApplicationSecret" value="The Application Secret" />
<add key="ExtendedPermissions" value="offline_access" />
</appSettings>
The application ID and the Application Secret are generated by Facebook “See section Facebook Configuration”.
On the main page “Main.cs”, on the load event of the Page, add these lines:
var destinationURL = String.Format(@"https://www.facebook.com/dialog/oauth?client_id={0}&
scope={1}&redirect_uri=http://www.facebook.com/connect/login_success.html&response_type=token",
this.ApplicationID , this.ExtendedPermissions);
webBrowser.Navigated += WebBrowserNavigated;
webBrowser.Navigate(destinationURL);
These lines define the Login URL used internally by Facebook. You have to construct this URL with the “applicationID
” and the “ExtendedPermissions
”. You can add their values directly into the source code or write two getters properties to get their values from the config file:
public string ApplicationId
{
get
{
return ConfigurationManager.AppSettings["ApplicationId"];
}
}
public string ExtendedPermissions
{
get
{
return ConfigurationManager.AppSettings["ExtendedPermissions"];
}
}
public string AppSecret
{
get
{
return ConfigurationManager.AppSettings["ApplicationSecret"];
}
}
Now, what remains is only writing code to publish pictures directly from the application to Facebook wall.
private void WebBrowserNavigated(object sender, WebBrowserNavigatedEventArgs e)
{
Dictionary<string, object> parameters = new Dictionary<string, object>();
FacebookMediaObject media = new FacebookMediaObject
{
FileName = "CodeProject",
ContentType = "image/jpeg"
};
byte[] img = File.ReadAllBytes("the Path of a picture ");
media.SetValue(img);
parameters.Add("source", media);
var url = e.Url.Fragment;
if (url.Contains("access_token") && url.Contains("#"))
{
url = (new Regex("#")).Replace(url, "?", 1);
this.AccessToken = System.Web.HttpUtility.ParseQueryString(url).Get("access_token");
var fb = new FacebookClient(this.AccessToken);
dynamic result = fb.Post("/me/photos", parameters);
var newPostId = result.id;
}
You find the source code of the application. You can download it but do not forget to update the web config file with your ApplicationID
and the ApplicationSecret
generated by Facebook.
Thank you for reading and do not hesitate to leave your questions and comments.