使用 python-firestore AsyncClient 和 firebase

Bad*_*ads 3 python google-authentication firebase google-cloud-firestore

目前我已经设置了 firebase 帐户。我希望通过 python asyncio 将项目添加到该项目的 firestore 中。

据我了解,包:python-firestore确实支持异步通过AsyncClient.

python 包firebase_admin目前不支持异步。所以我想知道是否可以在没有firebase_admin.

firebase_管理员:

import firebase_admin
from firebase_admin import credentials    
cred = credentials.Certificate("path/to/serviceAccountKey.json")
firebase_admin.initialize_app(cred)
Run Code Online (Sandbox Code Playgroud)

python-firestore:

from google.cloud.firestore import AsyncClient    
client = AsyncClient(credentials=???)
Run Code Online (Sandbox Code Playgroud)

Bad*_*ads 6

在深入研究源代码后,我自己找到了答案。

from google.cloud.firestore import AsyncClient 
from google.oauth2 import service_account     
with open("path/to/serviceAccountKey.json") as json_file:
    json_data = json.load(json_file)
firestore_client = AsyncClient(
    project=json_data['project_id'],
    credentials=service_account.Credentials.from_service_account_info(json_data),
)
Run Code Online (Sandbox Code Playgroud)