errorsea

  • PHP
    • Latest Version 7.0
    • Operators
    • String Functions
    • Array Functions
    • Date/Time Functions
    • Session
    • MySQLi Functions
    • $_FILES
    • Upload File in PHP
    • Force Download in PHP
  • MySQL
    • Queries and Operators
    • Primary Key and Foreign Key
    • Add/Drop Unique Key
  • JavaScript
    • Operators
    • String Methods
    • Array Methods
    • Create Element
    • Remove Element
  • jQuery
    • Create Element
    • Remove Element
  • How To
    • Download Turbo C++
    • HTACCESS Redirect
    • How to Replace All Words in a String Using JavaScript
    • Simple Registration and Login form with PHP and MySQL
    • PHP Multiple File Uploader Using Ajax
    • How to Send Emails From PHPMailer Using SMTP
    • How to Add Drop Unique Key in MySQL
    • Scroll to Top of the Page Using jQuery
  • Interview Questions
    • Top 25 HR Interview Questions
    • Top 25 Hibernate Interview Questions
    • Top 25 AWS Interview Questions
    • Top 25 C++ Interview Questions
    • Top 25 PHP OOPS Interview Questions
    • Top 25 MySQL Interview Questions
    • Top 25 SQL Queries Interview Questions
    • Top 25 HTML Interview Questions
    • TOP 25 CSS Interview Questions
    • Top 25 Web Designing Interview Questions
Home » How to Generate Authorization(oauth2)/Bearer Token for Firebase V1 API in Python

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

November 19, 2019 by Nachiket Panchal

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 are going to take a look at a few steps in python to generate an oauth2 token which will be helpful to use Firebase V1 API.

Index

  • Generate Firebase V1 API Token
    • Step 1: Install oauth2client and requests library
    • Step 2: Create a service-account.json File
    • Step 3: Code to Generate oauth2/Bearer/Authentication Token

Generate Firebase V1 API Token

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

As this post is all about Firebase Token Generation in Python, so I hope you all are 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 also requests to make a request in python.

You can install them in python with the below code.

1
2
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.

1
2
3
4
5
6
7
8
9
10
11
12
{
"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 simply 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 service-account.json file

  1. Open https://console.cloud.google.com/project/_/iam-admin
  2. Select project and click continue
    Step 1 select firebase project
  3. From left navigation select Service Accounts
    step 2 go to service accounts
  4. Select an email account and click on the Action button  at right in that row, and click on Create key.
    step 3 create service account 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 below function in your python project and generate a token by calling function.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
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.

I hope you found this post informative.

Enjoy Programming 🙂

Filed Under: How To, Python

About Nachiket Panchal

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

Categories

  • Ajax
  • Bootstrap
  • CSS
  • How To
  • Interview Questions
  • JAVA
  • JavaScript
  • jQuery
  • MySQL
  • PHP
  • Python

Latest posts

  • How to Install MongoDB on windows[7,8, 8.1,10] December 1, 2019
  • How to Generate Authorization(oauth2)/Bearer Token for Firebase V1 API in Python November 19, 2019
  • How to Apply Ripple Effect to On Button With CSS November 14, 2019
  • List of PHP Date/Time Functions With Examples November 8, 2019
  • Things You Should Know About PHP Latest Version 7.0 November 7, 2019

with & in India.
© 2018-2019 errorsea
Privacy Policy | Contact Us | Disclaimer