2

I hope someone can help. I'm using PyDrive to authenticate and upload a file to my Google Drive with no problems. Then I try to change permissions of same file to allow sharing by updating its metadata.

drive = GoogleDrive(gauth)

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

However, when I pprint the userPermission, it doesn't seem to have set the permissions as I have intended.

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

I get:

{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'
}

I don't know why the permissions are being set properly. I'm not getting any errors of any kind. Is there anything wrong with how I'm setting the variables within userPermission? Thanks for any help.

user272735
  • 10,473
  • 9
  • 65
  • 96
mkatsu
  • 21
  • 3

1 Answers1

2

I'm not sure if you can set permissions during file creation. Also I'm not sure if you can modify userPermission at all. But what you can do is to modify permissions list with InsertPermission.

Below you'll find an example how to add a sharing permission. I think the code below is self-explanatory.

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()

Example

$ 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'}]
user272735
  • 10,473
  • 9
  • 65
  • 96