How To

How to Generate Authorization(oauth2)/Bearer Token for Firebase V1 API in Python

Firebase is a service-based mobile and web development platform that provides lots of services like messaging, push notifications, cloud storage, authentication, real-time database, and much more. To use these functionalities, firebase API requires an Authorization or Bearer token.

This Authorization token, also called “oauth2 Token,” needs to be generated through backend languages like Python, Java, nodeJS, etc…

Here, we will take a look at a few steps in python to generate an oauth2 token, which will help use Firebase V1 API.

Generate Firebase V1 API Token in Python

Note: You must have a google firebase account first. If you don’t have one, you can sign up here https://firebase.google.com

This post is all about Firebase Token Generation in Python, so I hope you are all aware of the firebase console.

Step 1: Install oauth2client and Requests Library

You need to install oauth2client dependency to use google APIs in python and request to request in python.

You can install them in python with the below code.

pip install requests
pip install oauth2client

Note: You need to use Sudo if you get a user permission issue.

Step 2: Create a service-account.json File

We need to create a service-account.json file, which holds all the information about our firebase project. It looks like the below example.

{
"type": "service-account",
"project_id": "[PROJECT-ID]",
"private_key_id": "[KEY-ID]",
"private_key": "---BEGIN PRIVATE KEY---\n[PRIVATE-KEY]\n---END PRIVATE KEY---\n",
"client_email": "[SERVICE_ACCOUNT_EMAIL]",
"client_id": "[CLIENT_ID]",
"auth_uri": "https://accounts.google.com/o/oauth2/auth",
"token_uri": "https://accounts.google.com/o/oauth2/token",
"auth_provider_x509_cert_url": "https://www.googleapis.com/oauth2/v1/certs",
"client_x509_cert_url": "https://www.googleapis.com/robot/v1/metadata/x509/[SERVICE-ACCOUNT-EMAIL]"
}

We can replace the dummy data with our project credentials. You can also directly download the file here https://console.cloud.google.com/project/_/iam-admin

Follow the below steps to generate the service-account.json file.

  1. Open https://console.cloud.google.com/project/_/iam-admin
  2. Select the project and click continue.
  3. From the left navigation, select Service Accounts

  4. Select an email account, click on the Action button at right in that row, and click on the Create Key.
  5. Select JSON, and it’s done. The service-account.json file will be downloaded automatically.
  6. Move the service-account.json file to your project directory.

Step 3: Code to Generate oauth2/Bearer/Authentication Token

Copy and paste the below function in your python project and generate a token by calling the function.

import argparse
import json
import requests
from oauth2client.service_account import ServiceAccountCredentials

PROJECT_ID = '<--YOUR PROJECT_ID-->'
BASE_URL = 'https://fcm.googleapis.com'
FCM_ENDPOINT = 'v1/projects/' + PROJECT_ID + '/messages:send'
FCM_URL = BASE_URL + '/' + FCM_ENDPOINT
SCOPES = ['https://www.googleapis.com/auth/firebase.messaging']

def access_token():
credentials = ServiceAccountCredentials.from_json_keyfile_name('<-- PATH TO service-account.json FILE', SCOPES)
access_token_info = credentials.get_access_token()
return access_token_info.access_token

print(access_token())

Note: Please don’t forget to replace PROJECT_ID and PATH to the service-account.json file.

Read Also: How to Call API in PHP Using Curl

Conclusion

In this article, we learned the complete process of generating a firebase authentication token for the firebase’s V1 API. I hope you found this post informative.

Enjoy Programming 🙂

Nachiket Panchal

Founder & Administrator of `errorsea` Having interest in Programming & Technology.

Recent Posts

5 Important Things To Know About WordPress Before You Use It

There is a reason big-name companies like CNN use WordPress. WordPress is a popular content…

3 years ago

How to Install MySQL on Your PC in 3 Easy Steps

In this tutorial, I'm going to show you how to install MySQL on your computer.…

5 years ago

Download and Install Turbo C++ for Windows 10 (Full Installation Guide)

Download Turbo C++ for windows 10 in just 7 Mb and run your first C++…

5 years ago

PHP .HTACCESS Redirects

We can redirect any webpage to any other or redirect the whole domain or website…

5 years ago

PHP Redirect Pages

There are lots of methods to redirect pages, like refresh-redirect from META tag, redirect from…

5 years ago

PHP Include & Required

Include files in PHP are used in appending various global or config files. We can…

5 years ago