Reputation: 5081
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
Upvotes: 5
Views: 4982
Reputation: 834
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...
Upvotes: 4
Reputation: 1213
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.
Upvotes: 3
Reputation: 33259
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.'
}
)
Upvotes: 9
Reputation: 5030
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.
Upvotes: 0
Reputation: 5081
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.
Upvotes: 0
Reputation: 5081
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.
Upvotes: 0