Google Vision API authentication on heroku

≯℡__Kan透↙ 提交于 2019-12-07 16:02:17

问题


What is a best, simple way to authenticate Vision API on heroku?

In development I just use:

@vision = Google::Cloud::Vision.new( project:  "instacult",
                                     keyfile:  "path/to/keyfile.json" )

Where keyfile is a json produced by google after creating service account (https://cloud.google.com/vision/docs/common/auth).

But obviously I can't just upload the keyfile to github.

I tried saving whole json to Heroku's config vars and running:

Rails.env.production? ? ENV["GOOGLE_CREDENTIALS"] : path

But I got "is not a valid file" in heroku's logs. Seems logical since I'm not passing a file but an object. But how to get over it?

Cheers, Kai


回答1:


SOLVED:

Turns out you can provide a json object in environment variable, but there is a naming convention.

Here are the environment variables (in the order they are checked) for credentials:

  1. VISION_KEYFILE - Path to JSON file
  2. GOOGLE_CLOUD_KEYFILE - Path to JSON file
  3. VISION_KEYFILE_JSON - JSON contents
  4. GOOGLE_CLOUD_KEYFILE_JSON - JSON contents

source: https://googlecloudplatform.github.io/google-cloud-ruby/#/docs/google-cloud-vision/v0.23.0/guides/authentication

So I ended up with calling:

@vision = Google::Cloud::Vision.new( project:  "instacult")

Having set VISION_KEYFILE_JSON in my ~/.bashrc:

export VISION_KEYFILE_JSON='the_json_content'

and on heroku (https://devcenter.heroku.com/articles/config-vars#limits).



来源:https://stackoverflow.com/questions/42937145/google-vision-api-authentication-on-heroku

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!