gcp firestore python credentials

  • Last Update :
  • Techknowledgy :

If I simply open a new terminal and run my python script (I do not set any reference to my credentials file). The data is successfully added to the database, but I get the following warning:

UserWarning: Your application has authenticated using end user credentials from Google Cloud
SDK.We recommend that most server applications use service accounts instead.If your
application continues to use end user credentials from Cloud SDK, you might receive a "quota 
exceeded " or "
API not enabled " error. For more information about service accounts, see 
https: //cloud.google.com/docs/authentication/

If I state the the credentials file directly in the python file using a line similar to this db = firestore.Client(project="myproj-99999",credentials="folder/file.json") I get this error:

Traceback (most recent call last):
File "sendToDB.py", line 23, in <module>
   update_create_if_missing(args.uid, args.words)
   File "sendToDB.py", line 9, in update_create_if_missing
   db = firestore.Client(project="earningstotext-251320",credentials="keys/gcpcmdlineuser.json")
   File "/home/vagrant/.local/lib/python3.6/site-packages/google/cloud/firestore_v1/client.py", line 105, in __init__
   project=project, credentials=credentials, _http=None
   File "/home/vagrant/.local/lib/python3.6/site-packages/google/cloud/client.py", line 227, in __init__
   Client.__init__(self, credentials=credentials, _http=_http)
   File "/home/vagrant/.local/lib/python3.6/site-packages/google/cloud/client.py", line 130, in __init__
   raise ValueError(_GOOGLE_AUTH_CREDENTIALS_HELP)
   ValueError: This library only supports credentials from google-auth-library-python.
   See https://google-cloud-python.readthedocs.io/en/latest/core/auth.html
   for help on authentication with this library.

Suggestion : 2

Google Cloud Firestore API client library,Read the Client Library Documentation for Cloud Firestore API to see other available methods on the client.,If you are using an end-of-life version of Python, we recommend that you update as soon as possible to an actively supported version.,Install this library in a virtualenv using pip. virtualenv is a tool to create isolated Python environments. The basic problem it addresses is one of dependencies and versions, and indirectly permissions.

Mac/Linux

pip install virtualenv
virtualenv <your-env>
   source <your-env>/bin/activate
      <your-env>/bin/pip install google-cloud-firestore

Windows

pip install virtualenv
virtualenv <your-env>
   <your-env>\Scripts\activate
      <your-env>\Scripts\pip.exe install google-cloud-firestore

Suggestion : 3

For a list of all google-cloud-firestore releases:,Google Cloud Client Libraries for google-cloud-firestore,Enable the Cloud Firestore API.,Read the Cloud Firestore API Product documentation to learn more about the product and see How-to Guides.

pip install virtualenv
virtualenv <your-env>
   source <your-env>/bin/activate
      <your-env>/bin/pip install google-cloud-firestore
pip install virtualenv
virtualenv <your-env>
   <your-env>\Scripts\activate
      <your-env>\Scripts\pip.exe install google-cloud-firestore

Suggestion : 4

Python Firebase Cloud Firestore: the Full Course. Python Firebase tutorials for complete beginners. Use the Firebase Admin SDK to work with Firebase Cloud Firestore and Python. With Firestore, Learn how to perform CRUD operations: Create, Read, Update, Delete.,According to Firebase Documentation, Cloud Firestore is a flexible, scalable database for mobile, web, and server development from Firebase and Google Cloud Platform. Like Firebase Realtime Database, it keeps your data in sync across client apps through realtime listeners and offers offline support for mobile and web so you can build responsive apps that work regardless of network latency or Internet connectivity. Cloud Firestore also offers seamless integration with other Firebase and Google Cloud Platform products, including Cloud Functions.,Python has a steadily developing community that offers enormous help. From amateurs to specialists, there's everybody. There are a lot of instructional exercises, documentation, and guides accessible for Python web development solutions. ,In simple terms, moving from local to the public cloud server is called cloud migration. Gartner says 17.5% revenue growth as promised in cloud migration and also has a forecast for 2022 as shown in the following image.

OK so after creation of your project in Firebase Console, you need to enable Firebase Cloud Firestore in your project. after that for integration of Firebase SDK you need to install the SDK for the language of your choice. because we are using python, so we do the process for python programming language. you can easily install Firebase Admin SDK with pip like this.

pip install firebase - admin

This is the complete code for Python Firebase Cloud Firestore Example

import firebase_admin
from firebase_admin
import credentials
from firebase_admin
import firestore

# initializations
cred = credentials.Certificate('firebase-sdk.json')
firebase_admin.initialize_app(cred)
db = firestore.client()

#adding first data
doc_ref = db.collection('employee').document('empdoc')

doc_ref.set({

   'name': 'Parwiz',
   'lname': 'Forogh',
   'age': 24

})

#adding second data
doc_ref = db.collection('employee').document('emptwodoc')
doc_ref.set({

   'name': 'John',
   'lname': 'Doe',
   'email': 'john@gmail.com',
   'age': 24

})

#Reading the data
   ''
'

emp_ref = db.collection('employee')
docs = emp_ref.stream()

for doc in docs:
   print('{} => {} '.format(doc.id, doc.to_dict()))

''
'

OK now these are the imports that we need to use for this article , basically we have imported firebase_admin, credentials and firestore.

import firebase_admin
from firebase_admin
import credentials
from firebase_admin
import firestore

Now we need to create our firestore in here.

cred = credentials.Certificate('firebase-sdk.json')

firebase_admin.initialize_app(cred)

Create a new collection and a document using the following example code.

doc_ref = db.collection('employee').document('empdoc')

doc_ref.set({

   'name': 'Parwiz',
   'lname': 'Forogh',
   'age': 24

})