firebase_admin.initialize_app() using json instead of the file
eliezerp3 opened this issue · 3 comments
eliezerp3 commented
Hi. Is there any way to load the service_key using json instead of a path to the json file? I would rather not store that in version control. I would rather put it int an env variable and load it. This is what i tried already:
from .base import *
import json
from firebase_admin import initialize_app, credentials
from google.oauth2.service_account import Credentials
GOOGLE_JSON_CREDS = os.environ.get('GOOGLE_JSON_CREDS')
class CustomFirebaseCredentials(credentials.ApplicationDefault):
def _get_creds(self):
# Load raw json from env if exists else load from file
json_creds = None
if GOOGLE_JSON_CREDS:
json_creds = GOOGLE_JSON_CREDS
else:
with open(os.path.join(BASE_DIR, 'core', 'keys', 'fcm_service_key.json'), 'r') as f:
json_creds = f.read()
return json.loads(json_creds)
def _load_credential(self):
if not self._g_credential:
json_creds = self._get_creds()
self._project_id = json_creds['project_id']
self._g_credential = Credentials.from_service_account_info(json_creds, scopes=credentials._scopes )
custom_credentials = CustomFirebaseCredentials()
FIREBASE_MESSAGING_APP = initialize_app(custom_credentials, name='messaging')
FCM_DJANGO_SETTINGS = {
"DEFAULT_FIREBASE_APP": FIREBASE_MESSAGING_APP,
"ONE_DEVICE_PER_USER": False,
"DELETE_INACTIVE_DEVICES": False,
"UPDATE_ON_DUPLICATE_REG_ID": True,
}
But i am getting
ValueError: The default Firebase app does not exist. Make sure to initialize the SDK by calling initialize_app().
when i try to use it.
dickermoshe commented
Try:
FIREBASE_APP = initialize_app()
FCM_DJANGO_SETTINGS = {
"DEFAULT_FIREBASE_APP": None,
...
eliezerp3 commented
FIREBASE_APP = initialize_app()
FCM_DJANGO_SETTINGS = {
"DEFAULT_FIREBASE_APP": None,
That’s if want to use the path to file. I want to use raw json
eliezerp3 commented
I checked the lates commits of the fcm-django GitHub repo and realized the whole using custom DEFAULT_FIREBASE_APP setting was only recently added in latest release last month... I was using an older version.. I updated to latest release and now my original code works