Click here to Skip to main content
15,860,972 members
Articles / Web Development / ASP.NET

Implementing Yahoo! Contact Reader Using ASP.NET

Rate me:
Please Sign up or sign in to vote.
4.92/5 (37 votes)
5 Jul 2010Ms-PL6 min read 202K   5.7K   53   64
Implementing Yahoo! contact reader app using Yahoo contact API and ASP.NET

Introduction

Mail contact reader have become an exciting feature as new social sites are being introduced or email-campaign becomes a key success factor for online business. I have worked on several projects where app facilitates email contact reading from user’s personal mailing account. Most of the users use free mailing services like Gmail/Hotmail/AOL/Yahoo and the list goes on. Most of the time, I used third party solutions (that saves lots of my time) and those work pretty well. But those solutions I used are not easy to customize according to my needs. So this time, I have decided to find myself a stable solution and I started with a very popular mail service Yahoo!. This article will be helpful for those people who are working with contact reader app and for those who are interested in working with Yahoo! API.

Yahoo! API

The Yahoo! Developer Network (YDN) is Yahoo!'s center for developer resource. YDN contains tools/utilities/gadgets/API docs and samples for developers.You can start using the resources provided by signing yourself for an API key and you are ready to go.

Authentication and Authorization with Yahoo!

Yahoo! offers 3 ways to connect with their services, the first is OpenID to authenticate users, the second one is OAuth to control access to protected data and the third one is OpenID-OAuth Hybrid Protocol, which combines OpenID authentication with OAuth authorization in a single interface. I found OAuth is most convincing and will stick to it for this article. If you want to know more about other 2 authentication models, I suggest you follow this link. Before jumping to the implementation with OAuth model, let's refresh our mind with a quick review of basic OAuth mechanism.

OAuth Authentication Basics

OAuth is the industry-standard authorization method and is used on various platforms. It's an open authorization model based primarily on existing standards that ensures secure credentials can be provisioned and verified by different software platforms. The simplest definition can be OAuth protocol enables users to provide third-party access to their web resources without sharing their passwords (You will find details about the authentication here). OAuth is a secure and quick way to publish and access private data, such as contact lists and updates, and this is why I choose OAuth model to retrieve users' contact information.

Figure 1: Basic OAuth model

You can download the sample code/documentation compatible for .NET from the links below:

Setting up Yahoo! OAuth

In order to use the Yahoo! OAuth, we have to follow a series of steps:

  • Sign Up and Get a Consumer Key: Before you can start making Yahoo! API requests, you need to sign up and submit some details about your application.
  • Get a Request Token: The Request Token is a temporary token used to initiate User authorization for your application. The Request Token tells Yahoo! that you've obtained User approval.
  • Get User Authorization: After getting the Request Token from Yahoo!, your application presents to your Users a Yahoo! authorization page asking them to give permission to your application to access their data.
  • Exchange the Request Token and OAuth Verifier for an Access Token: After your Users authorize your application access to their information, your application needs to exchange the approved Request Token for an Access Token, which tells Yahoo! that your application has been given authorization to access User data.
  • Refresh the Access Token: You can use the Access Token for one hour until it expires. To get a new Access Token for continued use, use the same expired token and the get_token call to be provided a new Access Token.

Let's see how OAuth works with Yahoo! API:

oAuth

Figure 2: Yahoo! OAuth model

Setting Up an API Key

You can request for an API key by navigating this link. You have to fill up the web form before you request for a key. There are 2 steps. The first step is filling out app specific information and request for an API key and the second step is to specify what services can be accessible by the API key. You can choose to access all public resources or alternatively, you can specify which services you are particularly interested in.

Step 1: Setting up App Information & Get API Key

Figure 3: Setting up app information
Configuration Notes
  • Application URL: This is the URL where your application resides.You can point out the root of the application here. For my app, I mentioned http:www.imgalib.com/ as app URL.
  • Choose an appropriate application name (my application name is qcontactreader).
  • Specify app kind, my sample app is web based.
  • Provide a small description about your application.
  • Access scope: Choose "This app requires access to private user data." option as my sample app is going to access the user contact list.
  • Hit Get API key and you are ready to roll.

Step 2 : Specify Permissions with the API Key

Figure 4: Specify permissions
Configuration Notes
  • Choose Yahoo Contact API and allow read permission. This API allows the app to view and/or import a user's Contacts data from the Yahoo! Contacts application.

Please remember the notes mentioned above are used to configure an app based on my needs. Feel free to configure according to your app needs.

