简体   繁体   English

将整个 JSON 文件上传到 Firebase

[英]Uploading an Entire JSON file to Firebase

I am trying to "push" the entirety of a JSON file to Firebase, however upon executing the script, I am presented with a Response [405] and no values end up being uploaded to Firebase.我正在尝试将整个 JSON 文件“推送”到 Firebase,但是在执行脚本时,我看到了一个响应 [405],并且最终没有任何值上传到 Firebase。

In essence, I am trying to dump all the information inside a JSON file directly to my Firebase test database... here is my current code and JSON file:本质上,我试图将 JSON 文件中的所有信息直接转储到我的 Firebase 测试数据库……这是我当前的代码和 JSON 文件:



from firebase import firebase
import requests
import json
from pprint import pprint

with open ('/var/www/mywebsite.com/html/private/scripts/data.json') as data_file:
    data=json.load(data_file)
    jsondata = json.dumps('/var/www/mywebsite.com/html/private/scripts/data.json')
pprint(data)

firebase=firebase.FirebaseApplication("https://myapp.firebaseio.com")

r2 = requests.put("https://myapp.firebaseio.com",data=jsondata)
print(r2)

JSON file (data.json): JSON 文件 (data.json):

{
    "_type": "News",
    "queryContext": {
        "originalQuery": "politics"
    },
    "readLink": "https://api.cognitive.microsoft.com/api/v7/news/search?q=politics",
    "totalEstimatedMatches": 30800,
    "value": [
        {
            "about": [
                {
                    "name": "Donald Trump",
                    "readLink": "https://api.cognitive.microsoft.com/api/v7/entities/1a466af2-ed23-25bd-794d-1ca925e4681b"
                }
            ],
            "category": "Politics",
            "datePublished": "2019-05-05T22:25:00.0000000Z",
            "description": "President Donald Trump tweeted a fiery rejection of special council Robert Mueller testifying before Congress on Sunday, backtracking previous statements on the matter and going against the ...",
            "image": {
                "thumbnail": {
                    "contentUrl": "https://www.bing.com/th?id=ON.D548DEF199C3CFAFC9E1636ED4EDF797&pid=News",
                    "height": 466,
                    "width": 700
                }
            },
            "name": "President Trump Attacks Mueller Testifying to Congress, in a Reversal of Previous Comments",
            "provider": [
                {
                    "_type": "Organization",
                    "image": {
                        "thumbnail": {
                            "contentUrl": "https://www.bing.com/th?id=AR_53b75d2fe5b1d0f12d7437384fe429c0&pid=news"
                        }
                    },
                    "name": "Time"
                }
            ],
            "url": "http://time.com/5583464/trump-mueller-testify-congress-republicans/"
        },
        {
            "about": [
                {
                    "name": "Donald Trump",
                    "readLink": "https://api.cognitive.microsoft.com/api/v7/entities/1a466af2-ed23-25bd-794d-1ca925e4681b"
                },
                {
                    "name": "China",
                    "readLink": "https://api.cognitive.microsoft.com/api/v7/entities/5fcc3d97-0cf2-94e5-6dad-cd70e387bd69"
                },
                {
                    "name": "Reuters",
                    "readLink": "https://api.cognitive.microsoft.com/api/v7/entities/9f417f89-9a37-e0d0-912e-99dccfb8982d"
                }
            ],
            "category": "Politics",
            "datePublished": "2019-05-05T23:28:00.0000000Z",
            "description": "WASHINGTON (Reuters) - President Donald Trump dramatically increased pressure on China to reach a trade deal by announcing on Sunday he would hike U.S. tariffs on $200 billion worth of Chinese goods this week and target hundreds of billions more soon.",
            "image": {
                "thumbnail": {
                    "contentUrl": "https://www.bing.com/th?id=ON.282E46AFB3D802530215CF503A4968E0&pid=News",
                    "height": 466,
                    "width": 700
                }
            },
            "mentions": [
                {
                    "name": "Donald Trump"
                },
                {
                    "name": "China"
                },
                {
                    "name": "Reuters"
                }
            ],
            "name": "Trump vows new tariff hike on Chinese goods, escalating tension in trade talks",
            "provider": [
                {
                    "_type": "Organization",
                    "image": {
                        "thumbnail": {
                            "contentUrl": "https://www.bing.com/th?id=AR_62e71bb461e92ae25d37bed673fc53b9&pid=news"
                        }
                    },
                    "name": "Reuters"
                }
            ],
            "url": "https://www.reuters.com/article/us-usa-trade-china/trump-vows-new-tariff-hike-on-chinese-goods-escalating-tension-in-trade-talks-idUSKCN1SB0KP?feedType=RSS&feedName=topNews"
        },
        {
            "about": [
                {
                    "name": "Joe Biden",
                    "readLink": "https://api.cognitive.microsoft.com/api/v7/entities/cad484f9-be75-7a78-12dd-16233f823cd7"
                },
                {
                    "name": "Bernie Sanders",
                    "readLink": "https://api.cognitive.microsoft.com/api/v7/entities/d1b180da-aff9-59f7-887e-85784c68fe0c"
                }
            ],
            "category": "Politics",
            "datePublished": "2019-05-05T22:42:00.0000000Z",
            "description": "Former Vice President Joe Biden Joseph (Joe) Robinette Biden Ben & Jerry's co-founder introduces Sanders as a president who 'stands for justice in all its flavors' How the Democrats might avoid ...",
            "image": {
                "thumbnail": {
                    "contentUrl": "https://www.bing.com/th?id=ON.CB91D4F2DB6E19E5A9A0E6F2E9885FA5&pid=News",
                    "height": 360,
                    "width": 640
                }
            },
            "mentions": [
                {
                    "name": "Joe Biden"
                },
                {
                    "name": "Bernie Sanders"
                },
                {
                    "name": "Dark skin"
                }
            ],
            "name": "Sanders faces big problem with Biden and black voters",
            "provider": [
                {
                    "_type": "Organization",
                    "image": {
                        "thumbnail": {
                            "contentUrl": "https://www.bing.com/th?id=AR_fbfe507a57f598418fd179b0dec78111&pid=news"
                        }
                    },
                    "name": "The Hill"
                }
            ],
            "url": "https://thehill.com/homenews/campaign/442146-sanders-faces-big-problem-with-biden-and-black-voters"
        },
        {
            "about": [
                {
                    "name": "Barack Obama",
                    "readLink": "https://api.cognitive.microsoft.com/api/v7/entities/3af18923-3590-e53b-fdcd-4d1796a9a4cb"
                },
                {
                    "name": "Donald Trump",
                    "readLink": "https://api.cognitive.microsoft.com/api/v7/entities/1a466af2-ed23-25bd-794d-1ca925e4681b"
                },
                {
                    "name": "U.S. Immigration and Customs Enforcement",
                    "readLink": "https://api.cognitive.microsoft.com/api/v7/entities/26bea532-e9e5-e53c-b037-ba7ac831cb90"
                }
            ],
            "category": "Politics",
            "datePublished": "2019-05-05T20:56:00.0000000Z",
            "description": "President Trump tapped former Border Patrol chief under former President Barack Obama to run the U.S. Immigration and Customs Enforcement (ICE) agency a month after he withdrew his previous nominee to go \u201cin a tougher direction.\u201d \u201cI am pleased to ...",
            "image": {
                "thumbnail": {
                    "contentUrl": "https://www.bing.com/th?id=ON.9D74B8CE1B8EE359001429857B7FF649&pid=News",
                    "height": 466,
                    "width": 700
                }
            },
            "name": "Trump picks Obama-era Border Patrol head to lead ICE after several recent Fox appearances",
            "provider": [
                {
                    "_type": "Organization",
                    "image": {
                        "thumbnail": {
                            "contentUrl": "https://www.bing.com/th?id=AR_e1eef5681737e5676d7a09990b13411a&pid=news"
                        }
                    },
                    "name": "YAHOO!"
                }
            ],
            "url": "https://www.yahoo.com/entertainment/trump-picks-obamaera-border-patrol-head-to-lead-ice-after-several-recent-fox-appearances-205342554.html"
        },
        {
            "about": [
                {
                    "name": "Joe Biden",
                    "readLink": "https://api.cognitive.microsoft.com/api/v7/entities/cad484f9-be75-7a78-12dd-16233f823cd7"
                },
                {
                    "name": "Donald Trump",
                    "readLink": "https://api.cognitive.microsoft.com/api/v7/entities/1a466af2-ed23-25bd-794d-1ca925e4681b"
                },
                {
                    "name": "Jim Crow laws",
                    "readLink": "https://api.cognitive.microsoft.com/api/v7/entities/f6bd5d26-f89f-9ea0-05af-ad055a804427"
                }
            ],
            "category": "Politics",
            "datePublished": "2019-05-06T00:45:00.0000000Z",
            "description": "COLUMBIA, S.C. (AP) \u2014 Former Vice President Joe Biden charged Saturday that Jim Crow is \u201csneaking back in\u201d as he emphasized voting rights at his first presidential campaign stop in South Carolina, where black voters play a key role in the South\u2019s ...",
            "image": {
                "thumbnail": {
                    "contentUrl": "https://www.bing.com/th?id=ON.23F5987DE69FAFBE74A5F9DD6842D054&pid=News",
                    "height": 466,
                    "width": 700
                }
            },
            "name": "Biden: Trump, Republicans allowing Jim Crow to return",
            "provider": [
                {
                    "_type": "Organization",
                    "image": {
                        "thumbnail": {
                            "contentUrl": "https://www.bing.com/th?id=AR_ea6cb82ab2fec30d4d14c92dad031a5a&pid=news"
                        }
                    },
                    "name": "The Seattle Times"
                }
            ],
            "url": "https://www.seattletimes.com/nation-world/nation/while-others-stump-in-iowa-biden-turns-attention-to-sc/"
        },
        {
            "category": "Politics",
            "datePublished": "2019-05-06T01:04:00.0000000Z",
            "description": "WASHINGTON -- Why aren't Democratic presidential candidates talking more about foreign policy? Are they being shrewd in playing down a divisive concern that is not at the front of voters' minds, or are they missing an opportunity? The right answer is: Both.",
            "image": {
                "thumbnail": {
                    "contentUrl": "https://www.bing.com/th?id=ON.80BFDEBF99FE3F3ECA2A956A9CF3BFD1&pid=News",
                    "height": 467,
                    "width": 700
                }
            },
            "name": "Bringing Foreign Policy Home",
            "provider": [
                {
                    "_type": "Organization",
                    "name": "RealClearPolitics"
                }
            ],
            "url": "https://www.realclearpolitics.com/articles/2019/05/06/bringing_foreign_policy_home_140242.html"
        },
        {
            "about": [
                {
                    "name": "Joe Biden",
                    "readLink": "https://api.cognitive.microsoft.com/api/v7/entities/cad484f9-be75-7a78-12dd-16233f823cd7"
                },
                {
                    "name": "The Young Turks",
                    "readLink": "https://api.cognitive.microsoft.com/api/v7/entities/6453d254-a1e3-148b-a351-d1a21b9227c9"
                },
                {
                    "name": "The Lead with Jake Tapper",
                    "readLink": "https://api.cognitive.microsoft.com/api/v7/entities/8a1206e9-495c-418d-93b9-d1bba50b4b75"
                }
            ],
            "category": "Politics",
            "datePublished": "2019-05-05T23:36:00.0000000Z",
            "description": "Cenk Uygur, John Iadarola, and Maytha Alhassen, hosts of \"The Young Turks,\" discuss a CNN poll conducted by research organization SSRS that showed Joe Biden with a resounding lead against Sen. Bernie Sanders in the Democratic presidential primary.",
            "image": {
                "thumbnail": {
                    "contentUrl": "https://www.bing.com/th?id=ON.CDC923190BF0F206D8541BBF4B1BD870&pid=News",
                    "height": 375,
                    "width": 500
                }
            },
            "name": "TYT's John Iadarola: CNN Poll Showing Biden In The Lead Sampled Statistically Insignificant Number Of Voters Under 50",
            "provider": [
                {
                    "_type": "Organization",
                    "name": "RealClearPolitics"
                }
            ],
            "url": "https://www.realclearpolitics.com/video/2019/05/05/tyts_john_iadarola_cnn_poll_showing_biden_in_the_lead_sample_statistically_insignificant_number_of_voters_under_50.html"
        },
        {
            "about": [
                {
                    "name": "Robert Mueller",
                    "readLink": "https://api.cognitive.microsoft.com/api/v7/entities/63db3609-63fd-6756-fe02-fa71dab53ae4"
                },
                {
                    "name": "Malta",
                    "readLink": "https://api.cognitive.microsoft.com/api/v7/entities/00727e9c-b7f7-2e31-0220-f5b9e956de8d"
                },
                {
                    "name": "Devin Nunes",
                    "readLink": "https://api.cognitive.microsoft.com/api/v7/entities/293daa86-e689-8fdb-be85-caee83c910f9"
                }
            ],
            "category": "Politics",
            "datePublished": "2019-05-05T23:30:00.0000000Z",
            "description": "House Intelligence Committee ranking member Devin Nunes, in a Fox News interview Sunday, blasted \u201ccherry-picked\u201d findings in Special Counsel Robert Mueller\u2019s report about Joseph Mifsud -- the mysterious professor from Malta who helped ignite the ...",
            "image": {
                "thumbnail": {
                    "contentUrl": "https://www.bing.com/th?id=ON.0C0514FB13A987B0C168F5272A569E88&pid=News",
                    "height": 331,
                    "width": 589
                }
            },
            "name": "Nunes: Mueller report 'cherry-picked' information about mysterious Maltese Professor Mifsud",
            "provider": [
                {
                    "_type": "Organization",
                    "image": {
                        "thumbnail": {
                            "contentUrl": "https://www.bing.com/th?id=AR_7dc0f55e947bdbc51189eb1a022e9eb6&pid=news"
                        }
                    },
                    "name": "Fox News"
                }
            ],
            "url": "https://www.foxnews.com/politics/nunes-mueller-report-mifsud-cherry-picked-information"
        },
        {
            "about": [
                {
                    "name": "Jimmy Carter",
                    "readLink": "https://api.cognitive.microsoft.com/api/v7/entities/07919d8e-1871-c8b1-0532-f3c1c0c0d3b2"
                },
                {
                    "name": "Peter Buttigieg",
                    "readLink": "https://api.cognitive.microsoft.com/api/v7/entities/4b0f74fe-6fd5-795d-dd93-58df9a9969ff"
                }
            ],
            "category": "Politics",
            "datePublished": "2019-05-05T23:58:00.0000000Z",
            "description": "Mayor Pete Buttigieg and his husband, Chasten, visited Jimmy Carter's Sunday school class in Plains, Georgia, on Sunday, and then had lunch with the former president. The South Bend mayor said he was \"humbled\" to meet with Carter, who has also hosted other ...",
            "image": {
                "thumbnail": {
                    "contentUrl": "https://www.bing.com/th?id=ON.855CDEC005F48CBB9A73CB73C027E7AE&pid=News",
                    "height": 350,
                    "width": 700
                }
            },
            "mentions": [
                {
                    "name": "Jimmy Carter"
                },
                {
                    "name": "Peter Buttigieg"
                },
                {
                    "name": "Business Insider"
                }
            ],
            "name": "Pete Buttigieg went to Jimmy Carter's Sunday school class and the former president invited him to read from the Bible",
            "provider": [
                {
                    "_type": "Organization",
                    "image": {
                        "thumbnail": {
                            "contentUrl": "https://www.bing.com/th?id=AR_86ac6d5fd1859e509650fb5feef96bf7&pid=news"
                        }
                    },
                    "name": "Business Insider"
                }
            ],
            "url": "https://www.businessinsider.com/pete-buttigieg-husband-chasten-attend-jimmy-carters-sunday-school-2019-5"
        },
        {
            "category": "Politics",
            "datePublished": "2019-05-06T00:22:00.0000000Z",
            "description": "ST.PAUL, Minn. (AP) \u2014 There's a new mix at Minnesota's big conference table for the always-contentious negotiations at the end of the legislative session over the big questions of taxes and spending, which got going late last week. It's the first budget ...",
            "image": {
                "thumbnail": {
                    "contentUrl": "https://www.bing.com/th?id=ON.ABE50EE80E8B9F4884B37663471649A7&pid=News",
                    "height": 517,
                    "width": 700
                }
            },
            "name": "2019 brings a new mix to Minnesota's big conference table",
            "provider": [
                {
                    "_type": "Organization",
                    "image": {
                        "thumbnail": {
                            "contentUrl": "https://www.bing.com/th?id=AR_54d6e8857a180186fb2098ccfbefbf4b&pid=news"
                        }
                    },
                    "name": "sfgate.com"
                }
            ],
            "url": "https://www.sfgate.com/news/article/2019-brings-a-new-mix-to-Minnesota-s-big-13820647.php"
        }
    ]
}

If you study the Firebase document for the REST API carefully, you'll notice that the URLs in this API must end in .json .如果您仔细研究REST APIFirebase 文档,您会注意到此 API 中的 URL 必须以.json结尾。

So try:所以尝试:

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

This works fine:这工作正常:

requests.put(url="https://{app_id}.firebaseio.com/{...}/data.json", json=jsondata)

And check if you need to configure your admin access first:并检查您是否需要先配置您的管理员访问权限:

  1. Download a service key: Your Firebase project -> Settings icon -> Users and permissions -> Service Accounts.下载服务密钥:您的 Firebase 项目 -> 设置图标 -> 用户和权限 -> 服务帐户。

  2. Use a code snippet for your language.为您的语言使用代码片段。

Python: Python:

import firebase_admin
from firebase_admin import credentials

cred = credentials.Certificate("path/to/serviceAccountKey.json")
firebase_admin.initialize_app(cred)

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

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