Skip to content
Advertisement

How to initialize google-cloud-firestore – Python

from google.cloud import firestore

# Add a new document
db = firestore.Client()  # need to put credentials
doc_ref = db.collection(u'users').document(u'alovelace')
doc_ref.set({
    u'first': u'Ada',
    u'last': u'Lovelace',
    u'born': 1815
})

# Then query for documents
users_ref = db.collection(u'users')

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

This is a example code from https://googleapis.dev/python/firestore/latest/ When I run that it shows

Traceback (most recent call last):
  File "/home/kulothungan/Purp/temp/temp.py", line 4, in <module>
    db = firestore.Client()
  File "/home/kulothungan/.local/lib/python3.6/site-packages/google/cloud/firestore_v1/client.py", line 94, in __init__
    client_options=client_options,
  File "/home/kulothungan/.local/lib/python3.6/site-packages/google/cloud/firestore_v1/base_client.py", line 128, in __init__
    _http=None,
  File "/home/kulothungan/.local/lib/python3.6/site-packages/google/cloud/client.py", line 277, in __init__
    _ClientProjectMixin.__init__(self, project=project, credentials=credentials)
  File "/home/kulothungan/.local/lib/python3.6/site-packages/google/cloud/client.py", line 225, in __init__
    project = self._determine_default(project)
  File "/home/kulothungan/.local/lib/python3.6/site-packages/google/cloud/client.py", line 244, in _determine_default
    return _determine_default_project(project)
  File "/home/kulothungan/.local/lib/python3.6/site-packages/google/cloud/_helpers.py", line 186, in _determine_default_project
    _, project = google.auth.default()
  File "/home/kulothungan/.local/lib/python3.6/site-packages/google/auth/_default.py", line 483, in default
    raise exceptions.DefaultCredentialsError(_HELP_MESSAGE)
google.auth.exceptions.DefaultCredentialsError: Could not automatically determine credentials. Please set GOOGLE_APPLICATION_CREDENTIALS or explicitly create credentials and re-run the application. For more information, please see https://cloud.google.com/docs/authentication/getting-started

but I don’t know how to initialize that and also I don’t want to do that by using environmental variables If anyone knows pls inform me Thanks in advance

Advertisement

Answer

If you have access to this particular project, you need to go to the Google cloud console Service Accounts page here and create a service account for accessing firestore. Service account creation. Give the service account one of the datastore roles as appopriate.

Once created, you will have to generate keys for the service account. You can do this from the “Keys” tab on the service account’s page.

Service Account Key Creation

Select the JSON format. You’ll get a file called keys.json. Store that on your computer and create the environment variable GOOGLE_APPLICATION_CREDENTIALS

export GOOGLE_APPLICATION_CREDENTIALS=/path/to/keys.json

Now try running your python script.

Without using Environment Variables

You need to install the google-auth python package which provides the google.oauth2.service_account module in which there is the Credentials class. You can initialize this class with your credentials like this:

from google.oauth2 import service_account

credentials = service_account.Credentials.from_service_account_file(
    '/path/to/keys.json')

Then you can pass this credentials object to the firestore Client() like this:

db = firestore.Client(credentials=credentials)
Advertisement