Last active
April 15, 2024 10:49
-
-
Save billydh/734e5cc4d78d46e497b4f0589bc34ac0 to your computer and use it in GitHub Desktop.
A Python script to sign user in with email and password via Firebase Auth REST API
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import argparse | |
import json | |
import os | |
import requests | |
import pprint | |
FIREBASE_WEB_API_KEY = os.environ.get("FIREBASE_WEB_API_KEY") | |
rest_api_url = f"https://identitytoolkit.googleapis.com/v1/accounts:signInWithPassword" | |
def get_args(): | |
parser = argparse.ArgumentParser(description="Sign in a Firebase user with email and password") | |
parser.add_argument("--email", required=True, help="The email address which the user wants to sign in with.") | |
parser.add_argument("--password", required=True, help="The password of the user.") | |
return parser.parse_args() | |
def sign_in_with_email_and_password(email: str, password: str, return_secure_token: bool = True): | |
payload = json.dumps({ | |
"email": email, | |
"password": password, | |
"returnSecureToken": return_secure_token | |
}) | |
r = requests.post(rest_api_url, | |
params={"key": FIREBASE_WEB_API_KEY}, | |
data=payload) | |
return r.json() | |
if __name__ == "__main__": | |
args = get_args() | |
token = sign_in_with_email_and_password(args.email, args.password) | |
pprint.pprint(token) |
Thank you for awesome code! This is really helpful.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Really helpfull, Thanks