Reputation: 661
I want to upload a entire JSON file to my Firebase Database, but I can't get it to run. I am new to Python and Firebase.
This is my code so far:
import firebase_admin
from firebase_admin import credentials
from firebase_admin import db
import json
import requests
from pprint import pprint
cred = credentials.Certificate("path/to/serviceAccountKey.json")
firebase_admin.initialize_app(cred)
with open ('/Users/name/Desktop/Test.json') as data_file: data = json.load(data_file)
jsondata = json.dumps('/Users/name/Desktop/Test.json')
requests.put(url="https://myapp.firebaseio.com/", json= jsondata)
I get a error : <Response [400]>
Upvotes: 3
Views: 4949
Reputation: 598765
Your code that performs the actual write operation:
requests.put(url="https://myapp.firebaseio.com/", json= jsondata)
This code does not in any way use the Admin SDK, but instead writes directly to the REST API of the Firebase Realtime Database.
If you want to write using the REST API, it is possible, but you'll need to:
Ensure your REST call is authenticated, as shown in the documentation on authenticating REST requests.
Use a URL that ends with .json
to ensure your call end up to the REST API. So:
requests.put(url="https://myapp.firebaseio.com/.json", json= jsondata)
Yes, that /.json
at the end of the URL is normal.
Alternatively you can continue to use the Admin SDK to write the data, but in that case you'll need to read and parse the JSON into your code, and send it to the database as a JavaScript object, as shown in the documentation on saving data with the Admin SDK.
Upvotes: 3