Google-api-ruby-client 翻译 API 示例
我很高兴看到谷歌代码:google-api-ruby- client 项目,因为这对我来说意味着 Ruby 人员可以使用 Google API-s 完善代码。
目前,虽然我很困惑,因为给出的唯一示例使用 Buzz,并且从我的实验来看,Google Translate (v2) api 的行为必须与 google-api-ruby-client。
我对“Explorer”演示示例很感兴趣——但据我所知,它并不是一个浏览器。它所做的就是调用 Buzz 服务,然后查看它已经知道的有关 Buzz 服务的信息。对我来说,浏览器应该让您“发现”所公开的服务和方法/功能,而无需了解它们。
我很想知道 Ruby 命令行和桌面应用程序使用此功能: google-api-ruby-client 用于除 Buzz 之外的其他服务,特别是 Translate api(我较少 em> 对目前使用翻译服务的现有 Ruby gems 感兴趣)。
谢谢...会
I was very happy to see the google code: google-api-ruby-client project, because it meant to me that Ruby people can polish code with the Google API-s.
For now though I'm stumped because the only example given uses Buzz and from my experiments, the Google Translate (v2) api must behave quite differently to Buzz in the google-api-ruby-client.
I was intrigued by the 'Explorer' demo example -- But it isn't much of an explorer as far as I can see. All it does is call up a Buzz service and then pokes about in things it ALREADY knows about Buzz the services. To me, an explorer ought to let you 'discover' the services and the methods/functions exposed without necessarily knowing them already.
I'd love to hear of Ruby command line and desktop applications using this: google-api-ruby-client for services other than Buzz and in particular the Translate api (I'm less interested in the existing Ruby gems using the translate service at this point).
thanks ... will
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
调用翻译 API 的代码如下所示:
Code for making calls to the translate API looks like this:
我最近在 Rails 5 项目中解决了这个问题,以下是我如何让它工作的。我认识到这是使用更新的 Google Gem - 但希望人们会找到这个更新的答案并发现它有用。
首先,您需要在 Google Console 中启用翻译 API - 我还在此处创建了一个服务帐户。我在这里完成了一个向导并获得了我的 json 凭证文件(见下文)。
首先:
/Gemfile
我将 ENV 设置添加到初始值设定项文件中。我还添加了一个项目字符串...花了我一段时间才弄清楚。确保将第二行的“google-project-name”设置为您的谷歌控制台项目名称!
/config/initializers/my_project_name_init.rb
之后就相当简单了。我在模型中添加了一个简单的方法:
我知道这是一个二年级的答案 - 但我希望这可以节省人们很多时间。我花了太长时间才明白这一点,谷歌的文档已经过时、冗余而且太糟糕了......
I recently tackled this in a Rails 5 project and here is how I got it to work. I recognize this is using a newer Google Gem - but hopefully people will find this more recent answer and find it useful.
First you will need to enable the translation API in the Google Console - and I also created a service account here. I walked through a wizard here and got my json credentials file (see below).
First:
/Gemfile
I added the ENV setting to an initializer file. I also added a project string... took me a while to figure out. Make sure to set 'google-project-name' on the second line to your google console project name!
/config/initializers/my_project_name_init.rb
After this it's fairly easy. I added a simple method to a model:
I know this is a sophomoric answer - but I hope this saves someone a lot of time. It took me way too long to get to this, google's docs are outdated, redundent and just awful...
我在以下位置发布了有关身份验证问题和解决方法(使用 api 密钥)的完整代码和详细信息:
代码所在地 - google-api-client对于 ruby
安装 gem、获取 Google API 密钥并设置自定义搜索帐户(其首选项扩展到所有网页)....我可以使用以下命令在 irb 中搜索 google 搜索结果(将粘贴复制到 irb 中,然后在完成后检查响应):
这是为了让服务器访问 google api 并绕过所有 oauth 内容。最重要的一点是构建客户端时的 :authorization 参数...这确保在调用时使用 api 密钥,而不是 oauth。如果没有它,您每次都会收到 401 未经授权的响应状态。
I posted up full code and detail for auth issues and workarounds (using an api key) at
the code abode - google-api-client for ruby
After installing the gem, getting a Google API Key, and setting up a custom search account (with its prefs widened to all web pages).... I could trawl google search results in irb with the following (copy paste into irb, then inspect response when finished):
This is to get server access to the google api and bypass all the oauth stuff. THE MOST IMPORTANT BIT was the :authorization param when constructing he client.... this ensures the api key is used when calling, in preference to oauth. Without it you will get 401 Unauthorized response status everytime.
感谢鲍勃,我又向前迈进了一步。
我现在发现 .CRT 证书文件有问题。我有一个从 cURL 网站 (http://curl.haxx .se/ca)。
另请阅读:如何治愈NetHhttps 有风险的默认 HTTPS 行为
我认为下一个问题是寻找 OpenSSL 的捆绑包。
w。
Thanks to Bob, I'm one step forward.
I'm now finding trouble with the .CRT certificates file. I have a CA-bundle I got from the CA-bundle generator available on the cURL site (http://curl.haxx.se/ca).
Read also: How to Cure NetHhttps Risky Default HTTPS Behavior
I think the next question needs to be about finding the bundle for OpenSSL.
w.
嗨(所有)感兴趣的人,
更多进展。我为 错误报告 a href="http://code.google.com/p/google-api-ruby-client/" rel="nofollow">Google-API-Ruby-client 解决此问题的一部分。简短的版本是(至少)翻译的 Google-API-Ruby- client 在 SSL::VERIFY_PEER 下失败,在使用 SSL::VERIFY_NONE 时成功。
简而言之,有一个问题需要解决,并且需要两项增强功能。
Hi (all) interested folks,
More progress. I put a bug report for Google-API-Ruby-client for part of this problem. The short version is that for Translate (at least) the Google-API-Ruby-client fails under SSL::VERIFY_PEER and succeeds when SSL::VERIFY_NONE is used.
In a nut shell there's one problem to fix and two enhancements needed.
我在这里有一个解决方案(http://jjinux.blogspot.com/2012/02/ruby-working-around-ssl-errors-on-os-x.html),猴子按顺序修补 Net::HTTP#use_ssl=使用操作系统的根证书。
I have a solution here (http://jjinux.blogspot.com/2012/02/ruby-working-around-ssl-errors-on-os-x.html) that monkey patches Net::HTTP#use_ssl= in order to use the operating system's root certificates.