Skip to content
Advertisement

How to initialize google-cloud-firestore – Python

JavaScript

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

JavaScript

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

JavaScript

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:

JavaScript

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

JavaScript
Advertisement