Tipping44
Tipping44

Reputation: 281

Firebase Admin SDK credentials certificate not validating

After creating a chatbot in Dialogflow I want to connect this to my PyCharm environment, my end goal is to create a GUI within Python and allow it to connect through Dialogflow back-end, I also have a Firestore database and a few API's set up.

I have read to connect PyCharm to the Dialogflow (and, the Google Cloud platform) I need to use the Firebase-Admin SDK, which has been installed through PIP.

import dialogflow_v2beta1
from google.cloud import firestore
import firebase_admin
from firebase_admin import credentials


#Initialize the Admin SDK
cred = credentials.Certificate('C:Users\folder1\folder2\chatbot.json')
default_app = firebase_admin.initialize_app(cred)

#The below is a default test hoping to write a new document to the Firestore Database to check the connection works.
   doc_ref = db.collection(u'users').document(u'alovelace')
    doc_ref.set({
        u'first': u'Ada',
        u'last': u'Lovelace',
        u'born': 1815
    })

So, with the above I simply hope to connect my environment to my chatbot through the Google platform and when I run this code I hope for some data to be created in my Firestore database.

The error I get when I run the above is:

C:\Users\Me\PycharmProjects\Chatbot\venv\Scripts\python.exe C:/Users/Me/PycharmProjects/Chatbot/venv/Chatbot.py
Traceback (most recent call last):
  File "C:/Users/Me/PycharmProjects/Chatbot/venv/Chatbot.py", line 12, in <module>
    cred = credentials.Certificate('C:Users\folder1\folder2\chatbot.json')
  File "C:\Users\Me\PycharmProjects\Chatbot\venv\lib\site-packages\firebase_admin\credentials.py", line 83, in __init__
    with open(cert) as json_file:
IOError: [Errno 2] No such file or directory: 'C:Users\\folder1\\folder2\\chatbot.json'

Process finished with exit code 1

In short, I've checked the line 83 error in the credentials.py file where the default comment suggests means the file can't be found, but is correct as far as I can tell. The only thing I notice are the two \ in the error.

Any help would be much appreciated.

UPDATE This has erased that error, but now being shown another three:

SNIMissingWarning: An HTTPS request has been made, but the SNI (Subject Name Indication) extension to TLS is not available on this platform. This may cause the server to present an incorrect TLS certificate, which can cause validation failures. You can upgrade to a newer version of Python to solve this. For more information, see https://urllib3.readthedocs.io/en/latest/advanced-usage.html#ssl-warnings

InsecurePlatformWarning: A true SSLContext object is not available. This prevents urllib3 from configuring SSL appropriately and may cause certain SSL connections to fail. You can upgrade to a newer version of Python to solve this. For more information, see https://urllib3.readthedocs.io/en/latest/advanced-usage.html#ssl-warnings

google.api_core.exceptions.PermissionDenied: 403 Missing or insufficient permissions.

Upvotes: 1

Views: 6226

Answers (5)

Alberto Gomez
Alberto Gomez

Reputation: 1

I got the same error (MAC USER)

Code fb_test.py

import firebase_admin
from  firebase_admin import db
from firebase_admin import credentials

cred = credentials.Certificate("<dir>cred.json")
firebase_admin.initialize_app(cred, {'databaseURL':'https://<URL>'})

I ended adding the verify=False parameter on /Users/<user>/.pyenv/versions/3.9.11/lib/python3.9/site-packages/google/auth/transport/requests.py

response = self.session.request(method, url, data=body, headers=headers, 
timeout=timeout, verify=False, **kwargs)

Upvotes: 0

snow
snow

Reputation: 1

be careful

use this C:/../ instead c:\ ... \

don't forget:

cred = credentials.Certificate('C:/Users/ASPIREone/PycharmProjects/amazon/tester/serviceAccountKey.json')

firebase_admin.initialize_app(cred, {
    'databaseURL': 'https://hrd-line.firebaseio.com'
})

db = firestore.client()

doc_ref = db.collection(u'users').document(u'president')
doc_ref.set({
    u'first': u'Barrack',
    u'last': u'Obama',
    u'born': 1815
})

Upvotes: 0

caspii
caspii

Reputation: 869

I had this problem too. It was caused by an old Python version (2.7.6) on Ubuntu 14.04.

Firebase requires SSLContext which was introduced in 2.7.9. I fixed it using this howto.

Upvotes: 0

Tipping44
Tipping44

Reputation: 281

Solved the additional errors by;

import requests.packages.urllib3
requests.packages.urllib3.disable_warnings()

Upvotes: 1

Hiranya Jayathilaka
Hiranya Jayathilaka

Reputation: 7438

That path is wrong as far as I can tell. Should be C:\Users\folder1\folder2\chatbot.json. You're missing \ after C:.

Upvotes: 1

Related Questions