【问题标题】:RuntimeError: Could not load the default credentialsRuntimeError:无法加载默认凭据
【发布时间】:2019-12-10 18:26:27
【问题描述】:

我在我的项目中安装了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 示例测试凭据配置,它工作正常,但是当我尝试使用:google/cloud/translate gem 时

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

我还是遇到了同样的错误

可能是什么错误? 提前感谢您的帮助。

【问题讨论】:

  • 你没看developers.google.com/accounts/docs/…吗?它告诉你去那个页面找出它为什么不起作用。当您转到该页面时,它提供了多个示例,说明如何将您的凭据加载到您的应用程序中,包括export GOOGLE_APPLICATION_CREDENTIALS。该页面上甚至还有 Ruby 代码示例来展示如何做到这一点。我在这里错过了什么?

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


【解决方案1】:

您好,您需要将 .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

希望这可以清楚:)...!

【讨论】:

  • 这个 location_id 是什么?这是哪里来的?
【解决方案2】:
  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
    

【讨论】:

  • 我已经按照cloud.google.com/docs/authentication/production 的所有步骤,创建了一个服务帐户、一个凭据密钥并设置了环境变量(在 Windows 上),然后我尝试使用“google/cloud”测试凭据配置/storage" 示例,它运行良好,但是当我尝试使用 translate = Google::Cloud::Translate.new version: :v2, project_id: project_id 使用 "google/cloud/translate" gem 时,我仍然遇到同样的错误
  • @ManuelCarrero 返回页面阅读全文。在.new 调用期间使用keyfile 有一个非常简单的示例,告诉它加载特定的密钥文件。您问题的所有答案都在该页面上。
猜你喜欢
  • 2021-01-06
  • 2020-04-28
  • 2017-07-13
  • 1970-01-01
  • 1970-01-01
  • 2021-01-02
  • 2018-11-23
  • 2017-05-19
  • 2017-06-21
相关资源
最近更新 更多