Firebase Admin Python SDK v2.0.0 was released on May 17, 2017, and some calling methods were different, so I added it as a supplement.
On April 4, 2017 (around 4/5 early morning in Japan time), the Firebase SDK for Python was released by Google officially. The official name is "Firebase Admin Python SDK", and as the name suggests, it is an SDK that supports APIs around Admin.
As the name suggests (second time), processing around Admin is provided. However, when I read the reference and guide, it seems that the function currently provided is "only the part that creates a Custom Token or Access Token from the private key of the service account".
Add the Firebase Admin SDK to your Server | Firebase
I will trace the contents written above as it is, but be careful as it will be clogged if it is ** as it is **.
First of all, get the private key. Open the project from the Firebase console site (https://console.firebase.com/), go to Gear> Project Settings> Service Account next to Overview in the menu, and click "New Private Key" at the bottom of the screen. Press the "Generate" button to get the json file.
Then install the required libraries with pip.
$ pip install firebase-admin
The code looks like this.
v1.0.0
getToken.py
import firebase_admin
from firebase_admin import credentials
def getToken:
credential = credentials.Certificate('./serviceAccountKey.json')
scopes = [
'https://www.googleapis.com/auth/firebase.database',
'https://www.googleapis.com/auth/userinfo.email'
]
# access_Get token
accessTokenInfo = credential.get_credential().create_scoped(scopes).get_access_token()
print("access_token:" + accessTokenInfo.access_token)
print("expire:" + str(accessTokenInfo.expires_in))
if __name__ == '__main__':
getToken()
The difference from the official document is that you can set the scope to credential without using firebase_admin.initialize_app ()
and get the AccessToken as it is.
v2.0.0
getToken.py
import firebase_admin
from firebase_admin import credentials
def getToken:
credential = credentials.Certificate('./serviceAccountKey.json')
# access_Get token
accessTokenInfo = credential.get_access_token()
print("access_token:" + accessTokenInfo.access_token)
print("expire:" + str(accessTokenInfo.expiry)) # 「20XX-XX-XX XX:XX:XX.XXXXXX "is returned
if __name__ == '__main__':
getToken()
The difference from v1.0.0 is that you no longer need to get the credential information once with get_credential ()
or set the scope.
Also, while the content of the expiration date was the number of seconds remaining in v1.0.0, from v2.0.0 the property name is ʻexpiry`, and the available expiration date is returned as a datetime object. I will.
When using with RealtimeDatabase, you can access it by putting it in the parameter ʻaccess_token`. Since this AccessToken is created by a service account, it is possible to read and write even if all users are false in the database rules, so be careful when handling it. ** By the way, the token is valid for 1 hour after issuance.
https://{FIREBASE_PROJECT_ID}.firebaseio.com/hoge.json?access_token={FIREBASE_ACCESS_TOKEN}
I think that you can get a CustomToken in the same way, but when you get an AccessToken correctly, it is no longer necessary for you, so it remains unverified.
Recommended Posts