Click here to Skip to main content
15,868,141 members
Articles / Programming Languages / C#

C# Application Integration with Facebook & Twitter with oAuth

Rate me:
Please Sign up or sign in to vote.
4.91/5 (41 votes)
9 May 2012Public Domain3 min read 293.3K   35.8K   81   52
This is a very basic level tutorial about authenticating with Facebook and Twitter and setting your status with it.

Introduction

This articles describes a simple tutorial explaining:

  1. Setting a Facebook status through a C# application
  2. Sending a Tweet through a C# application

Background

Last week i had to do a little proof of concept about integration of Twitter & Facebook with a C# application. All I had to do was post a simple status on users' Facebook wall or post a tweet. What I was surprised after considerable research was the lack of basic tutorials/resources that were available. I did not come across a simple "Hello World" example to get me started. So I had to go through some high level tutorials and extract a basic POC; which iam going to share with the community.

This is my first share, so i would appreciate any suggested improvements . Also I am very much open to any suggestion/corrections. I would love to learn more from some really good developers of the community.

Using the code

FACEBOOK:  

Let's start by first doing the Facebook part of this article. 

To set a status on Facebook using your C# application, first of all you will need to create an application by going to the following link:

https://developers.facebook.com/apps

  • Click on the CREATE NEW APP button.

Image 1

  • Fill in the required information and click Continue

Image 2

Image 3

  • Now your application has been created. There are 2 things that you need to take note of; The App ID & AppSecret. We will be using them later on.

  Image 4 

  • Now let us move on to the coding. Make a new Windows Forms Project. We will add a WebBrowser to our main form.
  • The App.config of your project should look something like this:

XML
<?xml version="1.0" encoding="utf-8"?>
<configuration>
  <appSettings>
    <add key="EnableSqlDependency" value="true" />
    <add key="ApplicationId" value="<YOUR APP ID GOES HERE>" />
    <add key="ApplicationUrl" value="" />
    <add key="ApiKey" value="" />
    <add key="ApplicationSecret" value="<YOUR APP SECRET GOES HERE>" />
    <add key="ExtendedPermissions" value="offline_access" />
  </appSettings>
  <startup>
    <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.0" />
  </startup>
</configuration>

  • Now we'll move on to authorizing the application. For this you'll have to login to your Facebook account when the application prompts. This will give you the access token that you would need to communicate. 
  • We'll be using the Facebook C# SDK (http://csharpsdk.org/) You can get it from the mentioned link
  • Now we are ready to post our status:

C#
var fb = new FacebookClient(this.AccessToken);
dynamic result = fb.Post("me/feed", new { message = "My second wall post using Facebook C# SDK" });

  • The complete project is attached with the article, All you have to do to make it work is to enter your App ID & App secret in the app.config.

Twitter

Now let us move on to sending a tweet on Twitter. For this purpose we will be using a wrapper name Tweetsharp. It is widely regarded as the best in the business when it comes to integrating your application with twitter. Get the library from https://github.com/danielcrenna/tweetsharp.

To send a Tweet using your C# application,  first of all you will need to create an application by going to the following link:

https://dev.twitter.com/

  • Click on Create an app
  • After signing in with your twitter account credentials, you would be taken to a form where you need to fill in required information in order to create the application.
  • After you are done creating your application, you will be taken to your application's page. You need to take note of the keys highlighted in the following figure:

Image 5

  • In case your Access token & Access Token Secret have not yet been generated, create them 1st. 
  • Now let us move on to the coding. Make a new Console Application.  
  • The App.config of your project should look something like this:

XML
<?xml version="1.0" encoding="utf-8"?>
<configuration>
  <appSettings>
    <add key="ConsumerKey" value="<YOUR CONSUMER KEY GOES HERE>" />
    <add key="ConsumerSecret" value="<YOUR CONSUMER SECRET GOES HERE>" />
    <add key="AccessToken" value="<YOUR ACCESS TOKEN GOES HERE>" />
    <add key="AccessTokenSecret" value="<ACCESS TOKEN SECRET GOES HERE>" />
  </appSettings>
  <startup>
    <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.0" />
  </startup>
  <runtime>
    <assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
      <dependentAssembly>
        <assemblyIdentity name="Newtonsoft.Json" 
            publicKeyToken="30ad4fe6b2a6aeed" culture="neutral" />
        <bindingRedirect oldVersion="0.0.0.0-4.5.0.0" newVersion="4.5.0.0" />
      </dependentAssembly>
    </assemblyBinding>
  </runtime>
