July 27. 18:44 -  Enjoying dinner at SFO.

Using BBAuth with the Yahoo! Address Book

April 25, 2009 12:11 by docbliny

There have been several people requesting help using the Yahoo! Address Book web services in the .NET Developer Forum. People are getting 403 or 404 error codes back.

I did some digging into the issue this morning and the cause for the 404 error turns out to be an invalid URL for the DOCTYPE declaration for the returned XML data. In short the URL http://l.yimg.com/a/lib/pim/r/abook/xml/2/pheasant.dtd set as the DOCTYPE for the resulting successful web service call XML data is invalid.

This causes problems when using the Yahoo.Authentication class methods, such as GetAuthenticatedDataSet(), resulting in a 404 when the .NET XmlReader attempts to validate the data against the DTD.

The way to work around this issue is to use your own XmlReader with custom XmlReaderSettings specifying to ignore the DTD.

Below is an example of reading address book data and placing it in a DataSet object without using the GetAuthenticateDataSet() method.

// Running on BBAuth callback page


string url 
   = "http://address.yahooapis.com/v1/searchContacts?format=xml&fields=name,email";

try
{
  // This line will cause a 404 on the returned DTD, use the code below instead
  //dsServices = auth.GetAuthenticatedServiceDataSet(new System.Uri(url));

  // Get the data as a Stream
  Stream dataStream = auth.GetAuthenticatedServiceStream(new Uri(url));
    
  // Create custom options for XmlReader
  XmlReaderSettings settings = new XmlReaderSettings();
  settings.XmlResolver = null;
  settings.ProhibitDtd = false;

  // Create an XmlReader
  XmlReader reader = XmlReader.Create(dataStream, settings);
    
  // Create a new DataSet object and load in the data
  System.Data.DataSet dsServices = new DataSet();
  dsServices.ReadXml(reader);
}
catch (Exception ex)
{
  // Something went wrong...
}

I'll work with the Yahoo! Developer Network people to see if we can get the DTD issue fixed.


Comments