首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >RuntimeError:无法加载默认凭据

RuntimeError:无法加载默认凭据
EN

Stack Overflow用户
提问于 2019-12-10 18:26:27
回答 2查看 1.6K关注 0票数 0

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

代码语言:javascript
复制
google-cloud-translate (2.1.0)

使用以下代码:

代码语言:javascript
复制
require "google/cloud/translate"
project_id = "<Project ID>" # from my Google Cloud Platform
translate = Google::Cloud::Translate.new version: :v2, project_id: project_id

这是文档所说的,也是回答相关的建议(请注意,我使用的是v2而不是v3)

代码语言:javascript
复制
RuntimeError: Could not load the default credentials. Browse to
https://developers.google.com/accounts/docs/application-default-credentials for more information

此部分返回true:

代码语言:javascript
复制
require "google/cloud/translate"

Update I已经执行了以下所有步骤:

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

我还是犯了同样的错误

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

EN

回答 2

Stack Overflow用户

发布于 2020-09-18 03:43:38

为此,您将需要使用google帐户的所有密钥的.json文件,一旦您在项目的相同路径中拥有该文件,您可以执行如下代码:

代码语言:javascript
复制
    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

希望这能清楚:)!

票数 2
EN

Stack Overflow用户

发布于 2019-12-10 20:49:56

  1. 创建一个服务帐户: gcloud iam服务-帐户创建rubyruby -描述"rubyruby“显示名称"rubyruby”
  2. 获取服务帐户名称: gcloud iam服务-帐户列表
  3. 为服务帐户创建凭据密钥文件: gcloud服务-帐户键创建key.json --账户rubyruby@you-project.iam.gserviceAccount.com
  4. 设置env变量: 导出GOOGLE_APPLICATION_CREDENTIALS=key.json
  5. 启用转换API
  6. 安装库: gem安装google-云翻译
  7. 编辑ruby.rb脚本
代码语言:javascript
复制
   # 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.rb
  2. 输出: “我在家”被认为是一种语言:信心:1
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/59273317

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档