Using the Sample Code

The sample code is simplified with the steps mentioned in Fig: 2. As mentioned in step 2 function:

C#
private string GetRequestToken()
{
        string authorizationUrl = string.Empty;
        OAuthBase oauth = new OAuthBase();

        Uri uri = new Uri("https://api.login.yahoo.com/oauth/v2/get_request_token");
        string nonce = oauth.GenerateNonce();
        string timeStamp = oauth.GenerateTimeStamp();
        string normalizedUrl;
        string normalizedRequestParameters;
        string sig = oauth.GenerateSignature
		(uri, ConsumerKey, ConsumerSecret, string.Empty, 
		string.Empty, "GET", timeStamp, nonce, 
		OAuthBase.SignatureTypes.PLAINTEXT, out normalizedUrl, 
		out normalizedRequestParameters); //OAuthBase.SignatureTypes.HMACSHA1
        StringBuilder sbRequestToken = new StringBuilder(uri.ToString());
        sbRequestToken.AppendFormat("?oauth_nonce={0}&", nonce);
        sbRequestToken.AppendFormat("oauth_timestamp={0}&", timeStamp);
        sbRequestToken.AppendFormat("oauth_consumer_key={0}&", ConsumerKey);
        sbRequestToken.AppendFormat("oauth_signature_method={0}&", 
					"PLAINTEXT"); //HMAC-SHA1
        sbRequestToken.AppendFormat("oauth_signature={0}&", sig);
        sbRequestToken.AppendFormat("oauth_version={0}&", "1.0");
        sbRequestToken.AppendFormat("oauth_callback={0}", 
	HttpUtility.UrlEncode("http://www.imgalib.com/demo/yahoo-oauth/default.aspx"));
        ..........
        ..........
        ...........
}

This function builds request to connect with Yahoo! through oAuth and receives Request token and with this token now requests to access user address book by requesting access token:

C#
private void GetAccessToken(string oauth_token, string oauth_verifier)
{
    OAuthBase oauth = new OAuthBase();

       Uri uri = new Uri("https://api.login.yahoo.com/oauth/v2/get_token");
       string nonce = oauth.GenerateNonce();
       string timeStamp = oauth.GenerateTimeStamp();
       string sig = ConsumerSecret + "%26" + OauthTokenSecret;

       StringBuilder sbAccessToken = new StringBuilder(uri.ToString());
       sbAccessToken.AppendFormat("?oauth_consumer_key={0}&", ConsumerKey);
       sbAccessToken.AppendFormat("oauth_signature_method={0}&",
                   "PLAINTEXT"); //HMAC-SHA1
       sbAccessToken.AppendFormat("oauth_signature={0}&", sig);
       sbAccessToken.AppendFormat("oauth_timestamp={0}&", timeStamp);
       sbAccessToken.AppendFormat("oauth_version={0}&", "1.0");
       sbAccessToken.AppendFormat("oauth_token={0}&", oauth_token);
       sbAccessToken.AppendFormat("oauth_nonce={0}&", nonce);
       sbAccessToken.AppendFormat("oauth_verifier={0}", oauth_verifier);
       ................
       ................
}

This step will prompt the user with a permission window. If user allows app to read his/her contact list, then the list is retrieved by:

C#
private void RetriveContacts()
{
   Uri uri = new Uri("http://social.yahooapis.com/v1/user/" +
       OauthYahooGuid + "/contacts?format=XML");
   .........
   .........
}

If you want to run the sample code, you have to go through a couple of steps, and that starts with setting up an API key described above. Then host the app at the server as Yahoo! needs to communicate with your provided callback URL. Open default.aspx and change these property values with your respective registered key:

C#
public string ConsumerKey
{
    get
    {
        return "YOUR_CONSUMER_KEY";
    }
}
C#
public string ConsumerSecret
{
    get
    {
        return "YOUR_CUSTOMER_SECRET_KEY";
    }
}

Open the GetRequestToken() function, change the callback URL with your callback URL:

C#
sbRequestToken.AppendFormat("oauth_callback={0}", 
	HttpUtility.UrlEncode("http://www.yoursite.com/yahoo-oauth/default.aspx"));

That's it, you are ready to go. You can also navigate to this link to find more details about Yahoo! oauth request format or regarding contact API. Also, you can download the library used for this sample code from here.

Demo at Live

Try this application at live hosted at Yahoo! contact reader demo.

