簡體   English   中英

Ruby的Firestore-無法加載默認憑據

[英]Firestore from Ruby - Could not load the default credentials

我正在嘗試使用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

根據源代碼,您可以使用多種方法來創建憑據。

在您的情況下,您的代碼應該可以工作。 嘗試將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)

我必須安裝gcloud並生成一個憑證文件。 我采取的步驟可以在quickstart-linux認證/ 入門中找到,但我也在此處列出了它們。

  1. 下載並安裝gcloud

  2. 運行gcloud

    • gcloud初始化
  3. 選擇一個默認的雲項目(不確定是否需要)

    • 在此階段,我擁有設置服務帳戶和創建keyfile.json所需的工具。
    • 在進入步驟4之前,我確實需要退出終端並再次運行它。
  4. 使用命令行設置服務帳戶

    • gcloud iam服務帳戶創建[NAME]

gcloud iam服務帳戶創建我的服務帳戶

  1. 分配特定的項目權限

    • gcloud項目add-iam-policy-binding [PROJECT_ID]-成員“ serviceAccount:[NAME] @ [PROJECT_ID] .i​​am.gserviceaccount.com” --role“角色/所有者”

gcloud項目add-iam-policy-binding jg-jai-dev-成員“ serviceAccount:my-service-account@cool-project.iam.gserviceaccount.com”-角色“角色/所有者”

  1. 產生金鑰

gcloud iam服務帳戶密鑰創建google-cloud-key.json --iam帳戶my-service-account@cool-project.iam.gserviceaccount.com

  1. 更改紅寶石代碼

規范測試中的示例代碼

it 'do something with firestore', focus: true do
  firestore = Google::Cloud::Firestore.new(project_id: 'jg-jai-dev', credentials: "./google-cloud-key.json")
end

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM