Can't set serviceAccountKey for Cloud Firestore with Python

时间:2018-04-27 22:23:35

标签: python firebase google-cloud-firestore firebase-admin

I am trying to access to the Cloud Firestore db of my Firebase project. I have a very simple code, I just want to connect to my database and add something to it, but unfortunately I can't set the serviceAccountKey properly, therefore I get the following error:

raise exceptions.DefaultCredentialsError(_HELP_MESSAGE)
google.auth.exceptions.DefaultCredentialsError: Could not automatically determine credentials. Please set GOOGLE_APPLICATION_CREDENTIALS or
explicitly create credential and re-run the application. For more
information, please see
https://developers.google.com/accounts/docs/application-default-credentials.

Of course I understand this message and I have visited the Google Developers link时创建一个将在侧边栏后面的部分,但我找不到任何可能对我有用的内容。

必须添加我正在尝试this Firebase tutorial,因此我按照了它的示例代码,并使用serviceAccountKey.json文件进行验证。

所以这是我的代码,它给出了错误:

import firebase_admin
from firebase_admin import credentials
from firebase_admin import firestore

cred = credentials.Certificate('serviceAccountKey.json')
firebase_admin.initialize_app(cred)

db = firestore.client()

doc_ref = db.collection(u'dev').document(u'alovelace')
doc_ref.set({
    u'first': u'Ada',
    u'last': u'Lovelace',
    u'born': 1815
})

我尝试了两个不同的帐户密钥,从Firebase管理员内部的两个不同位置下载。我假设正确的是我在Settings > Service Accounts > Firebase Admin SDK找到的 - 所以我已经下载了生成的密钥,复制到我的apps文件夹并将其重命名为serviceAccountKey.json,然后运行代码并从顶部获取错误这个问题。

如果有关于该主题的更多经验的人能够判断我是否在我的代码中犯了明显的错误,或者任何其他建议对于如何修复此问题有帮助,我将非常感激。

0 个答案:

没有答案