1

Import Yahoo Contacts using Oauth in C#

Im building a Web app which uses the Yahoo API to download the contact details from Yahoo
Im able to connet to the Yahoo API get till the Access Token and Oauth guid token.

But when i try to send a Web request to the yahoo api to retrieve the contacts I get the Cannot connect to the remote server error. Can you please help to see what im missing??
Below is the code snippet

Uri uri = new Uri("http://social.yahooapis.com/v1/user/" + OauthYahooGuid + "/contacts?format=XML");

string nonce = oauth.GenerateNonce();
string timeStamp = oauth.GenerateTimeStamp();
string normalizedUrl;
string normalizedRequestParameters;
string sig = oauth.GenerateSignature(uri, ConsumerKey, ConsumerSecret, OauthToken, OauthTokenSecret, "GET", timeStamp, nonce, OAuthBase.SignatureTypes.PLAINTEXT, out normalizedUrl, out normalizedRequestParameters);

StringBuilder sbGetContacts = new StringBuilder(uri.ToString());

//Response.Write("URL: " + sbGetContacts.ToString());
//Response.End();

try
{
string returnStr = string.Empty;
WebRequest req = WebRequest.Create(uri);
req.Method = "GET";

string authHeader = "Authorization: OAuth " +
"realm=\"yahooapis.com\"" +
",oauth_consumer_key=\"" + ConsumerKey + "\"" +
",oauth_nonce=\"" + nonce + "\"" +
",oauth_signature_method=\"PLAINTEXT\"" +
",oauth_timestamp=\"" + timeStamp + "\"" +
",oauth_token=\"" + OauthToken + "\"" +
",oauth_version=\"1.0\"" +
",oauth_signature=\"" + HttpUtility.UrlEncode(sig) + "\"";

//Response.Write("</br>Headers: " + authHeader);

req.Headers.Add(authHeader);

WebResponse res = req.GetResponse();
//THe code is failing here "Cannot connect to remote server"
StreamReader streamReader = new StreamReader(res.GetResponseStream());
returnStr = streamReader.ReadToEnd();
XmlDocument xmldoc = new XmlDocument();
xmldoc.LoadXml(returnStr);
XmlNodeList elemList = xmldoc.DocumentElement.GetElementsByTagName("fields");

ArrayList emails = new ArrayList();
for (int i = 0; i < elemList.Count; i++)
{
if (elemList[i].ChildNodes[1].InnerText == "email")
emails.Add(elemList[i].ChildNodes[2].InnerText);
//Response.Write(elemList[i].ChildNodes[2].InnerText + "<br/>");
}
}

Thanks

by
0 Replies

Recent Posts

in Contacts