エラーの例
以下のようなエラーがでた場合、認証系のところでトラブってることになります
google.auth.exceptions.DefaultCredentialsError: Could not automatically determine credentials. Please set GOOGLE_APPLICATION_CREDENTIALS or explicitly create credential and re-run the application.
サービスアカウントの設定方法
GCPの管理画面でサービスアカウントキーを作成してダウンロード(jsonファイル)しておきます。認証設定しないといけない箇所は
- 環境変数の設定
- CREDENTIALS情報の読み込み
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import firebase_admin | |
from firebase_admin import credentials | |
from firebase_admin import firestore | |
import os | |
#環境変数 | |
os.environ['GOOGLE_APPLICATION_CREDENTIALS'] = './xxxxx-credentials.json' | |
#credentialsの読み込み | |
cred = credentials.Certificate('xxxxx-credentials.json') | |
firebase_admin.initialize_app(cred) | |
db = firestore.Client() |