Implementing Yahoo Contact Reader In ASP.NET and cSharp

Implementing Yahoo Contact Reader In ASP.NET and cSharp

In this tutorial I will explain how to use Yahoo API to read a person’s Yahoo contacts. In common language I create an App called Yahoo Contact Reader for anyone who authorizes it so that this app can access his Yahoo contacts. I will use C# to communicate with Yahoo API and my 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. I 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 from 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 replace JSON with XML in the query string.

It is also necessary to include Authorization: Bearer Access Token in the header of the HTTP GET request.

Related Tutorial – In Google Contacts API article I have communicated with the Google API to export a person’s contacts. You should check this tutorial to.

IMPLEMENTATION

After getting the Yahoo response on completing the STEP 1, pass the response to the 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 + " ").Append(data.value.ToString()).Append("<br/>");
                    i++;
                }
            }
        }
        /*End*/
        dataDiv.InnerHtml = yahooContactStringBuilder.ToString();
    }
    catch (WebException ex)
    {
        Response.Write(ex.Message);
    }
}

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

In Indentity their is a security feature which you can use, this is User Lockout in ASP.NET Core Identity

I am serializing the contacts data using inbuilt C# class called 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 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 I am showing the Yahoo Contacts of the person in a div control.

Download link:

DOWNLOAD

Conclusion

In this way you 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.

Further reading – Session Authentication is a quick way to create login and logout feature in an ASP.NET website. Using this technique you can prevent access to secured pages of your website to non-logged in users.

SHARE THIS ARTICLE

  • linkedin
  • reddit
yogihosting

ABOUT THE AUTHOR

I hope you enjoyed reading this tutorial. If it helped you then consider buying a cup of coffee for me. This will help me in writing more such good tutorials for the readers. Thank you. Buy Me A Coffee donate