4

I'm trying to use Google's Custom Search API through the Google API Ruby client. I have setup my API key through the Google API console, and have also created my CSE. Based on the documentation, it seems that, as long as I provide an API key (which I am doing), I shouldn't need an OAuth2 authentication token to call the list method. However, when I try to execute the code below, I get the following error:

ArgumentError: Missing access token.

What am I missing? Here's my code:

# create client
client = Google::APIClient.new

# Fetch discovery doc
search = client.discovered_api('custom search')

# Call list method
response = client.execute(
  search.cse.list, 'key' => '<my API key>', 'cx' => '<my CSE id>', 'alt' => 'json', 'q' => 'hello world'
)
Moshe
  • 57,511
  • 78
  • 272
  • 425
Seb B.
  • 69
  • 1
  • 5

4 Answers4

2

I believe this is in fact a bug in the client (it's in alpha). After fiddling with it a little more, I've found a workaround:

just after creating the client object, assign it a "dummy" access token:

client.authorization.access_token = '123'

then you can call the search.cse.list method without getting the 'ArgumentError: Missing access token.' error.

N.N.
  • 8,336
  • 12
  • 54
  • 94
Seb B.
  • 69
  • 1
  • 5
  • 1
    You could also just do `client.authorization = nil`. This will tell google to rely on api key rather than credentials. http://stackoverflow.com/a/10825773/838874 – Agent47DarkSoul Oct 02 '13 at 07:27
0

If you're just after using Google CSE with ruby, try google-cse. I just built the gem, although I've been using it for a while privately. Much easier to work with than the alpha client

Achilles
  • 766
  • 4
  • 13
0

I found out that adding client.retries = 3 to my code solves this problem.

three
  • 8,262
  • 3
  • 35
  • 39
0

With the current version of the gem (0.7.1), you need to set the authorization to nil in addition to setting the key:

require 'google/api_client'

client = Google::APIClient.new
client.key = ENV['GOOGLE_API_KEY']
client.authorization = nil

client.execute ...
balexand
  • 9,549
  • 7
  • 41
  • 36