谷歌驱动器文件权限更新

时间:2019-01-29 11:41:04

标签: permissions google-drive-api

我的Google驱动器中有一个文件,如何在python中更新该文件的权限。 由于我是编程的新手,请提出一些可能的解决方案。

import json
import requests

FILE_ID = '###############'
access_token = '##############'
url = 'https://www.googleapis.com/drive/v2/files/'+ FILE_ID +'/permissions'

meta_data = {
"type": "user",
"emailAddress": 'user@gmail.com',
"role": "writer"
}

headers = {'Authorization': 'Bearer ' + access_token} 
response = requests.post(url, headers=headers, data=json.dumps(meta_data))

这是我尝试过的方法,但出现“需要许可类型”错误。

1 个答案:

答案 0 :(得分:0)

我建议关注Google quickstart,然后您应该可以使用以下代码。

从apiclient导入错误     #...

def update_permission(service, file_id, permission_id, new_role):
  """Update a permission's role.

  Args:
    service: Drive API service instance.
    file_id: ID of the file to update permission for.
    permission_id: ID of the permission to update.
    new_role: The value 'owner', 'writer' or 'reader'.

  Returns:
    The updated permission if successful, None otherwise.
  """
  try:
    # First retrieve the permission from the API.
    permission = service.permissions().get(
        fileId=file_id, permissionId=permission_id).execute()
    permission['role'] = new_role
    return service.permissions().update(
        fileId=file_id, permissionId=permission_id, body=permission).execute()
  except errors.HttpError, error:
    print 'An error occurred: %s' % error
  return None