如何在 Rails 上使用 Ruby 中的 ENV 变量验证 Google Vision/Cloud

How to Authenticate Google Vision/Cloud Using ENV Variable in Ruby on Rails

我的应用程序托管在 Heroku 上,所以我想弄清楚如何使用 JSON Google 云提供(以进行身份​​验证)作为环境变量,但到目前为止我可以未通过身份验证。

我搜索了 Google 和 Stack Overflow,我找到的最佳线索是:

Google Vision API authentication on heroku

他们都说他们能够让它工作,但他们没有提供我已经能够工作的代码。有人可以帮帮我吗?我知道这可能是愚蠢的事情。

我目前正尝试利用 Google 中的示例代码测试我的产品模型中的服务。我的看起来像这样:

def self.google_vision_labels
  # Imports the Google Cloud client library
  require "google/cloud/vision"

  # Your Google Cloud Platform project ID
  project_id = "foo"

  # Instantiates a client
  vision = Google::Cloud::Vision.new project: project_id

  # The name of the image file to annotate
  file_name = "http://images5.fanpop.com/image/photos/27800000/FOOTBALL-god-sport-27863176-2272-1704.jpg"

  # Performs label detection on the image file
  labels = vision.image(file_name).labels

  puts "Labels:"
  labels.each do |label|
    puts label.description
  end
 end

我一直收到这个错误,

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

根据我阅读的内容,我尝试将 JSON 内容放在 secrets.yml 中(我使用的是 Figaro gem),然后在 gem 中引用它=56=] 文件基于这个 SO 问题的答案。

application.yml中,我把(为了安全,我在post中覆盖了一些内容):

GOOGLE_APPLICATION_CREDENTIALS: {
  "type": "service_account",
  "project_id": "my_project",
  "private_key_id": "2662293c6fca2f0ba784dca1b900acf51c59ee73",
  "private_key": "-----BEGIN PRIVATE KEY-----\n #keycontents \n-----END PRIVATE KEY-----\n",
  "client_email": "foo-labels@foo.iam.gserviceaccount.com",
  "client_id": "100",
  "auth_uri": "https://accounts.google.com/o/oauth2/auth",
  "token_uri": "https://accounts.google.com/o/oauth2/token",
  "auth_provider_x509_cert_url": 
  "https://www.googleapis.com/oauth2/v1/certs",
  "client_x509_cert_url": 
  "https://www.googleapis.com/robot/v1/metadata/x509/get-product-labels%40foo.iam.gserviceaccount.com"
}

config/google.yml 中,我输入:

GOOGLE_APPLICATION_CREDENTIALS = ENV["GOOGLE_APPLICATION_CREDENTIALS"]

也试过:

GOOGLE_APPLICATION_CREDENTIALS: ENV["GOOGLE_APPLICATION_CREDENTIALS"]

我还尝试根据此 Google 页面更改两个文件中的这些变量名称,而不是 GOOGLE_APPLICATION_CREDENTIALSGOOGLE_CLOUD_KEYFILE_JSONVISION_KEYFILE_JSON

有人可以帮助我理解我在 referencing/creating 具有 JSON 凭据的环境变量中做错了什么吗?谢谢!

Google 决定通过文件而不是一系列环境变量来存储秘密来破坏事实上的凭证标准,这真的很烦人。

也就是说,我对这个问题的解决方案是创建一个 .env 变量 GOOGLE_API_CREDS

我将原始 JSON blob 粘贴到 .env 中,然后删除所有换行符。然后在应用程序代码中,我使用 JSON.parse(ENV.fetch('GOOGLE_API_CREDS') 将 JSON blob 转换为真正的散列:

.env 文件:

  GOOGLE_API_CREDS={"type": "service_account","project_id": "your_app_name", ... }

然后在应用代码中(GoogleOCR客户端为例):

  Google::Cloud::Vision::ImageAnnotator.new(credentials: JSON.parse(ENV.fetch('GOOGLE_API_CREDS'))

干杯

根据 Dylan 的回答,我发现我需要使用额外的一行来配置凭据,如下所示:

Google::Cloud::Language.configure {|gcl| gcl.credentials = JSON.parse(ENV['GOOGLE_APP_CREDS'])}

因为 .new(credentials: ...) 方法不适用于 Google::Cloud::Language

必须查看 Google Cloud Language 的(稀疏)ruby 参考部分。

是的...将秘密存储在文件中确实很烦人。

我使用 Google 中的 "Getting Started" 文档对 Google Cloud Speech 遇到了同样的问题。

以上回答帮助很大,同时将我的 Google 演讲 Gem 更新为 V1 (https://googleapis.dev/ruby/google-cloud-speech-v1/latest/Google/Cloud/Speech/V1/Speech/Client.html)

我只是使用了一个 StringIO 对象,这样 Psych 就认为它是我阅读的实际文件:

google:
  service: GCS
  project: ''
  bucket: ''
  credentials: <%= StringIO.new(ENV['GOOGLE_CREDENTIALS']) %>