驱动器 API - 插入文件时出现解析错误
Drive API - Parse Error when inserting file
我需要在带有 requests
python 库的 Google 驱动器中创建一个新文件。
r = requests.post(
'https://www.googleapis.com/drive/v2/files',
headers={
'Content-type': 'application/json; charset=UTF-8',
'Authorization': 'Bearer %s' % access_token
}
)
这段代码可以成功创建一个所有值都设置为默认值的文件,但是如果我尝试,例如设置文件的标题:
r = requests.post(
'https://www.googleapis.com/drive/v2/files',
data={"title": "Foo"},
headers={
'Content-type': 'application/json; charset=UTF-8',
'Authorization': 'Bearer %s' % self.access_token
}
)
然后服务器响应 400 Bad Request
和 json 响应:
{
"error": {
"code": 400,
"message": "Parse Error",
"errors": [
{
"domain": "global",
"message": "Parse Error",
"reason": "parseError"
}
]
}
}
我做错了什么以及如何解决?
事实证明,当发送 json 请求时,数据参数应该是一个序列化的 json 对象。
r = requests.post(
'https://www.googleapis.com/drive/v2/files',
data=json.dumps({"title": "Foo"}),
headers={
'Content-type': 'application/json; charset=UTF-8',
'Authorization': 'Bearer %s' % self.access_token
}
)
我需要在带有 requests
python 库的 Google 驱动器中创建一个新文件。
r = requests.post(
'https://www.googleapis.com/drive/v2/files',
headers={
'Content-type': 'application/json; charset=UTF-8',
'Authorization': 'Bearer %s' % access_token
}
)
这段代码可以成功创建一个所有值都设置为默认值的文件,但是如果我尝试,例如设置文件的标题:
r = requests.post(
'https://www.googleapis.com/drive/v2/files',
data={"title": "Foo"},
headers={
'Content-type': 'application/json; charset=UTF-8',
'Authorization': 'Bearer %s' % self.access_token
}
)
然后服务器响应 400 Bad Request
和 json 响应:
{
"error": {
"code": 400,
"message": "Parse Error",
"errors": [
{
"domain": "global",
"message": "Parse Error",
"reason": "parseError"
}
]
}
}
我做错了什么以及如何解决?
事实证明,当发送 json 请求时,数据参数应该是一个序列化的 json 对象。
r = requests.post(
'https://www.googleapis.com/drive/v2/files',
data=json.dumps({"title": "Foo"}),
headers={
'Content-type': 'application/json; charset=UTF-8',
'Authorization': 'Bearer %s' % self.access_token
}
)