Код: Выделить всё
import json
import requests
import firebase_admin
from firebase_admin import credentials, auth
# Load service account
cred = credentials.Certificate('service_account.json')
# Initialize Firebase Admin SDK
firebase_admin.initialize_app(cred)
# Load service account to get the UID
with open('service_account.json', 'r') as f:
service_account = json.load(f)
# Tenant ID for developers
tenant_id = 'DEVELOPERS_TENANT_ID'
# Use a developer user UID
uid = 'SOME_RANDOM_USER_ID'
# Your Firebase Web API Key (replace with your actual API key)
api_key = 'API_KEY'
# Create custom token using Firebase Admin SDK with tenant ID
custom_token = auth.create_custom_token(uid)
# Decode bytes to string if needed
if isinstance(custom_token, bytes):
custom_token = custom_token.decode('utf-8')
print(f"Custom token:")
print(custom_token)
# Make API call to sign in with custom token
url = f"https://identitytoolkit.googleapis.com/v1/accounts:signInWithCustomToken?key={api_key}"
payload = {
"token": custom_token,
"returnSecureToken": True,
"tenantId": tenant_id
}
headers = {
"Content-Type": "application/json"
}
print("\nMaking API call to sign in...")
response = requests.post(url, json=payload, headers=headers)
if response.status_code == 200:
result = response.json()
print("Sign-in successful!")
print(f"ID Token: {result.get('idToken')}")
print(f"Refresh Token: {result.get('refreshToken')}")
print(f"Local ID: {result.get('localId')}")
print(f"Expires In: {result.get('expiresIn')} seconds")
else:
print(f"Sign-in failed with status code: {response.status_code}")
print(f"Error: {response.text}")
Код: Выделить всё
Sign-in failed with status code: 500
Error: {
"error": {
"code": 500,
"message": "Internal error encountered.",
"errors": [
{
"message": "Internal error encountered.",
"domain": "global",
"reason": "backendError"
}
],
"status": "INTERNAL"
}
}
Подробнее здесь: https://stackoverflow.com/questions/797 ... oded-token