简体   繁体   中英

upload .json file to firebase database using python

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]>

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:

  1. Ensure your REST call is authenticated, as shown in the documentation on authenticating REST requests .

  2. 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 .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM