Click here to Skip to main content
Click here to Skip to main content

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

By , 24 Feb 2012
Rate this:
Please Sign up or sign in to vote.

Introduction

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.

Background

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 http://hueniverse.com/oauth/. 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 http://api.twitter.com/1/users/show.json?screen_name=abc.

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. "OAuth4Client.zip", 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. Smile | :)

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 https://developers.facebook.com/apps -> 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.

History

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

License

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

About the Author

Sachin Kumar, Synechron
Software Developer (Senior) Mindfire Solutions
India India
No Biography provided
Follow on   Twitter

Comments and Discussions

 
BugGetting error while accessing google profile Pinmembersumi_200312-Jan-14 21:39 
QuestionNot working with mvc3 PinmemberShashi Kant Yadav19-Oct-13 21:14 
GeneralAouth implementation PinmemberMember 1005876914-Oct-13 18:56 
QuestionGetting Error while Profile Response from Social Sites using oAuth in ASP.NET C# Pinmemberravi.r.potdar@gmail.com15-Sep-13 23:14 
AnswerRe: Getting Error while Profile Response from Social Sites using oAuth in ASP.NET C# PinmemberGarry Lowther27-Sep-13 1:09 
Questionfacebook btn is worked succesfully. but gmail btn is not worked ? Pinmembersrigates7-Jun-13 0:10 
QuestionCould you publish MVC version ? Pinmemberfortix200721-May-13 21:27 
QuestionHow can I use this from a WinForms application? Pinmembermeraydin9-May-13 23:22 
Questionthanks for this code PinmemberKranti Singh8-Apr-13 3:29 
QuestionThe page requests Consumer Key and Secret PinmemberR koyee14-Mar-13 21:44 
AnswerRe: The page requests Consumer Key and Secret PinmemberChung Lai Thanh12-Dec-13 17:01 
Questionprofile picture PinmemberArthur L R Souza7-Jan-13 11:01 
QuestionRegards Vs2008 Pinmemberarunraju18-Dec-12 20:38 
QuestionCode for risevision core api using asp.net PinmemberjatinThedeveloper4-Dec-12 23:27 
QuestionHi Thanks for giving greate article,Explain how it works PinmemberMember 917112815-Jul-12 21:30 
QuestionObject reference not set to an instance of an object errors PinmemberMember 23870299-Jul-12 12:41 
AnswerRe: Object reference not set to an instance of an object errors PinmemberSachin Kumar, Mindfire10-Jul-12 2:04 
AnswerRe: Object reference not set to an instance of an object errors PinmemberMember 238702910-Jul-12 5:05 
QuestionMVC PinmemberRichB377-Apr-12 20:23 
QuestionInitializing error! Please check the Customer key and Customer secret key PinmemberRajeev Chowdary Gurram6-Apr-12 16:31 
AnswerRe: Initializing error! Please check the Customer key and Customer secret key PinmemberSachin Kumar, Mindfire7-Apr-12 2:55 
QuestionCustomer key and Customer secret key PinmemberMember 876154929-Mar-12 1:44 
AnswerRe: Customer key and Customer secret key PinmemberSachin Kumar, Mindfire29-Mar-12 2:47 
GeneralRe: Customer key and Customer secret key PinmemberMember 876154929-Mar-12 3:06 
GeneralRe: Customer key and Customer secret key PinmemberRajeev Chowdary Gurram6-Apr-12 16:30 

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

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

| Advertise | Privacy | Mobile
Web03 | 2.8.140415.2 | Last Updated 24 Feb 2012
Article Copyright 2012 by Sachin Kumar, Synechron
Everything else Copyright © CodeProject, 1999-2014
Terms of Use
Layout: fixed | fluid