Reader Level:
ARTICLE

Intergrating with google Search API

Posted by mem_1910 Articles | Web Services in C# August 17, 2006
This article will show us how to integrate with "Google Search API" from our windows application or web application.
  • 0
  • 0
  • 30384

Google created a search API web service that allow us to search billions of web pages from our own applications, those web services can be exposed from any programming environment such as .Net or java.

So the following are the needed steps for integrating with this service and small .Net sample .

  1. You can download the Developers Kit which contains .net and java samples and the wsdl file from the following Url. 

    http://www.google.com/apis/download.html

  2. You must have google account 'gmail' in order to get the license key for using this API. So if you don't have an account you should go to the following link and create one. 

    https://www.google.com/accounts/NewAccount?
    continue=http://api.google.com/createkey&
    followup=http://api.google.com/createkey


    or if you already have one just sign in and an email message containing the license  key will be sent to you .

    https://www.google.com/accounts/Login?continue=http%3A%2F%2Fapi.google.com%2Fcreatekey&followup=http%3A%2F%2Fapi.google.com%2Fcreatekey

  3. After you get the license key you can start develop the application which we will mention in the following steps.

    • Open Visual Studio .Net 2003 or 2005 
    • Choose new project and select the type and your preferred language ' we will choose win app with C# in our example'
    • Right click in the solution explorer and choose add web reference and write the following url  in the appeared dialog 

      http://api.google.com/GoogleSearch.wsdl 

      And choose add reference as in the picture.

      google1.gif

    • Put in the form a textbox and change its ID to Txt_Text for the search and put a button and change it's ID to Btn_Search 
    • Drag and drop a datagrid if you are using 2003 or datagridview if 2005 .
    • Write the following code under the Btn_Search _Click event.

      com.google.api.GoogleSearchService s = new TestGoogle.com.google.api.GoogleSearchService();
      com.google.api.GoogleSearchResult r = s.doGoogleSearch("put your lisence key her ", Txt_Text.Text, 0, 10, false, "", true, "", "", "");
      int estimatedCount =r.estimatedTotalResultsCount;

      DataTable dtResults = new DataTable();
      dtResults.Columns.Add(new DataColumn("Title", typeof(string)));
      dtResults.Columns.Add(new DataColumn("Summary", typeof(string)));
      dtResults.Columns.Add(new DataColumn("URL", typeof(string)));
      for (int i = 0; i < 10; i++)
      {
          DataRow dr = dtResults.NewRow();
          dr[0] =  r.resultElements[i].title;
          dr[1] =  r.resultElements[i].snippet;
          dr[2] = r.resultElements[i].URL;
          dtResults.Rows.Add(dr);
      }
      dataGridView1.DataSource = dtResults;

google2.gif

This is the running application

google3.gif

COMMENT USING

Trending up