在google drive中更改文件权限的post请求失败

2024-05-16 23:15:52 发布

您现在位置:Python中文网/ 问答频道 /正文

Im使用python请求库使google一个驱动器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

当我运行这个程序时,我得到以下响应:

^{pr2}$

我一直在使用googledocsapi来解决这个问题,但一直没能弄清楚我在做什么。 https://developers.google.com/drive/v2/reference/permissions/insert

甚至他们也试试!节已断开,因为没有添加所需“值”字段的选项。在

我做错什么了?还有其他人遇到这些问题吗?在

谢谢


Tags: httpscomurlpermissionsgoogledrivev2headers
1条回答
网友
1楼 · 发布于 2024-05-16 23:15:52

我用的是urllib.请求模块,工作正常。这是我的代码:

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.请求通过Requests module(使用它更干净)在我的小库中,但是,现在起作用了。在

因为我使用python3,所以不能使用googleapi Python客户端。在

相关问题 更多 >