Click here to Skip to main content
13,295,178 members (66,670 online)
Click here to Skip to main content
Add your own
alternative version

Tagged as


61 bookmarked
Posted 15 Jan 2012

Getting User Profile Response from Social Sites using oAuth in ASP.NET C#

, 24 Feb 2012
Rate this:
Please Sign up or sign in to vote.
This article describes how to get a response from social websites using OAuth code. The demo project reads from 4 major social sites (Google, Facebook, Twitter and LinkedIn).


In this article, I will discuss about how to implement OAuth in getting responses from 4 major social sites (Facebook, Google, Twitter and LinkedIn). The demo application will help the developers working with social authentication or social login integration to their website. The article will cover points starting from difference between OAuth version 1.0 and 2.0 to getting user profile responses. The demo application is a playground to test OAuth process step by step. In the application, we have the scope to add other social provider if we wish to.


I faced so many issues in getting started with Version 1.0 OAuth code with different social sites like Google, Twitter and Linkedin. There are so many frustrating errors and responses coming from social sites that are of no real help and hint. But understanding them over again, I came to know that it is just a matter of sending correct authorization header to valid end points. I compiled all the working code of all social sites and made a working project to successfully authenticate from 4 different sites (Google, Facebook, Twitter and Linked in) and lastly getting back the user profile responses.

Difference between OAuth Version 1.0 and 2.0

OAuth 1.0 version specifications are given at In a nutshell version, practical steps are:

  1. We need to have API keys (customer key and customer secret).
  2. Using API keys get Request Token.
  3. Using Request Token, verify user by login to social site and redirecting back to the application.
  4. After successful verification, get Access Token.
  5. Using Access token, create request for service APIs like getting user profile, post comments, get friends list etc.

OAuth 2.0 version has relatively less steps as follow:

  1. We need to have API keys (customer key and customer secret).
  2. Using API keys, verify user by login to social site and redirecting back to the application.
  3. After successful verification, get Access Token.
  4. Using Access token, Create request for service APIs like getting user profile, post comments, get friends list, etc.

Using the Code

Starting from step 1 and code sample:

  1. Declare "CustomerKey" and "CustomerSecret" in OAuth\Credentials.cs.
  2. Function GetRequestToken() called in OAuth\OAuthClient.cs to get request token.
  3. After that function ObtainVerifier() is used to redirect to social site login page for authentication.
  4. After redirecting from social site, we land at "OAuthVerifier.aspx". Then button "Get Access Token" triggers function GetAccessToken() to get "AccessToken" and "AccessTokenSecret" for Version 1.0 and "Code" for Version 2.0. Now these vars can be used to call service APIs of social sites.
  5. Button "Get Response" is now activated in OAuthVerifier.aspx to get profile response. It fires function GetProfileResponse().

Profile Response Code Explanation

In OAuth process, there are 2 phases:

  1. The first phase involves a redirection-based browser process for end-users to authorize client access to their resources.
  2. The second phase involves a method for making authenticated HTTP requests using set of credential for using resources.

The first phase process is straightforward and has common code for almost all social sites authorization (depends on version 1.0 and 2.0). But for the second phase, we need some awareness to call the resources APIs. E.g., In Twitter, even after getting Access token, we needed "screen_name" or "user_id" to get profile data from Twitter server. Profile request URL for Twitter should be

Similarly, other providers need some query string params to pass with URL. We have a function CreateQueryString() in OAuth\OAuthContext.cs to add needed vars.

private string CreateQueryString()
   string queryString;
   switch (SocialSiteName.ToLower())
       case "twitter"  : queryString = "?screen_name=" + ScreenName;   break;
       case "facebook" : queryString = "?access_token=" + AccessToken; break;
       case "linkedin" : queryString = "?format=json";                 break;
       case "google"   : queryString = string.Empty;                   break;
       default         : queryString = string.Empty;                   break;

    return (queryString);

Getting Started

  1. Download the attached demo project. "", extract the file and open up the solution in Visual Studio 2010.
  2. Go to OAuth\Credentials.cs, give consumer key and consumer secret for each social site to test.
  3. Make Default.aspx page as start page and press F5. Choose your social site and now you are in the playground. :)

Facebook integration (Version 2.0)

I got some mails regarding Facebook error coming with the code. To get profile response from Facebook, we need to follow some basic steps:
  1. Provide ConsumerKey and ConsumerSecret for Facebook in OAuth\Credentials.cs (as usual).
  2. Go to -> login there -> you will be asked to give password again.
  3. Select the app for which you are using the ConsumerKey and ConsumerSecret -> click Edit App.
  4. Go to Website -> Site url text box. Provide the "http://localhost:49262/OAuthVerifier.aspx" in there -> Save
  5. Come to the application. Press F5 (run in debug mode, it should run with "localhost:49262"). Then test for the profile response. It should come now.


  • 15th January, 2012: Initial post
  • 24th February, 2012: Article updated


