无法通过POST请求更改Google Drive中文件的权限
我正在使用Python的requests库来向Google Drive的API发送请求,目的是更改一个文件的权限,这里指的是文件的拥有者。
我的代码大概是这样的:
fileId = "123abcEfJl-mNooP45Kl6u" #fake file id
url = https://www.googleapis.com/drive/v2/files/%s/permissions' % fileId
payload = {"role":"owner", "type":"user", "value":"<some_user>@gmail.com"}
headers = {'Authorization': 'Bearer %s'%access_token, 'Content-Type':'application/json'}
permResponse = requests.post(url, data=payload, headers=headers)
print permResponse.text
当我运行这段代码时,得到了以下的响应:
{
"error": {
"errors": [
{
"domain": "global",
"reason": "parseError",
"message": "Parse Error"
}
],
"code": 400,
"message": "Parse Error"
}
}
我一直在参考Google文档的API,但还是没搞明白我哪里出错了。
https://developers.google.com/drive/v2/reference/permissions/insert甚至他们的“试试这个!”部分也有问题,因为没有选项可以添加所需的“值”字段。
我到底哪里做错了?有没有其他人遇到类似的问题?
谢谢!
1 个回答
0
我在用urllib.request这个模块,它运行得很好。我的代码是:
key = "?key=" + MY_API_KEY
url_destino = ("https://www.googleapis.com/drive/v2/files/%s/permissions" % source_id)+ key
values = "{"role":"owner", "type":"user", "value":"<some_user>@gmail.com"}"
data = values.encode('utf-8')
request = urllib.request.Request(url_destino, data, method='POST')
request.add_header("Authorization", "Bearer " + token)
request.add_header("Content-Length", len(data))
request.add_header("Content-Type", "application/json")
print(request.header_items()) # for debugging purpouse
f = urllib.request.urlopen(request)
print(f.read())
我想把urllib.request换成Requests模块(这个用起来更简单)在我的小库里,但现在这个也能用。
因为我用的是Python 3,所以我不能使用google-api-python-client。