Skip to content

Instantly share code, notes, and snippets.

@ibrahim0814
Created December 7, 2021 00:00
Show Gist options
  • Save ibrahim0814/08287472054bcedad25a062fce34ac14 to your computer and use it in GitHub Desktop.
Save ibrahim0814/08287472054bcedad25a062fce34ac14 to your computer and use it in GitHub Desktop.
Firebase API Auth (Python)
from google.oauth2 import service_account
from google.auth.transport.requests import AuthorizedSession
# Define the required scopes
scopes = [
"https://www.googleapis.com/auth/userinfo.email",
"https://www.googleapis.com/auth/firebase.database"
]
# Authenticate a credential with the service account
credentials = service_account.Credentials.from_service_account_file(
"path/to/serviceAccountKey.json", scopes=scopes)
# Use the credentials object to authenticate a Requests session.
authed_session = AuthorizedSession(credentials)
response = authed_session.get(
"https://<DATABASE_NAME>.firebaseio.com/users/ada/name.json")
# Or, use the token directly, as described in the "Authenticate with an
# access token" section below. (not recommended)
request = google.auth.transport.requests.Request()
credentials.refresh(request)
access_token = credentials.token
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment