Google-api-ruby-client 翻译 API 示例

发布于 2024-10-18 15:26:47 字数 843 浏览 2 评论 0原文

我很高兴看到谷歌代码: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 技术交流群。

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

发布评论

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

评论(6

你在我安 2024-10-25 15:26:47

调用翻译 API 的代码如下所示:

require 'google/api_client'
client = Google::APIClient.new(:key => YOUR_DEVELOPER_KEY)
translate = client.discovered_api('translate', 'v2')
result = client.execute(
  :api_method => translate.translations.list,
  :parameters => {
    'format' => 'text',
    'source' => 'en',
    'target' => 'es',
    'q' => 'The quick brown fox jumped over the lazy dog.'
  }
)

Code for making calls to the translate API looks like this:

require 'google/api_client'
client = Google::APIClient.new(:key => YOUR_DEVELOPER_KEY)
translate = client.discovered_api('translate', 'v2')
result = client.execute(
  :api_method => translate.translations.list,
  :parameters => {
    'format' => 'text',
    'source' => 'en',
    'target' => 'es',
    'q' => 'The quick brown fox jumped over the lazy dog.'
  }
)
无尽的现实 2024-10-25 15:26:47

我最近在 Rails 5 项目中解决了这个问题,以下是我如何让它工作的。我认识到这是使用更新的 Google Gem - 但希望人们会找到这个更新的答案并发现它有用。

首先,您需要在 Google Console 中启用翻译 API - 我还在此处创建了一个服务帐户。我在这里完成了一个向导并获得了我的 json 凭证文件(见下文)。

首先:

/Gemfile

gem 'google-cloud-translate'

我将 ENV 设置添加到初始值设定项文件中。我还添加了一个项目字符串...花了我一段时间才弄清楚。确保将第二行的“google-project-name”设置为您的谷歌控制台项目名称!

/config/initializers/my_project_name_init.rb

ENV['GOOGLE_APPLICATION_CREDENTIALS'] = Rails.root.to_s + '/cert/my_project-translation-credentials.json'
GOOGLE_TRANSLATE_PROJECT_STRING = "projects/google-project-name/locations/us-central1"

之后就相当简单了。我在模型中添加了一个简单的方法:

require "google/cloud/translate"
client   = Google::Cloud::Translate.new
response = client.translate_text(["Let's go surfing"], 'fr', GOOGLE_TRANSLATE_PROJECT_STRING)
translation = response.translations&.first&.translated_text

我知道这是一个二年级的答案 - 但我希望这可以节省人们很多时间。我花了太长时间才明白这一点,谷歌的文档已经过时、冗余而且太糟糕了......

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

gem 'google-cloud-translate'

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

ENV['GOOGLE_APPLICATION_CREDENTIALS'] = Rails.root.to_s + '/cert/my_project-translation-credentials.json'
GOOGLE_TRANSLATE_PROJECT_STRING = "projects/google-project-name/locations/us-central1"

After this it's fairly easy. I added a simple method to a model:

require "google/cloud/translate"
client   = Google::Cloud::Translate.new
response = client.translate_text(["Let's go surfing"], 'fr', GOOGLE_TRANSLATE_PROJECT_STRING)
translation = response.translations&.first&.translated_text

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

无戏配角 2024-10-25 15:26:47

我在以下位置发布了有关身份验证问题和解决方法(使用 api 密钥)的完整代码和详细信息:
代码所在地 - google-api-client对于 ruby​​

安装 gem、获取 Google API 密钥并设置自定义搜索帐户(其首选项扩展到所有网页)....我可以使用以下命令在 irb 中搜索 google 搜索结果(将粘贴复制到 irb 中,然后在完成后检查响应):

  require 'openssl'
  OpenSSL::SSL::VERIFY_PEER = OpenSSL::SSL::VERIFY_NONE

  require 'google/api_client'
  client = Google::APIClient.new(:key => 'your-api-key', :authorization => nil)
  search = client.discovered_api('customsearch')

  response = client.execute(
    :api_method => search.cse.list,
    :parameters => {
      'q' => 'the hoff',
      'key' => 'your-api-key',
      'cx' => 'your-custom-search-id'
    }
  )

这是为了让服务器访问 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):

  require 'openssl'
  OpenSSL::SSL::VERIFY_PEER = OpenSSL::SSL::VERIFY_NONE

  require 'google/api_client'
  client = Google::APIClient.new(:key => 'your-api-key', :authorization => nil)
  search = client.discovered_api('customsearch')

  response = client.execute(
    :api_method => search.cse.list,
    :parameters => {
      'q' => 'the hoff',
      'key' => 'your-api-key',
      'cx' => 'your-custom-search-id'
    }
  )

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.

尘曦 2024-10-25 15:26:47

感谢鲍勃,我又向前迈进了一步。

我现在发现 .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.

浮光之海 2024-10-25 15:26:47

嗨(所有)感兴趣的人

更多进展。我为 错误报告 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 时成功。

简而言之,有一个问题需要解决,并且需要两项增强功能。

  1. 当底层(已知或检测到)使安全性、身份隐私和数据完整性失效时,API 客户端接口层应报告错误或警告
  2. 报告所使用的底层服务的相关配置信息(例如,是否使用了 SSL?是 VERIFY_PEER 还是 VERIFY_NONE?给出错误的完全限定 URI 是什么?对我来说,我认为该信息可以对日志文件进行排序和处理由服务类(类方法)上的选项和实例变量“请求”
  3. 为您的 Gem 设计提供诊断,并将诊断扩展到您自己的代码中(例如,进行的网络“执行”调用数量、执行次数等指标)。字节输入和字节输出)。

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.

  1. An API-client interface layer should report an Error or Warning when the underlying layers (as can be known or detected) invalidate security, identity-privacy and data integrity.
  2. Report on relevant configuration information for the underlying service used (e.g. was SSL used? Was it VERIFY_PEER or VERIFY_NONE? What is the fully qualified URI to give the error? For me, I think that info can do to a log-file of sorts and be 'Requested' by an Option on the service class (class methods) and by the instance variable.
  3. Provide diagnostics with your Gem design and extend diagnostics into your own code (e.g. metrics like: number of network 'execute' calls made, number of bytes in and bytes out).
七婞 2024-10-25 15:26:47

我在这里有一个解决方案(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.

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