</configuration>

  • Using the TweetSharp library, we'll simple send a tweet as follows:

C#
TwitterStatus twitterStatus = twitterService.SendTweet(tweetMessage);

Conclusion 

I would like to acknowledge the help that i took from the Facebook C# SDK Samples as well as the TweetSharp samples. I would love any input to this article to make it better. This is my first time, so dont be too hard on me Smile

License

This article, along with any associated source code and files, is licensed under A Public Domain dedication


Written By
Software Developer (Senior) Aurora Solutions (pvt) Ltd.
Pakistan Pakistan
When I was born, I was the 5,205,164,370th person alive on Earth & the 80,219,140,249th person to have lived since history began. Analogue at birth, digital by design. Leading the Trading Platforms teams at Aurora Solutions. A Manchester United aficionado. Always hungry. Extremely curious. Never idle. Usually found around my laptop, but when if you can’t find me check the football field or the cinema.

Comments and Discussions

 
Questionsmall change Pin
geodeath10-Jan-13 23:33
geodeath10-Jan-13 23:33 
AnswerRe: small change Pin
Niranjan Kumar Mahendralingam21-May-13 20:42
Niranjan Kumar Mahendralingam21-May-13 20:42 
QuestionNice Job Pin
Princess Dimaculangan5-Jan-13 14:58
Princess Dimaculangan5-Jan-13 14:58 
Questionauthentication Pin
audaijihad10-Dec-12 9:31
audaijihad10-Dec-12 9:31 
QuestionHow can we build something like this using .Net 3.5 Pin
P Srikanth Gautham3-Dec-12 0:24
P Srikanth Gautham3-Dec-12 0:24 
GeneralMy vote of 4 Pin
Burak Tunçbilek20-Sep-12 6:22
Burak Tunçbilek20-Sep-12 6:22 
GeneralRe: My vote of 4 Pin
Umer Aziz Malik24-Sep-12 23:22
Umer Aziz Malik24-Sep-12 23:22 
QuestionThis is very urgent Pin
anusha saxena17-Sep-12 20:04
anusha saxena17-Sep-12 20:04 
AnswerRe: This is very urgent Pin
Umer Aziz Malik24-Sep-12 23:23
Umer Aziz Malik24-Sep-12 23:23 
GeneralDisplay newsfeeds on my website Pin
NaniCh3-Sep-12 21:27
NaniCh3-Sep-12 21:27 
QuestionFor C# application authorize Pin
vishal katoch14-Aug-12 9:16
vishal katoch14-Aug-12 9:16 
AnswerRe: For C# application authorize Pin
Umer Aziz Malik15-Aug-12 0:31
Umer Aziz Malik15-Aug-12 0:31 
QuestionWhere are you using AppSecret? Pin
Alex_II26-Jul-12 13:08
Alex_II26-Jul-12 13:08 
AnswerRe: Where are you using AppSecret? Pin
Umer Aziz Malik29-Jul-12 22:35
Umer Aziz Malik29-Jul-12 22:35 
Questionhow can i login on our site using facebook username or id Pin
TusharBharambe22-Jul-12 20:52
TusharBharambe22-Jul-12 20:52 
QuestionWhat is OAUTH and How can i use this one in my ASP.NET website? Pin
Member 917112815-Jul-12 18:26
Member 917112815-Jul-12 18:26 
AnswerRe: What is OAUTH and How can i use this one in my ASP.NET website? Pin
Umer Aziz Malik15-Jul-12 20:36
Umer Aziz Malik15-Jul-12 20:36 
QuestionC# application Integration twitter API Pin
Yasodha.eng1-Jul-12 22:21
Yasodha.eng1-Jul-12 22:21 
QuestionAdd an image Pin
Z@clarco28-May-12 2:38
Z@clarco28-May-12 2:38 
AnswerRe: Add an image Pin
Umer Aziz Malik28-May-12 2:57
Umer Aziz Malik28-May-12 2:57 
GeneralMy vote of 5 Pin
vquangchung16-May-12 3:39
vquangchung16-May-12 3:39 
Questionfinding user's details after login Pin
abhishek.singh.valeteck15-May-12 2:15
abhishek.singh.valeteck15-May-12 2:15 
AnswerRe: finding user's details after login Pin
Umer Aziz Malik28-May-12 2:57
Umer Aziz Malik28-May-12 2:57 
GeneralVery good Pin
jesseseger10-May-12 1:17
professionaljesseseger10-May-12 1:17 
GeneralRe: Very good Pin
Umer Aziz Malik10-May-12 1:20
Umer Aziz Malik10-May-12 1:20 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.