3

I am trying to download a random file from a Google Drive using Google Drive API. Although after running the code I got an error message: The user has not granted the app (app_code) read access to the file (filename). How can I grant a read access to the file? I haven't found anything on the internet, and in the API Dashboard either.

 # Call the Drive v3 API
    results = service.files().list(
        pageSize=1000, fields="nextPageToken, files(id, name)").execute()
    items = results.get('files', [])
    rand_item = random.choice(items)
    print('{0} ({1})'.format(rand_item['name'], rand_item['id']))
    if not items:
        print('No files found.')
    else:
        request = service.files().get_media(fileId=rand_item['id'])
        fh = io.BytesIO()
        downloader = MediaIoBaseDownload(fh, request)
        done = False
        while done is False:
            status, done = downloader.next_chunk()
            print("Download %d%%." % int(status.progress() * 100))

Full code: Pastebin

SiDzej
  • 72
  • 2
  • 9
  • Perhaps you can check out this [documentation](https://developers.google.com/drive/api/v3/manage-sharing) on managing sharing. You can also see the [permissions guide](https://developers.google.com/drive/api/v3/about-permissions) for additional details about permissions and roles along with the [reference guide](https://developers.google.com/drive/api/v3/reference/permissions). – Jacque Nov 19 '18 at 06:30

1 Answers1

5

Its due to the scope. You only gave metadata.readonly access

Change it to 'https://www.googleapis.com/auth/drive.readonly'


Google Drive API Authorizations

enter image description here

fcsr
  • 921
  • 10
  • 17