我不确定您是否可以在文件创建期间设置权限。另外我不确定你是否可以修改userPermission
。但是你可以做的是permissions
用InsertPermission
.
您将在下面找到如何添加共享权限的示例。我认为下面的代码是不言自明的。
from pydrive.auth import GoogleAuth
from pydrive.drive import GoogleDrive
def get_drive_handle():
gauth = GoogleAuth()
gauth.LocalWebserverAuth()
drive = GoogleDrive(gauth)
return drive
def main():
drive = get_drive_handle()
# create an example file
file = drive.CreateFile({
'title': 'example.txt'
,'mimeType': 'text/plain'
})
file.Upload()
# original permissions
file.FetchMetadata(fields='permissions')
print('-' * 10);
print(file['permissions'])
# insert new permission
permission = file.InsertPermission({
'type': 'anyone'
,'value': 'anyone'
,'role': 'reader'
})
# new permission created above
print('-' * 10);
print(permission)
# all permissions
file.FetchMetadata(fields='permissions')
print('-' * 10);
print(file['permissions'])
if __name__ == '__main__':
main()
例子
$ python -V
Python 3.4.3
$ python so.py
----------
[{'photoLink': 'https://lh4.googleusercontent.com/-KhPJ2d6XQY4/AAAAAAAAAAI/AAAAAAAAAAA/JCJdY_mh4PA/s64/photo.jpg', 'role': 'owner', 'domain': 'stackoverflow.com', 'kind': 'drive#permission', 'emailAddress': 'user272735@stackoverflow.com', 'name': 'user272735', 'type': 'user', 'etag': '"_x2BG98d9xdLfiLb2EI-wbdIricX8/9AA4fWf3tZymo0DbdDmcl8WsxSY"', 'id': '1720734118151459308', 'selfLink': 'https://www.googleapis.com/drive/v2/files/0ByHjqvhF6xU8XGJLOUREVUVSWE0/permissions/1720734118151459308'}]
----------
{'withLink': False, 'type': 'anyone', 'role': 'reader', 'kind': 'drive#permission', 'etag': '"_x2BEY9xkGriLb2EI-wbdIricX8/HCM4BvjWTJ3XhPBw3h0wgA7Cruo"', 'id': 'anyone', 'selfLink': 'https://www.googleapis.com/drive/v2/files/0ByHjqvhF6xU8XGJLOUREVUVSWE0/permissions/anyone'}
----------
[{'photoLink': 'https://lh4.googleusercontent.com/-KhPJ2d6XQY4/AAAAAAAAAAI/AAAAAAAAAAA/JCJdY_mh4PA/s64/photo.jpg', 'role': 'owner', 'domain': 'stackoverflow.com', 'kind': 'drive#permission', 'emailAddress': 'user272735@stackoverflow.com', 'name': 'user272735', 'type': 'user', 'etag': '"_x2BG98d9xdLfiLb2EI-wbdIricX8/9AA4fWf3tZymo0DbdDmcl8WsxSY"', 'id': '1720734118151459308', 'selfLink': 'https://www.googleapis.com/drive/v2/files/0ByHjqvhF6xU8XGJLOUREVUVSWE0/permissions/1720734118151459308'},
{'withLink': False, 'type': 'anyone', 'role': 'reader', 'kind': 'drive#permission', 'etag': '"_x2BEY9xkGriLb2EI-wbdIricX8/HCM4BvjWTJ3XhPBw3h0wgA7Cruo"', 'id': 'anyone', 'selfLink': 'https://www.googleapis.com/drive/v2/files/0ByHjqvhF6xU8XGJLOUREVUVSWE0/permissions/anyone'}]