This article, along with any associated source code and files, is licensed under The MIT License


About the Author

Sachin Kumar, IGate
Technical Lead IGate Global Solutions
India India
No Biography provided

You may also be interested in...


Comments and Discussions

QuestionScope variable not working for the linked in Login. Please Help Pin
Member 836192927-Apr-15 4:33
memberMember 836192927-Apr-15 4:33 
QuestionHow can I Search Candidates profiles from Yahoo using Pin
Member 1138707526-Mar-15 22:58
memberMember 1138707526-Mar-15 22:58 
QuestionOauth 2 for linked in Pin
Member 1153996819-Mar-15 15:50
memberMember 1153996819-Mar-15 15:50 
QuestionHow to get Candidate profile from Yahoo? Pin
Member 1138707517-Mar-15 22:56
memberMember 1138707517-Mar-15 22:56 
QuestionError not working at all.. Pin
A.Girish3-Aug-14 21:38
memberA.Girish3-Aug-14 21:38 
Generalusing above code below error getting Pin
vr reddy3-Jul-14 22:52
membervr reddy3-Jul-14 22:52 
BugGetting error while accessing google profile Pin
sumi_200312-Jan-14 22:39
membersumi_200312-Jan-14 22:39 
QuestionNot working with mvc3 Pin
Shashi Kant Yadav19-Oct-13 22:14
memberShashi Kant Yadav19-Oct-13 22:14 
GeneralAouth implementation Pin
Member 1005876914-Oct-13 19:56
memberMember 1005876914-Oct-13 19:56 
QuestionGetting Error while Profile Response from Social Sites using oAuth in ASP.NET C# Pin
ravi.r.potdar@gmail.com16-Sep-13 0:14
memberravi.r.potdar@gmail.com16-Sep-13 0:14 
AnswerRe: Getting Error while Profile Response from Social Sites using oAuth in ASP.NET C# Pin
Garry Lowther27-Sep-13 2:09
memberGarry Lowther27-Sep-13 2:09 
Questionfacebook btn is worked succesfully. but gmail btn is not worked ? Pin
srigates7-Jun-13 1:10
membersrigates7-Jun-13 1:10 
QuestionCould you publish MVC version ? Pin
fortix200721-May-13 22:27
memberfortix200721-May-13 22:27 
QuestionHow can I use this from a WinForms application? Pin
meraydin10-May-13 0:22
membermeraydin10-May-13 0:22 
Questionthanks for this code Pin
Kranti Singh8-Apr-13 4:29
memberKranti Singh8-Apr-13 4:29 
QuestionThe page requests Consumer Key and Secret Pin
R koyee14-Mar-13 22:44
memberR koyee14-Mar-13 22:44 
AnswerRe: The page requests Consumer Key and Secret Pin
Chung Lai Thanh12-Dec-13 18:01
memberChung Lai Thanh12-Dec-13 18:01 
Questionprofile picture Pin
Arthur L R Souza7-Jan-13 12:01
memberArthur L R Souza7-Jan-13 12:01 
QuestionRegards Vs2008 Pin
arunraju18-Dec-12 21:38
memberarunraju18-Dec-12 21:38 
QuestionCode for risevision core api using Pin
jatinThedeveloper5-Dec-12 0:27
memberjatinThedeveloper5-Dec-12 0:27 
QuestionHi Thanks for giving greate article,Explain how it works Pin
Member 917112815-Jul-12 22:30
memberMember 917112815-Jul-12 22:30 
QuestionObject reference not set to an instance of an object errors Pin
Member 23870299-Jul-12 13:41
memberMember 23870299-Jul-12 13:41 
AnswerRe: Object reference not set to an instance of an object errors Pin
Sachin Kumar, Mindfire10-Jul-12 3:04
memberSachin Kumar, Mindfire10-Jul-12 3:04 
AnswerRe: Object reference not set to an instance of an object errors Pin
Member 238702910-Jul-12 6:05
memberMember 238702910-Jul-12 6:05 
QuestionMVC Pin
RichB377-Apr-12 21:23
memberRichB377-Apr-12 21:23 

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.

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web01 | 2.8.171207.1 | Last Updated 24 Feb 2012
Article Copyright 2012 by Sachin Kumar, IGate
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid