简体   繁体   English

使用 python 将.json 文件上传到 firebase 数据库

[英]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.我想将整个 JSON 文件上传到我的 Firebase 数据库,但我无法让它运行。 I am new to Python and Firebase.我是 Python 和 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]>我收到一个错误: <响应 [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. 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:如果您想使用 REST API 编写,这是可能的,但您需要:

  1. Ensure your REST call is authenticated, as shown in the documentation on authenticating REST requests .确保您的 REST 调用已通过身份验证,如有关身份验证 REST 请求的文档中所示。

  2. Use a URL that ends with .json to ensure your call end up to the REST API.使用以 .json 结尾的.json以确保您的通话结束至 REST ZDB97427114CA3ACE6D40。 So:所以:

     requests.put(url="https://myapp.firebaseio.com/.json", json= jsondata)

    Yes, that /.json at the end of the URL is normal.是的, /.json末尾那个/.json是正常的。


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 . 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管理员 SDK 的数据

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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