Seb B.
Seb B.

Reputation: 69

Why does Google's Custom Search API say that I'm missing an access token when using the Ruby client?

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'
)

Upvotes: 4

Views: 2721

Answers (4)

balexand
balexand

Reputation: 9665

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 ...

Upvotes: 0

three
three

Reputation: 8478

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

Upvotes: 0

Achilles
Achilles

Reputation: 766

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

Upvotes: 0

Seb B.
Seb B.

Reputation: 69

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.

Upvotes: 2

Related Questions