2

我希望有人能帮帮忙。我正在使用 PyDrive 进行身份验证并将文件上传到我的 Google Drive,没有任何问题。然后我尝试通过更新其元数据来更改同一文件的权限以允许共享。

drive = GoogleDrive(gauth)

file1 = drive.CreateFile({'title': fName, 'shareable':True, 
        'userPermission': [{'kind':'drive#permission', 'type': 'anyone',
        'value': 'anyone','role': 'reader'}]})
file1.SetContentFile(fName)

但是,当我打印 userPermission 时,它似乎没有按照我的意图设置权限。

userPermission = file1['userPermission']
pprint.pprint(userPermission)

我得到:

{u'etag': u'"pvTNHKA6KkAgXTpZXMwU4Pa7ELo/gZDvZYCYVjI-u0hbut2HQwdCArU"',
 u'id': u'me',
 u'kind': u'drive#permission',
 u'role': u'owner',
 u'selfLink': u'https://www.googleapis.com/drive/v2/files/0B1aI5mMxQzh3MktDMWl1ZFdOZw0/permissions/me',
 u'type': u'user'
}

我不知道为什么权限设置正确。我没有收到任何类型的错误。我在 userPermission 中设置变量的方式有什么问题吗?谢谢你的帮助。

4

1 回答 1

1

我不确定您是否可以在文件创建期间设置权限。另外我不确定你是否可以修改userPermission。但是你可以做的是permissionsInsertPermission.

您将在下面找到如何添加共享权限的示例。我认为下面的代码是不言自明的。

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'}]
于 2016-12-04T11:59:02.500 回答