将 Google 自定义搜索 API 与 Ruby google-api-client 结合使用

发布于 2024-11-14 19:54:07 字数 283 浏览 4 评论 0原文

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

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

浅笑轻吟梦一曲 2024-11-21 19:54:07

虽然我还没有对此进行测试,但类似这样的东西应该可以工作:

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 客户端的文档中找到这些代码。

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.

萌吟 2024-11-21 19:54:07

使用 api 密钥进行身份验证时有一些细节,而不是 代码所在

在构建客户端时,您必须显式地将authorzation参数设置为nil,否则gem会尝试使用OAuth进行身份验证,因此如果仅使用api密钥从服务器调用,您将始终得到401 Unauthorized。给出了使用 customsearch api 的完整代码(复制粘贴到 irb 中)。 代码所在地 - google-api-client对于红宝石

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

内心旳酸楚 2024-11-21 19:54:07

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

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

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文