1

We are already successfully authenticated. We have the following python function using PyDrive:

def upload_to_drive(local_filepath):

    gfile = drive.CreateFile({'parents': [{'id': '123ourdrivefolderid456'}]})
    gfile.SetContentFile(upload_file)
    gfile.Upload() # Upload the file.

    permission = gfile.InsertPermission({ 
        'type': 'user',
        'value': 'myemail@gmail.com',
        'role': 'reader'
    }, sendNotificationEmails = False)
    
    print(gfile['alternateLink'])

We receive the error TypeError: InsertPermission() got an unexpected keyword argument 'sendNotificationEmails'. We have pydrive-1.3.1. Other sources online indicated that passing the 2nd parameter sendNotificationEmails = False should work, but it is not. We've also tried { 'sendNotificationEmails': 'False' } as a 2nd parameter to no success...

Canovice
  • 9,012
  • 22
  • 93
  • 211
  • 1
    Have you tried including `'sendNotificationEmails': False` in the first dict itself? This is a parameter used by the [permissions.insert API](https://developers.google.com/drive/api/v2/reference/permissions/insert) and the PyDrive method isn't expecting a second parameter. – shriakhilc Jan 11 '22 at 18:05
  • @shriakhilc Yes and it is not working. I've tried both that as well as with `sendNotificationEmail` not pluralized – Canovice Jan 11 '22 at 19:27

1 Answers1

1

this is the original InsertPermission method in files.py from PyDrive package:

def InsertPermission(self, new_permission):
    """Insert a new permission. Re-fetches all permissions after call.

    :param new_permission: The new permission to insert, please see the
    official Google Drive API guide on permissions.insert for details.

    :type new_permission: object

    :return: The permission object.
    :rtype: object
    """
    file_id = self.metadata.get('id') or self['id']
    try:
      permission = self.auth.service.permissions().insert(
        fileId=file_id, body=new_permission).execute(http=self.http)
    except errors.HttpError as error:
      raise ApiRequestError(error)
    else:
      self.GetPermissions()  # Update permissions field.

    return permission

it only accepts your permission dict

{
    'type': 'user',
    'value': 'myemail@gmail.com',
    'role': 'reader'
}

as the body of API requests (and no other arguments accepted as parameters).

while according to Google Drive API Documentation, the sendNotificationEmails is a query parameter that you can not find it in PyDrive's InsertPermission method.

To temporarily fix this problem I would suggest adding this parameter in the InsertPermission method and manually setting a sendNotificationEmails parameter that is used from Google Drive API in files.py of the PyDrive package.

def InsertPermission(self, new_permission, sendNotificationEmails: bool = True):
    """Insert a new permission. Re-fetches all permissions after call.

    :param new_permission: The new permission to insert, please see the
    official Google Drive API guide on permissions.insert for details.

    :type new_permission: object

    :return: The permission object.
    :rtype: object
    """
    file_id = self.metadata.get('id') or self['id']
    try:
        permission = self.auth.service.permissions().insert(
            fileId=file_id, body=new_permission, sendNotificationEmails=sendNotificationEmails).execute(
            http=self.http)
    except errors.HttpError as error:
        raise ApiRequestError(error)
    else:
        self.GetPermissions()  # Update permissions field.

    return permission

Or you can try to replace your codes from PyDrive with codes from Google Drive documentation. In this case, you would be easier to find why the error occurred, and always has detailed documentation that you could refer to.

Hope it helps you.

Guangyu He
  • 11
  • 2