我正在尝试在Rails应用程序中使用Cloud Natural Language服务。 当我使用简单的ruby文件和环境变量进行设置时,以及在运行以下代码时,我可以访问或运行我的代码:
require "google/cloud/language"
text_content = "
Google Cloud Natural Language API: Google Cloud Natural Language API provides natural language understanding technologies to developers. Examples include sentiment analysis, entity recognition, and text annotations."
language = Google::Cloud::Language.new
response = language.analyze_sentiment(content: text_content, type: :PLAIN_TEXT)
categories = response
puts categories.inspect
工作正常,我使用以下命令导出了变量:
export GOOGLE_APPLICATION_CREDENTIALS="/home/user/Downloads/service-account-file.json"
我将我的ruby代码放入了rails控制器中,但是我收到了以下消息:
Could not load the default credentials. Browse to https://developers.google.com/accounts/docs/application-default-credentials for more information
有什么方法可以使用json文件对GCP进行身份验证。