Manuel Carrero
Manuel Carrero

Reputation: 637

RuntimeError: Could not load the default credentials

i've installed google/cloud/translate in my project, the version in the Gemfile.lock is:

google-cloud-translate (2.1.0)

With the below code:

require "google/cloud/translate"
project_id = "<Project ID>" # from my Google Cloud Platform
translate = Google::Cloud::Translate.new version: :v2, project_id: project_id

That is what the documentation says and also what this answer related suggest (please note that i'm using v2 instead of v3)

RuntimeError: Could not load the default credentials. Browse to
https://developers.google.com/accounts/docs/application-default-credentials for more information

This part returns true:

require "google/cloud/translate"

Update I already followed all the steps in:

https://cloud.google.com/docs/authentication/production

Created a service account, a credential key and set the env variable (on Windows), then I tried testing the credential configuration with the google/cloud/storage example and it's worked fine, but when I tried with: google/cloud/translate gem with

translate = Google::Cloud::Translate.new version: :v2, project_id: project_id

I still got the same error

What can be the error? Thanks in advance for any help.

Upvotes: 0

Views: 2630

Answers (2)

Daniel Pe&#241;aloza
Daniel Pe&#241;aloza

Reputation: 98

Hi for doing this you will need to have your .json file with all the keys of your google service account, once that you have that file in the same path of your project you could do something like the following code:

    module GoogleTranslator
        require "google/cloud/translate"
        ENV["GOOGLE_APPLICATION_CREDENTIALS"] = "yourfile.json"
        class Translate
            attr_reader :project_id, :location_id, :word, :target_language
            def initialize(project_id, location_id, word, target_language)
                @project_id         = project_id
                @location_id        = location_id
                @word               = word
                @target_language    = target_language
            end
    
            def translate
                client = Google::Cloud::Translate.translation_service
                parent = client.location_path project: project_id, location: location_id
                response = client.translate_text(parent: parent, contents: word.words.to_a, target_language_code: target_language)
                translate_content(response)
            end
    
            def translate_content(response)
                word.translation(response)
            end
        end
    end
    
    class Word
        attr_reader :words
        def initialize(words)
            @words = words
        end
    
        def translation(words)
            words.translations.each do |word|
                puts word.translated_text
            end
        end
    end
    
    module GoogleTranslatorWrapper
        def self.translate(project_id:, location_id:, word:, target_language:)
            GoogleTranslator::Translate.new(project_id, location_id, word, target_language)
        end
    end
    
    GoogleTranslatorWrapper.translate(project_id: "your_project_id_on_google", location_id: "us-central1", word: Word.new(["your example word"]), target_language: "es-mx").translate

Hope this can be clear :)...!

Upvotes: 2

marian.vladoi
marian.vladoi

Reputation: 8066

  1. Create a service account:

    gcloud iam service-accounts create rubyruby --description "rubyruby" --display-name "rubyruby"
    
  2. Get the service account name:

    gcloud iam service-accounts list
    
  3. Create the credential key file for your service account:

    gcloud iam service-accounts keys create key.json --iam-account [email protected]
    
  4. Set the env variable:

    export GOOGLE_APPLICATION_CREDENTIALS=key.json
    
  5. Enable the translate API

  6. Install the library:

    gem install google-cloud-translate
    
  7. Edit the ruby.rb script

   # project_id = "Your Google Cloud project ID"
   # text       = "The text you would like to detect the language of"

   require "google/cloud/translate"
   text = 'I am home'

   translate = Google::Cloud::Translate.new version: :v2, project_id: project_id
   detection = translate.detect text

   puts "'#{text}' detected as language: #{detection.language}"  
   puts "Confidence: #{detection.confidence}"

  1. Run the script:

    ruby ruby.rb
    
  2. Output:

    'I am home' detected as language: en
    Confidence: 1
    

Upvotes: 1

Related Questions