Implementing Yahoo Contact Reader In ASP.NET and CSharp

Implementing Yahoo Contact Reader In ASP.NET and CSharp

In this tutorial we will explain how to use Yahoo API to read a person’s Yahoo contacts. In common language we create an app called Yahoo Contact Reader for anyone who authorizes it to use it. We will use C# to communicate with Yahoo API and our app is made in ASP.NET platform.

There are 2 steps in making a Yahoo Contact Reader App –

STEP 1 – Implement Yahoo OAuth 2.0

The first step is to implement Yahoo OAuth 2.0. We have written a tutorial on Implementing Yahoo OAuth 2.0 in CSharp and Asp.Net

Once the OAuth 2.0 part is successfully done you will receive 5 values by Yahoo which are –

  • 1. access_token – the token through which you can make Yahoo API calls.
  • 2. token_type – the access token type
  • 3. expires_in – token lifetime in seconds
  • 4. refresh_token – the refresh token that is used to get new token after the current token expires (as given in the ’expires_in’ parameter ).
  • 5. xoauth_yahoo_guid – the GUID of the Yahoo user

STEP 2 – Communicate With Yahoo API

The step 2 involves making API request to Yahoo. The URL where we have to make the HTTP GET request is – https://social.yahooapis.com/v1/user/{xoauth_yahoo_guid}/profile?format=json. For getting response in XML remove JSON with XML in the query string. It is also needed to include Authorization: Bearer Access Token in the header of the HTTP GET request.

In Google Contacts API we have communicated with the Google API to export a person’s contacts.

IMPLEMENTATION

After getting the Yahoo response on completing the STEP 1 we pass the response to our custom function ‘GetContact’ whose definition is given below:

<asp:Button ID="yahooButton" Text="Yahoo Contact Reader" runat="server" OnClick="yahooButton_Click" />
<div id="dataDiv" runat="server"></div>
private void GetContact(string responseFromServer)
{
    responseFromServer = responseFromServer.Substring(1, responseFromServer.Length - 2);
    string accessToken = "", xoauthYahooGuid = "", refreshToken = "";
    string[] splitByComma = responseFromServer.Split(',');
    foreach (string value in splitByComma)
    {
        if (value.Contains("access_token"))
        {
            string[] accessTokenSplitByColon = value.Split(':');
            accessToken = accessTokenSplitByColon[1].Replace('"'.ToString(), "");
        }
        else if (value.Contains("xoauth_yahoo_guid"))
        {
            string[] xoauthYahooGuidSplitByColon = value.Split(':');
            xoauthYahooGuid = xoauthYahooGuidSplitByColon[1].Replace('"'.ToString(), "");
        }
        else if (value.Contains("refresh_token"))
        {
            string[] refreshTokenSplitByColon = value.Split(':');
            refreshToken = refreshTokenSplitByColon[1].Replace('"'.ToString(), "");
        }
    }

    try
    {
        /*Yahoo Api Request*/
        HttpWebRequest request = WebRequest.Create("https://social.yahooapis.com/v1/user/" + xoauthYahooGuid + "/contacts?format=json") as HttpWebRequest;
        request.Headers["Authorization"] = "Bearer " + accessToken;

        string contactResponse = "";
        using (HttpWebResponse response = request.GetResponse() as HttpWebResponse)
        {
            StreamReader reader = new StreamReader(response.GetResponseStream());
            contactResponse = reader.ReadToEnd();
        }
        /*End*/
            
        /*JSON Seriazlization*/
        StringBuilder yahooContactStringBuilder = new StringBuilder();
        YahooContactsData yahooContactsData = new System.Web.Script.Serialization.JavaScriptSerializer().Deserialize<YahooContactsData>(contactResponse);
        int i = 1;
        foreach (YContact yContact in yahooContactsData.contacts.contact)
        {
            List<YFields> fields = yContact.fields;
            foreach (YFields data in fields)
            {
                if (data.value.ToString().Contains("@"))
                {
                    yahooContactStringBuilder.Append(i + "&nbsp;").Append(data.value.ToString()).Append("<br/>");
                    i++;
                }
            }
        }
        /*End*/
        dataDiv.InnerHtml = yahooContactStringBuilder.ToString();
    }
    catch (WebException ex)
    {
        Response.Write(ex.Message);
    }
}

Note that we get the person’s contacts from yahoo in JSON format and we pass ‘xoauth_yahoo_guid’ in the URL. The xoauth_yahoo_guid value is received in the response of Step 1.

We are serializing the contacts data in JSON format with dot net inbuilt class ‘JavaScriptSerializer’. For making use of this we have made 4 custom classes in our application. These classes are –

public class YahooContactsData
{
    public YContacts contacts;
}
public class YContacts
{
    public List<YContact> contact { get; set; }
}
public class YContact
{
    public List<YFields> fields { get; set; }
}
public class YFields
{
    public int id { get; set; }
    public string type { get; set; }
    public object value { get; set; }
}

In the end we are showing the Yahoo Contacts of the person in a div.

DEMO DOWNLOAD

Conclusion
In this way we can create a Yahoo Contact Reader by making use of AUTH 2.0 and Yahoo API. It’s implementation in C# and ASP.NET is quite easy and fast. You can easily use it in other Yahoo API in making your custom apps.

Share this article -

yogihosting

ABOUT THE AUTHOR

This article has been written by the Technical Staff of YogiHosting. Check out other articles on “WordPress, SEO, jQuery, HTML” and more.