来自Ruby的Firestore - 无法加载默认凭据

时间:2018-05-10 08:31:29

标签: ruby firebase google-cloud-firestore

我正在尝试使用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

2 个答案:

答案 0 :(得分:0)

根据source code,您可以通过多种方式创建凭据。

在您的情况下,您的代码应该有效。尝试将json放入keyfile.jsoncredentials: '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-linuxauthentication/getting-started中找到我所采取的步骤,但我也在此处列出了这些步骤。

  1. 下载并安装gcloud

  2. 运行gcloud

    • gcloud init
  3. 选择默认云项目(不确定是否需要)

    • 在这个阶段,我有设置服务帐户和创建keyfile.json所需的工具。
    • 我确实需要退出终端并再次运行它,然后再转到第4步。
  4. 使用命令行

    设置服务帐户
    • gcloud iam service-accounts创建[NAME]
  5.   

    gcloud iam service-accounts创建my-service-account

    1. 分配特定的项目权限

      • gcloud projects add-iam-policy-binding [PROJECT_ID] - member“serviceAccount:[NAME] @ [PROJECT_ID] .i​​am.gserviceaccount.com”--role“roles / owner”
    2.   

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

      1. 生成密钥
      2.   

        gcloud iam service-accounts键创建google-cloud-key.json   --iam-account my-service-account@cool-project.iam.gserviceaccount.com

        1. 改变红宝石代码
        2. 规范测试中的示例代码

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