python - Firebase DB HTTP API Auth: When and how to refresh JWT token? -
i'm trying make python webapp write firebase db using http api (i'm using new version of firebase presented @ google i/o 2016).
my understanding far specific type of write i'd accomplish made post request url of type:
https://my-project-id.firebaseio.com/{path-to-resource}.json
what i'm missing auth part: if got correctly jwt should passed in http authorization header authorization : bearer {token}
.
so created service account, downloaded private key , used generate jwt, added request headers , request wrote firebase db.
now jwt has expired , similar request firebase db failing.
of course should generate new token question is: wasn't expecting handle token generation , refresh myself, http apis i'm used require static api key passed in request webapps kept relatively simple adding stati api key string request.
if have take care of token generation , expiration webapp logic needs become more complex (because i'd have store token, check if still valid , generate new 1 when not), or generate new token every request (but make sense?).
i'd know if there's best practice follow in respect or if i'm missing documentation regarding topic.
thanks, marco
addendum
this code i'm running:
import requests import json oauth2client.service_account import serviceaccountcredentials _base_url = 'https://my-app-id.firebaseio.com' _scopes = [ 'https://www.googleapis.com/auth/userinfo.email', 'https://www.googleapis.com/auth/firebase.database' ] def _get_credentials(): credentials = serviceaccountcredentials.from_json_keyfile_name('my_service_account_key.json', scopes=_scopes) return credentials.get_access_token().access_token def post_object(): url = _base_url + '/path/to/write/to.json' headers = { 'authorization': 'bearer '+ _get_credentials(), 'content-type': 'application/json' } payload = { 'title': title, 'message': alert } return requests.post(url, data=json.dumps(payload), headers=headers)
currently every request new jwt generated. doesn't seem optimal me. possible generate token doesn't expire?
thanks code example. got working better using credentials.authorize function creates authenticated wrapper http.
from oauth2client.service_account import serviceaccountcredentials httplib2 import http import json _base_url = 'https://my-app-id.firebaseio.com' _scopes = [ 'https://www.googleapis.com/auth/userinfo.email', 'https://www.googleapis.com/auth/firebase.database' ] # credentials make authorized call firebase credentials = serviceaccountcredentials.from_json_keyfile_name( _key_file_path, scopes=_scopes) # wrap http in credentials. subsequent calls authenticated http_auth = credentials.authorize(http()) def post_object(path, objecttosave): url = _base_url + path resp, content = http_auth.request( uri=url, method='post', headers={'content-type': 'application/json'}, body=json.dumps(objecttosave), ) return content objecttopost = { 'title': "title", 'message': "alert" } print post_object('/path/to/write/to.json', objecttopost)
Comments
Post a Comment