ファイルの作成中にアクセス許可を設定できるかどうかわかりません。また、まったく変更できるかどうかもわかりません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'}]