User Management With Firebase and Python
Tags: Data Science, Firebase, Python, Programming, Software Engineering

Set Up a New Firebase Project and Credentials

First things first, we need to set up our Firebase credentials so we’re able to use their APIs. Their documentation is detailed and easy enough to follow. You can find it here.

Anyway, let’s still go through the setup process so everybody is on the same page. We’ll only discuss the necessary steps here, so if you’d like to know more details, please visit the page I linked above.

Python version and pip installation

Firstly, your Python version needs to be 3.5 or above. Ensure you have it installed.

Next, we can install the Firebase Admin SDK using pip. Here’s the command you can run from your terminal to do that.

$ pip3 install firebase-admin

Just a reminder, it’s always better to install a package like this in our virtual environment to keep it contained in just the project that needs it.

Generate Google Application Default Credentials

Before we can use the SDK, we need to generate our Google Application Default Credentials.

Firstly, go to your Firebase console, and click on “Add project.”

Firebase console

Let’s give our project a name: python-admin-sdk-demo .

Name the Firebase project

The next step is to enable Google Analytics. For the purpose of this tutorial, we don’t need this to be turned on, so let’s just turn it off. After you disable it, click on “Create project.”

Google Analytics — turned off

Firebase will then set up and generate the new project for you. Once it’s finished, click on “Continue” to go to your Project Overview page.

Project Overview

On the top left-hand side, click on the cog icon next to “Project Overview,” and select “Project settings.”

Project settings

Then, navigate to the Service accounts tab. Click on Python for the Admin SDK configuration snippet.

Service accounts

Go ahead and click on “Generate new private key.” A window like below will pop up. As it says in the warning on the window, remember to keep the private key secure. Do not commit this to your project repository.

All right, we can continue and click on “Generate key.”

Generate a new private key

Firebase will open a prompt and ask you where you want to save the JSON file. Let’s create a new folder called python-admin-sdk-demo and store the file in there.

Great! We have what we need to be able to call the Firebase Admin APIs now. Let’s see how we can set it up in Python.

Here’s the instruction from Firebase:

“When authorizing via a service account, you have two choices for providing the credentials to your application. You can either set the GOOGLE_APPLICATION_CREDENTIALS environment variable, or you can explicitly pass the path to the service account key in code. The first option is more secure and is strongly recommended.”

So let’s follow their recommendation to set an environment variable named GOOGLE_APPLICATION_CREDENTIALS and give a value of the path to the JSON file we just downloaded from Firebase.

From your terminal, let’s execute the following command to set the environment variable.

~/demo/python-admin-sdk-demo ❯ export GOOGLE_APPLICATION_CREDENTIALS=/Users/billyde/demo/python-admin-sdk-demo/python-admin-sdk-demo-1a64n-firebase-adminsdk-ze72a-5a36713w1q.json ~/demo/python-admin-sdk-demo ❯ echo $GOOGLE_APPLICATION_CREDENTIALS

/Users/billyde/demo/python-admin-sdk-demo/python-admin-sdk-demo-1a64n-firebase-adminsdk-ze72a-5a36713w1q.json

Cool. Let’s write the Python code now.

Create a new file in your project directory and call it initialise_firebase_admin.py . This is what the content will be:

initialise_firebase_admin.py

Now, if we go back to the terminal and execute the following command, we’ll be able to see that the Firebase Admin App was successfully initialised.

~/demo/python-admin-sdk-demo ❯ python initialise_firebase_admin.py

<firebase_admin.App object at 0x125d19wg3>

Perfect! We’re ready to move on to more exciting stuff now.