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 –
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 –
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.
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.
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:
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.