在受限的情况下,我无法安装Python软件包-仅限于标准Python 2.7软件包。因此,我无法使用Firestore SDK。 documentation for updating documents使用此SDK。
我目前使用urllib2
授权请求发布到Firestore。
我想更新一个现有文档,而不是删除它并创建一个新文档。
在Firestore中发布和删除的示例:
# config object created above #
# ...
#
authdata = {"email":config.auth_email,"password":config.auth_pass,"returnSecureToken":True}
req = urllib2.Request(config.auth_url, json.dumps(authdata), {'Content-Type': 'application/json'})
f = urllib2.urlopen(req, cafile="/root/cacerts.pem")
response = f.read()
response = response.split()
idToken = response[response.index('"idToken":') + 1].replace("\"", '').strip(",")
## POST DATA ##
headers = {
'Content-type': 'application/json; charset=utf-8', 'Authorization':'Bearer %s' % idToken,
}
req = urllib2.Request(config.post_url, json.dumps(data), headers=headers)
f = urllib2.urlopen(req,cafile="/root/cacerts.pem")
response = f.read()
## DELETE DATA ##
headers = {
'Authorization':'Bearer %s' % idToken
}
req = urllib2.Request(config.delete_url, None, headers=headers)
req.get_method = lambda: 'DELETE'
f = urllib2.urlopen(req,cafile="/root/cacerts.pem")
response = f.read()