Skip to content

Instantly share code, notes, and snippets.

@billydh
Last active April 15, 2024 10:49
Show Gist options
  • Save billydh/734e5cc4d78d46e497b4f0589bc34ac0 to your computer and use it in GitHub Desktop.
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
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)
@ArthurDeveloper
Copy link

Helped me a lot, thanks!

@wishrohitv
Copy link

thanks , very helpful

@daniel-maturana
Copy link

Really helpfull, Thanks

@mjkweon17
Copy link

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