我正在尝试使用Ruby中的Google Firestore(FireBase),而不确定如何为服务器通信加载凭据。
我正在从测试中运行此代码
it 'do something with firestore', focus: true do
firestore = Google::Cloud::Firestore.new(project_id: 'jg-jai-dev')
end
并收到以下错误
RuntimeError:
Could not load the default credentials. Browse to
https://developers.google.com/accounts/docs/application-default-credentials
for more information
# /home/david/.rvm/gems/ruby-2.4.1@scraper/gems/googleauth-0.6.2/lib/googleauth/application_default.rb:61:in `get_application_default'
# /home/david/.rvm/gems/ruby-2.4.1@scraper/gems/googleauth-0.6.2/lib/googleauth/credentials.rb:132:in `from_application_default'
# /home/david/.rvm/gems/ruby-2.4.1@scraper/gems/googleauth-0.6.2/lib/googleauth/credentials.rb:90:in `default'
# /home/david/.rvm/gems/ruby-2.4.1@scraper/gems/google-cloud-firestore-0.21.0/lib/google/cloud/firestore.rb:559:in `default_credentials'
# /home/david/.rvm/gems/ruby-2.4.1@scraper/gems/google-cloud-firestore-0.21.0/lib/google/cloud/firestore.rb:507:in `new'
# ./spec/services/export/firestore_job_export_spec.rb:220:in `block (3 levels) in <top (required)>'
当我查看文档时,似乎我需要在JSON文件中包含某种凭证文件,但我不确定在哪里找到此文件,我在https://console.firebase.google.com
中看不到它it 'where do I get the keyfile so that I can use Server authentication', focus: true do
firestore = Google::Cloud::Firestore.new(project_id: 'jg-jai-dev', credentials: "keyfile.json")
end
你实际上在哪里获得 KeyFile.json ?
答案 0 :(得分:0)
根据source code,您可以通过多种方式创建凭据。
在您的情况下,您的代码应该有效。尝试将json放入keyfile.json
与credentials: 'keyfile.json'
规范相同的文件夹中。
另一种选择是自己创建凭证对象:
creds = Google::Auth::Credentials.new private_key: 2048
Google::Cloud::Firestore.new(project_id: 'jg-jai-dev', credentials: creds)
答案 1 :(得分:0)
我必须安装gcloud并生成凭证文件。我可以在quickstart-linux和authentication/getting-started中找到我所采取的步骤,但我也在此处列出了这些步骤。
下载并安装gcloud
运行gcloud
选择默认云项目(不确定是否需要)
使用命令行
设置服务帐户gcloud iam service-accounts创建my-service-account
分配特定的项目权限
gcloud项目add-iam-policy-binding jg-jai-dev --member “serviceAccount:my-service-account@cool-project.iam.gserviceaccount.com” --roro“角色/所有者”
gcloud iam service-accounts键创建google-cloud-key.json --iam-account my-service-account@cool-project.iam.gserviceaccount.com
规范测试中的示例代码
it 'do something with firestore', focus: true do
firestore = Google::Cloud::Firestore.new(project_id: 'jg-jai-dev', credentials: "./google-cloud-key.json")
end