RuntimeError:无法加载默认凭据

时间:2019-12-10 18:26:27

标签: ruby-on-rails ruby google-api google-translate google-cloud-translate

我已经在项目中安装了google/cloud/translate,Gemfile.lock中的版本是:

google-cloud-translate (2.1.0)

使用以下代码:

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

这就是documentation所说的,也是与answer相关的建议(请注意,我使用的是v2而不是v3)

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

此部分返回true:

require "google/cloud/translate"

更新 我已经按照以下步骤进行了操作:

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

创建了一个服务帐户,一个凭据密钥并设置了env变量(在Windows上),然后我尝试使用google/cloud/storage示例测试凭据配置,并且工作正常,但是当我尝试以下操作时:{{1 }}和

google/cloud/translate

我仍然遇到相同的错误

可能是什么错误? 预先感谢您的帮助。

2 个答案:

答案 0 :(得分:0)

  1. 创建服务帐户:

    gcloud iam service-accounts create rubyruby --description "rubyruby" --display-name "rubyruby"
    
  2. 获取服务帐户名称:

    gcloud iam service-accounts list
    
  3. 为您的服务帐户创建证书密钥文件:

    gcloud iam service-accounts keys create key.json --iam-account rubyruby@your-project.iam.gserviceaccount.com
    
  4. 设置环境变量:

    export GOOGLE_APPLICATION_CREDENTIALS=key.json
    
  5. 启用翻译API

  6. 安装库:

    gem install google-cloud-translate
    
  7. 编辑ruby.rb脚本

   # 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. 运行脚本:

    ruby ruby.rb
    
  2. 输出:

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

答案 1 :(得分:0)

您好,您需要将.json文件与google服务帐户的所有键一起使用,一旦将该文件放在项目的同一路径中,就可以执行以下代码:

    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

希望这很清楚:)...!