5

As part of a people search project I'm currently participating in, I need to write a ruby script that can send search queries to the Google Custom Search API and store the search results for processing. I found the Ruby google-api-client gem (http://code.google.com/p/google-api-ruby-client/) and installed it, but, despite having thoroughly read the documentation, I am at a loss as to how to execute a Custom Search API call. This is my first attempt at using Google APIs and I'm finding the process a bit overwhelming, is there anyone out there with any experience that could provide some sample code for me to study? Thanks

Richard Stokes
  • 3,532
  • 7
  • 41
  • 57

3 Answers3

2

While I haven't tested this, something like this should work:

require 'google/api_client'
# Creates an instance of the client.
client = Google::APIClient.new
# Authorization setup goes here.
# Fetch the discovery document and obtain a reference to the API we care about.
search = client.discovered_api('customsearch')
# Make an API call using a reference to a discovered method.
response = client.execute(
  search.cse.list, 'q' => 'your query'
)
status, headers, body = response

Note that I've omitted all the setup code for authentication, which you can find in the docs for the Ruby client.

Bob Aman
  • 32,839
  • 9
  • 71
  • 95
  • I think the area I'm falling down in is that I don't understand what discovery documents are. Any resources to learn about them and what I need to do with them? – Richard Stokes Jun 13 '11 at 12:20
  • @Richard I think you won't need to do anything with them other than build queries as it shows in the gem docs. Looks like the discovery doc handling bits are all abstracted away in the gem, but here's a link that explains what they are: http://code.google.com/apis/discovery/v1/using.html#discovery-doc – Tim Snowhite Jun 13 '11 at 14:09
  • Yeah, essentially, it's a resource that describes the API in a machine-readable way. This allows the client to understand exactly how to make API calls for a particular API. All you need to know is the identifier for the API and the version you're targeting to get a reference to the discovery document. You can then use this discovery document reference to get method references and then subsequently pass the appropriate parameters to the API call. – Bob Aman Jun 14 '11 at 07:41
  • It's also worth noting that the `google-api` command that ships with the gem allows you to browse parts of the discovery document for any given API. – Bob Aman Jun 14 '11 at 07:42
  • "I've omitted all the setup code for authentication, which you can find in the docs for the Ruby client." ... where are these exactly? I can only find authentication examples for buzz and calendar, which seem to be different in ways that are entirely unclear. – fields Feb 23 '12 at 03:46
  • I get this error: "Need to provide a Custom Search Engine ID. Missing cx or cref parameter." I've looked at the docs for these, but I can't quite make sense of it. Is there a sensible default for just running a search like I would on the web just by API? – Wolfram Arnold Jun 12 '12 at 19:08
  • Yeah, that could maybe be documented a little better. Define a search engine. Each search engine you define has an ID, which you'll find in the `cx` parameter. Click on the search engine from the "My search engines" page. It should take you to a URL with a `cx` parameter. Just copy that parameter and pass it in via the client as a `cx` parameter to the API call you're trying to make. – Bob Aman Jun 12 '12 at 19:32
1

There's a few ins and outs with authentication when using an api key as opposed to OAuth thats outlined at the code abode.

You have to explicitly set the authorzation param to nil when constructing the client, otherwise the gem tries to use OAuth to authenticate, so if calling from a server using an api key only, you will always get a 401 Unauthorized. Full Code using the customsearch api is given (copy paste into irb). the code abode - google-api-client for ruby

Ben
  • 1,203
  • 13
  • 8
0

https://developers.google.com/google-apps/calendar/firstapp

This walks you through getting setup to access the api and setting up keys in google's api console. It has a tab for ruby - so this is what you need to get started.

Ziferius
  • 91
  • 1
  • 6