4

This is surprisingly excruciating. There's no documentation whatsoever for the new Firebase + Python. So I'm trying to use the REST API, which needs a special kind of authentication.

enter image description here As you can see, they say it could be the app's secret (nowhere to be found in the new console) or the authentication token (which is not documented for python). If I click the REST authentication documentation I get a Java example.

I've downloaded the JSON Key file for the service account, and tried to apply pieces of code found in other places, unfortunately in vain.

Can anyone provide a hint on how to proceed with this?

MeLight
  • 5,454
  • 4
  • 43
  • 67

1 Answers1

0

You need to generate an access token from the service account JSON key file using the Google API Python client library, and then add it to the headers of your request.

Here's an example of how to do it with requests

from oauth2client.service_account import ServiceAccountCredentials
import requests
import json
import httplib2

scopes = [
    'https://www.googleapis.com/auth/firebase',
    'https://www.googleapis.com/auth/userinfo.email',
    "https://www.googleapis.com/auth/cloud-platform"
]

credentials = ServiceAccountCredentials.from_json_keyfile_name(
    '/pathto/json_file.json', scopes)

headers = {"content-type": "application/json; charset=UTF-8"}
headers['Authorization'] = 'Bearer ' + credentials.get_access_token().access_token

firebase_url = "https://docs-examples.firebaseio.com/rest/saving-data/fireblog/users/alanisawesome/name.json"

data = {
    "name": "Alan Turing",
    "birthday": "June 23, 1912"}

requests.put(firebase_url, headers=headers, data=json.dumps(data).encode("utf-8"))

You can install the google python api client library using pip:

pip install --upgrade google-api-python-client
atimothee
  • 658
  • 4
  • 11
  • I get error 403 Permission Denied when I try to access firebase database. According to the docs it should work. My question is here http://stackoverflow.com/questions/40810233 . I appreciate your help – bibscy Nov 26 '16 at 18:57