Skip to content
Advertisement

Using Environment Variables as Credentials on Heroku

I need to make the “google-credentials.json” file works on heroku it’s working locally by putting the local path on my laptop but in heroku I don’t know how to do it.

I search a lot, I found this solutions How to use Google API credentials json on Heroku? but I couldn’t make it works maybe the solution is old or I did it wrong the solution is to:

1 – Declare your env variables from in Heroku dashboard The GOOGLE_CREDENTIALS variable is the content of service account credential JSON file as is. The GOOGLE_APPLICATION_CREDENTIALS env variable in the string “google-credentials.json”

2 – Once variables are declared, add the builpack from command line :

$ heroku buildpacks:add https://github.com/elishaterada/heroku-google-application-credentials-buildpack

3 – Make a push. Update a tiny thing and push.

This is my vars and Buildpacks: This is my vars and Buildpacks

This is the Error Logs:

2022-03-11T09:22:57.866153+00:00 app[worker.1]: Traceback (most recent call last):
2022-03-11T09:22:57.866234+00:00 app[worker.1]:   File "/app/Professor_Bot.py", line 102, in <module>
2022-03-11T09:22:57.866678+00:00 app[worker.1]:     cred_obj = firebase_admin.credentials.Certificate(os.environ['GOOGLE_APPLICATION_CREDENTIALS'])
2022-03-11T09:22:57.866696+00:00 app[worker.1]:   File "/app/.heroku/python/lib/python3.9/site-packages/firebase_admin/credentials.py", line 83, in __init__
2022-03-11T09:22:57.866909+00:00 app[worker.1]:     with open(cert) as json_file:
2022-03-11T09:22:57.866987+00:00 app[worker.1]: FileNotFoundError: [Errno 2] No such file or directory: "'google-credentials.json'"
2022-03-11T09:22:58.491014+00:00 heroku[worker.1]: Process exited with status 1
2022-03-11T09:22:58.644442+00:00 heroku[worker.1]: State changed from up to crashed

This is the python code:

import firebase_admin
from firebase_admin import credentials
from firebase_admin import db
import json
import os


cred_obj = firebase_admin.credentials.Certificate(os.environ['GOOGLE_APPLICATION_CREDENTIALS'])

default_app = firebase_admin.initialize_app(cred_obj, {
    'databaseURL': 'https://professorbot-325702-default-rtdb.firebaseio.com/'})
ref = db.reference("/")
ref.set({
        'boxes':
            {
                'box001': {
                    'color': 'red',
                    'width': 1,
                    'height': 3,
                    'length': 2
                },
                'box002': {
                    'color': 'green',
                    'width': 1,
                    'height': 2,
                    'length': 3
                },
                'box003': {
                    'color': 'yellow',
                    'width': 3,
                    'height': 2,
                    'length': 1
                }
            }
        })

And I would someone can help me thanks a Lot

Advertisement

Answer

Thanks God I finally Solved this issue Instead of Using Environment Variables which have the credentials file name and the json credential script and then using Buildpack to Generate local path You Just need to Create New Json File then write the credentials script inside and finally pass the new Json File name as a parameter to the LoadCredentialsFile Function Like This:

firebase_credentials = {
  "type": "",
  "project_id": "",
  "private_key_id": "",
  "private_key": "-----BEGIN PRIVATE KEY-----
-----END PRIVATE KEY-----n",
      "client_email": "",
      "client_id": "",
      "auth_uri": "",
      "token_uri": "",
      "auth_provider_x509_cert_url": "",
      "client_x509_cert_url": ""
    }
    with open("firebase_credentials.json", "w") as write_file:
        json.dump(firebase_credentials, write_file)
    cred_obj = firebase_admin.credentials.Certificate('firebase_credentials.json')
User contributions licensed under: CC BY-SA
3 People found this is helpful
Advertisement