Resources

  • OAuthBase class that supports HMAC-SHA1, RSA-SHA1, and PLAINTEXT signature methods contributed by Mr. Andrew Arnott

History

  • 5th July, 2010: Initial post

License

This article, along with any associated source code and files, is licensed under The Microsoft Public License (Ms-PL)


Written By
Chief Technology Officer
Bangladesh Bangladesh
I am a Software Engineer and Microsoft .NET technology enthusiast. Professionally I worked on several business domains and on diverse platforms. I love to learn and share new .net technology and my experience I gather in my engineering career. You can find me from here

Personal Site
Personal Blog
FB MS enthusiasts group
About Me

Comments and Discussions

 
QuestionServer not responding? Pin
Member 1120827918-Nov-14 0:49
Member 1120827918-Nov-14 0:49 
QuestionThe remote server returned an error: (404) Not Found. Pin
Member 1085885228-Oct-14 23:47
Member 1085885228-Oct-14 23:47 
QuestionThe remote server returned an error: (404) Not Found. Pin
Pritam Vadaye10-Oct-14 0:24
Pritam Vadaye10-Oct-14 0:24 
Questionreturned an error: (406) Not Acceptable. Pin
jitendra kachhi14-Aug-14 0:34
jitendra kachhi14-Aug-14 0:34 
AnswerRe: returned an error: (406) Not Acceptable. Pin
Mich_9013-Sep-14 11:50
Mich_9013-Sep-14 11:50 
QuestionPop up page on Yahoo OAuth Pin
Mich_9020-Jul-14 4:26
Mich_9020-Jul-14 4:26 
AnswerRe: Pop up page on Yahoo OAuth Pin
Mich_9013-Sep-14 11:51
Mich_9013-Sep-14 11:51 
Questioncheck this link Pin
Chinmay Deosthali19-Jul-14 1:22
Chinmay Deosthali19-Jul-14 1:22 
Questionerror get resolved Pin
Chinmay Deosthali19-Jul-14 1:18
Chinmay Deosthali19-Jul-14 1:18 
QuestionAwesome work Pin
Member 102859807-Jan-14 23:04
Member 102859807-Jan-14 23:04 
Used the same code in my Desktop Application, works like charm... Smile | :)
QuestionError The remote server returned an error: (500) Internal Server Error. Pin
Member 1047693819-Dec-13 3:30
Member 1047693819-Dec-13 3:30 
QuestionGreat job. Pin
Yogesh_Singh7-Sep-13 1:41
Yogesh_Singh7-Sep-13 1:41 
QuestionHow to get email id and Names Pin
sudhar407-Aug-13 21:42
sudhar407-Aug-13 21:42 
Questioni got yahoo contacts thru c#.net but nt in vb.net Pin
Lakshman Netha13-Jun-13 19:04
Lakshman Netha13-Jun-13 19:04 
AnswerRe: i got yahoo contacts thru c#.net but nt in vb.net Pin
Mich_9013-Sep-14 12:06
Mich_9013-Sep-14 12:06 
Question[Desktop app] C# code to connect to Yahoo/Hotmail Pin
savanthhas24-Feb-13 1:24
savanthhas24-Feb-13 1:24 
QuestionHelp with MVC Version Pin
coommark3-Feb-13 5:20
coommark3-Feb-13 5:20 
GeneralMy vote of 5 Pin
Alen Joy2-Dec-12 18:55
Alen Joy2-Dec-12 18:55 
GeneralRe: My vote of 5 Pin
Shahriar Iqbal Chowdhury/Galib4-Dec-12 0:09
professionalShahriar Iqbal Chowdhury/Galib4-Dec-12 0:09 
NewsWorking Pin
Alen Joy2-Dec-12 18:55
Alen Joy2-Dec-12 18:55 
GeneralRe: Working Pin
Shahriar Iqbal Chowdhury/Galib4-Dec-12 0:09
professionalShahriar Iqbal Chowdhury/Galib4-Dec-12 0:09 
SuggestionWorking Pin
XYD218-Nov-12 20:51
XYD218-Nov-12 20:51 
Questioni had some error with default.aspx Pin
idan soleymani21-Aug-12 11:42
idan soleymani21-Aug-12 11:42 
GeneralMy vote of 5 Pin
gauravpandey13-Jul-12 2:02
gauravpandey13-Jul-12 2:02 
QuestionNot Working Properly plz Help Pin
raghupurohit7-Jul-12 2:27
raghupurohit7-Jul-12 2:27 

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.