Balazs Nemeth
Balazs Nemeth

Reputation: 2332

Firebase Admin SDK - Python

Recently the FB Admin SDK was introduced for Python as well, and here is a repo with some samples.

It's nice I can authenticate using credentials, and finally I have a firebase_admin authenticated which can create Custom Tokens too. But how can it help to do requests for the REST API e.g? Can I retrieve my authentication token and set it as Authorization header maybe to do API requests?

Upvotes: 0

Views: 3713

Answers (1)

Hiranya Jayathilaka
Hiranya Jayathilaka

Reputation: 7438

You should be able to get an OAuth token by calling the get_access_token() method on your credential, and then pass it to the REST API as described here.

However, in the v1.0.0 of the Python Admin SDK, the returned credential does not contain the Firebase scopes. Therefore the OAuth token obtained from the credential will not readily work with the REST API. This is a bug, and it will be addressed in a future release. In the meantime you can use the following trick:

from firebase_admin import credentials

scopes = [
    'https://www.googleapis.com/auth/firebase.database',
    'https://www.googleapis.com/auth/userinfo.email'
]

cred = credentials.Certificate('path/to/serviceKey.json')
token = cred.get_credential().create_scoped(scopes).get_access_token().access_token
# Pass token to REST API

In a future release, once the bug has been fixed, you will be to do the following:

from firebase_admin import credentials

cred = credentials.Certificate('path/to/serviceKey.json')
token = cred.get_access_token().access_token
# Pass token to REST API

Upvotes: 7

Related Questions