Posted by Anuraj on Monday, July 22, 2013
Long back I wrote a post about uploading files to Sky Drive from C# Winform application. This post is about uploading an image to Facebook using C# without any 3rd party library.
As you may know, Facebook does support OAuth2.0 for authentication. Unlike web applications, for desktop applications can directly get the access token, which required to upload the file to Facebook. For authentication, you need to create a Facebook Application. You can create it from Facebook Developer page - https://developers.facebook.com/apps. You can create an application using Create New App button.
And you can get the App ID/API Key and App Secret, by clicking on the App details page.
Here is the implementation, for displaying authentication dialog, Web Browser control is used, with Dock = Fill property. Here is the code.
The above code will display an authentication dialog like this.
Once the user authenticated successfully, Facebook will display an authorization dialog like this, which will inform user about the permissions the app is going to use.
Once user authorized the application successfully, the form will close, with dialog result OK, also the Form exposes the AuthToken property, which used to upload the File. And here is the code which helps to upload the File.
The response is JSON data, the ID of the image uploaded. And here is the sample image, uploaded by the application. The message query string value is used for image description.
You can post the photo to selected album, currently it is posting an album with the application name.