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

StackOverflow https://stackoverflow.com/questions/8013648

Вопрос

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'
)
Это было полезно?

Решение

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.

Другие советы

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

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

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 ...
Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top