Home > .net 2.0, .net 3.5, AJAX, c#, Google, JavaScript, Visual Studio 2005, Visual Studio 2008 > Consuming Services: Yahoo! API vs. Google API

Consuming Services: Yahoo! API vs. Google API

October 29, 2007

As both a proof of concept for some internal uses as well as demonstration, I’ve been doing a bit of research into how the Yahoo! and Google APIs work—especially in .NET.  Here’s a bit of a comparison of what I’ve found so far.  I welcome any comments and such as I’m quite sure others out there have been using these technologies far longer than I have—and may have some workarounds for the odd glitches found in each framework.

Yahoo! Developer API

I’ll start out that I’m quite impressed by Yahoo!.  The Developer API was introduced to me in an AJAX session I attended a few months ago and I had totally forgotten about it until recently.  Their .NET Developer Center provides articles, SDK libraries, and most anything you’d need to wrap Yahoo! functionality into your .NET Application. Since Yahoo! returns standard XML (or JSON if you so desire and want to use the FromJson extension method and a custom type), I built a quick console-based searcher app.  The UI is irrelevant, I simply wanted to see how fast I could take data, search for it, and return information.

Most of the code here can be gleamed from the Yahoo! examples…

static void Main(string[] args)

{

// Capture the Yahoo query from the console.

       Console.WriteLine(“Your query to http://www.yahoo.com’s Image Search engine:”);

       string query = Console.ReadLine();

       // If query is blank, end.

       if (query == “”) return;

                      

       foreach (XmlNode node in GetImageSearch(query))

       {

              Console.WriteLine(“{0} @ {1}”,

node[“Title”].InnerText,

node[“Url”].InnerText);

}

}

This bit of code takes the input (from the console at this point) and then searches.  My GetImageSearch method returns an XmlNodeList (encapsulating the whole “get an XML, convert it, get the elements, etc” process) and then allows us to iterate through it, set it to a DataSet, etc.

static XmlNodeList GetImageSearch(string query)

{

// Create the web request

       Uri address = new

Uri(http://search.yahooapis.com/

ImageSearchService/V1/imageSearch”);

string appId = “YahooDemo”;

HttpWebRequest request =

 WebRequest.Create(address) as HttpWebRequest;

       // Set type to POST  

       request.Method = “POST”;

       request.ContentType = “application/x-www-form-urlencoded”;

       // Create the data we want to send  

       StringBuilder data = new StringBuilder();

       data.Append(“appid=” + HttpUtility.UrlEncode(appId));

       data.Append(“&query=” + HttpUtility.UrlEncode(query));

       // Create a byte array of the data we want to send  

byte[] byteData =

UTF8Encoding.UTF8.GetBytes(data.ToString());

       // Set the content length in the request headers  

       request.ContentLength = byteData.Length;

       // Write data  

       using (Stream postStream = request.GetRequestStream())

       {

              postStream.Write(byteData, 0, byteData.Length);

}

       // Get response

       using (HttpWebResponse response =

request.GetResponse() as HttpWebResponse)

       {

              // Get the response stream  

             StreamReader reader =

new StreamReader(response.GetResponseStream());

             XmlDocument xmlOutput = new XmlDocument();

             xmlOutput.LoadXml(reader.ReadToEnd());

             XmlNodeList resultsNodes =

xmlOutput.GetElementsByTagName(“Result”);

             return resultsNodes;

}

}

That’s it!  Note for real world use, you’ll need to replace that AppId with a real Yahoo Application Id.

Pros:

  • XML Output is standard, JSON optional.  Works like a snap for .NET usage.
  • Amazing support and community for the APIs.
  • A Search SDK exists for C# and VB.NET, though I didn’t use it.  That’d probably replace all of the coding above to a simple call or two.
  • You only need one Yahoo! Application ID per… application! (you’ll see why this is a pro in a minute).
  • Almost any language/framework has a Developer Center.

Cons:

  • It’s Yahoo.  I’m a Google fan myself (using Web Search, Google Mail, Google Docs, etc.), maybe this will change my mind and make this into a Pro as time goes on.

Google Developer APIs

Google’s been my Search of Choice for years now.  As I mentioned above, I use several of their services and have never had much of a problem.  In the results I get back from their search services, I tend to get what I’m looking for in the top ten or twenty, not the top 100 to 200 as I do with Yahoo and Live Search. With that in mind, their Developer Center is almost horrifying.  While AJAX and JavaScript are grand technologies, I had hoped for a bit of the same control as the Yahoo! solution provided—especially if I wanted to consume the information in non-web applications. 

[update 30 Oct 07: Google previously had a SOAP Search, but as of 5 December 2006, the SOAP Search is no longer available.  Bloody hell, why?! 

“Depending on your application, the AJAX Search API may be a better choice for you instead. It tends to be better suited for search-based web applications and supports additional features like Video, News, Maps, and Blog search results.

Okay, I’ll give you that, but what if you don’t want to use it in a web application?  *grumble*]

To provide an example of the AJAX Web Search, here’s what we have:

 

<html>
<head>
<title>Search Example</title>

<script type=”text/javascript”
  src=”http://www.google.com/jsapi?key=KEY&#8221; />
<script type=”text/javascript”> 

google.load(“search”, “1”);

function initialize()
{
 var searchControl = new google.search.SearchControl();
 searchControl.addSearcher(new google.search.WebSearch());
 searchControl.draw(document.getElementById(“searchcontrol”));
}

google.setOnLoadCallback(initialize);

</script>
<script type=”text/javascript”>

</script>

</head>
<body>
    <div id=”searchcontrol”></div>
  </body>
</html>

 

That code works just fine, but it doesn’t give me control of the UI or the results, it generates: Now, I did find the google-gdata project, a Google Data API for .NET—but apparently GData doesn’t include things like searches, only Claendars, Docs, and such.  Bleh. So, what could be done to wrap this up?  I could put the JavaScript into a custom control and intercept the text coming back into the element from the .Draw method, but ehh… beyond that, I’m interested to see how others are wrapping Google technologies into .NET applications—if you have, please post links and feedback!

 

Pros:

  • Development outside .NET or outside the OOP side of .NET, it’s pretty slick.
  • Creates a nice interface for the tools.

Cons:

  • You need a new application key for every service you use, for every site you create.  If your site consumes Search, AJAX Feed, and Maps, you need 3 keys.  Really a hassle.
  • Appears to be little or no language specific support outside of HTML and JavaScript for the API.  That’s fine, but wrappers would be nice too.
  • Google Web Toolkit is Java only.😦

For now, both of these services still interest me and both are excellent examples of what could be done to mash up technologies into a modern web application—it all depends on what you need. For grins, I’m sure the “Microsoft” answer is to consume Live Services rather than the competition, but I’ve never had much luck using Live—anyone using it now?

  1. No comments yet.
  1. October 29, 2007 at 3:42 pm
Comments are closed.
%d bloggers like this: