Integrating With Google Search API

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.
      1. com.google.api.GoogleSearchService s = new TestGoogle.com.google.api.GoogleSearchService();  
      2. com.google.api.GoogleSearchResult r = s.doGoogleSearch("put your lisence key her ", Txt_Text.Text, 0, 10, false""true"""""");  
      3. int estimatedCount =r.estimatedTotalResultsCount;  
      4.   
      5. DataTable dtResults = new DataTable();  
      6. dtResults.Columns.Add(new DataColumn("Title"typeof(string)));  
      7. dtResults.Columns.Add(new DataColumn("Summary"typeof(string)));  
      8. dtResults.Columns.Add(new DataColumn("URL"typeof(string)));  
      9. for (int i = 0; i < 10; i++)  
      10. {  
      11.     DataRow dr = dtResults.NewRow();  
      12.     dr[0] =  r.resultElements[i].title;  
      13.     dr[1] =  r.resultElements[i].snippet;  
      14.     dr[2] = r.resultElements[i].URL;  
      15.     dtResults.Rows.Add(dr);  
      16. }  
      17. dataGridView1.DataSource = dtResults;

google2.gif

This is the running application

google3.gif


Similar Articles