首页 > 解决方案 > 无法在 Rails 5.1 中加载 google-cloud-text_to_speech Gem 的默认凭据

问题描述

在我的Rails 5.1.7应用程序中,我想使用 a 中的方法rake-task将文本转换为 mp3 音频文件。我正在使用google-cloud-text_to_speech gem这样做。这是我设置的一部分:

/Gemfile

gem 'google-cloud-text_to_speech'

这是task-file从任务中调用的方法:

/lib/tasks/sync.rake

require "google/cloud/text_to_speech"

def create_speech(content, id)
  text_to_speech_client = Google::Cloud::TextToSpeech.new
  text = content
  input = { text: text }
  language_code = "nl-NL"
  voice = { language_code: language_code }
  audio_encoding = :MP3
  audio_config = { audio_encoding: audio_encoding }
  response = text_to_speech_client.synthesize_speech(input, voice, audio_config)
  File.write("speach_id_#{id}.mp3", response.audio_content, mode: "wb")
  puts "Created speach_id_#{id}.mp3"
end

我根据以下说明对其进行了身份验证: https ://googleapis.dev/ruby/google-cloud-text_to_speech/latest/file.AUTHENTICATION.html

/config/application.rb

config.speech = config_for(:speech)

/config/speech.yml

google:
  project: ENV=["TEXTTOSPEECH_PROJECT"]
  credentials: ENV=["TEXTTOSPEECH_CREDENTIALS"]

我正在使用Figaro我的环境变量,因此我有一个application.yml文件。

/config/application.yml

development:
  TEXTTOSPEECH_PROJECT: "project-id"
  TEXTTOSPEECH_CREDENTIALS: "/path/to/file.json"

然后我运行我的任务:

$ bundle exec rake sync:feeds

当我在没有额外方法的情况下运行 rake 任务时,一切正常。但是当我使用该方法时,我得到:

"rake aborted!
Could not load the default credentials. Browse to
https://developers.google.com/accounts/docs/application-default-credentials"

我尝试了许多不同的方法来提供凭据,但我一次又一次地遇到相同的错误。有没有人可以帮助我?

标签: ruby-on-railsenvironment-variablesruby-on-rails-5google-text-to-speechfigaro-ruby

解决方案


推荐阅读