将Google Custom Search API与Ruby google-api-client一起使用

时间:2011-06-09 13:05:04

标签: ruby google-api-client

作为我目前参与的人物搜索项目的一部分,我需要编写一个ruby脚本,可以将搜索查询发送到Google Custom Search API并存储搜索结果以进行处理。我找到了Ruby google-api-client gem(http://code.google.com/p/google-api-ruby-client/)并安装了它,但是,尽管已经彻底阅读了文档,但我感到很茫然。至于如何执行自定义搜索API调用。这是我第一次尝试使用Google API,我发现这个过程有点压倒性,有没有人可以提供一些示例代码供我学习?感谢

3 个答案:

答案 0 :(得分:2)

虽然我没有对此进行测试,但这样的事情应该有效:

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

请注意,我省略了所有用于身份验证的设置代码,您可以在Ruby客户端的文档中找到它。

答案 1 :(得分:1)

使用api密钥进行身份验证时会有一些问题,而不是the code abode中概述的OAuth。

在构造客户端时,您必须明确地将授权参数设置为nil,否则gem会尝试使用OAuth进行身份验证,因此如果仅使用api密钥从服务器调用,则始终会获得401 Unauthorized。使用customsearch api的完整代码(复制粘贴到irb中)。 the code abode - google-api-client for ruby

答案 2 :(得分:0)

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

这会引导您完成设置以访问api并在google的api控制台中设置密钥。 它有一个ruby选项卡 - 所以这就是你需要开始的。