Search code examples
python-2.7google-drive-apigoogle-api-client

Google Drive API Client (Python): retrieve permissions


I got this error about with Google Drive API example.Can you help me ? I used an old Google project (that use Blogger API - work well) by add new Enable API for Google Drive into Google project. I make a new python file and I try to deal with Google Drive API. I used same json credentials.

My source code: https://paste.fedoraproject.org/paste/xTH1zf3tEtLH5~zvuAHf8Q

def retrieve_permissions(service, file_id):
  """Retrieve a list of permissions.

  Args:
    service: Drive API service instance.
    file_id: ID of the file to retrieve permissions for.
  Returns:
    List of permissions.
  """
  try:
    permissions = service.permissions().list(fileId=file_id).execute()
    return permissions.get('items', [])
  except errors.HttpError, error:
    print 'An error occurred: %s' % error
  return None

the main function:

if __name__ == '__main__':
    served = get_service()
    print "-------"
    print retrieve_permissions(served,'client_secret_driv.json')
    print "======="

The error is

C:\Python27\python.exe C:/Python27/goo_drive_002.py
-------
An error occurred: <HttpError 403 when requesting https://www.googleapis.com/drive/v3/files/client_secret_driv.json/permissions?alt=json returned "Insufficient Permission">
None
=======

Process finished with exit code 0

Solution

  • Modification points :

    1. In order to retrieve the permissions of file, the file ID is required. In your script, filename is given for fileID of service.permissions().list(fileId=file_id).execute(). So an error occurs. For this, it retrieves file ID from file name Using service.files().list().
    2. When you want the permission data from response, please change from permissions.get('items', []) to permissions.get('permissions', [])

    Modified script :

    file_name = 'client_secret_driv.json'
    file_id = service.files().list(q="name='" + file_name + "' and trashed=false", fields="files(id)").execute()
    permissions = service.permissions().list(fileId=file_id["files"][0]["id"]).execute()
    result = permissions.get('permissions', [])
    

    If I misunderstand your question, I'm sorry.