1

I am trying to download a file from Google Drive using Python3. I am getting the following error:

Traceback (most recent call last):
  File "/run/media/prateek/EC3096E23096B356/Users/Prateek Agrawal/Google Drive/projects/mini-project/cloudDrive/a.py", line 91, in <module>
    status, done = downloader.next_chunk()
  File "/usr/lib/python3.6/site-packages/oauth2client/_helpers.py", line 133, in positional_wrapper
    return wrapped(*args, **kwargs)
  File "/usr/lib/python3.6/site-packages/googleapiclient/http.py", line 692, in next_chunk
    raise HttpError(resp, content, uri=self._uri)
googleapiclient.errors.HttpError: <HttpError 403 when requesting https://www.googleapis.com/drive/v2/files/0B8C3Gga47u6-MVlPMnR1MENYb2M?alt=media returned "The authenticated user has not granted the app 159208223574 read access to the file 0B8C3Gga47u6-MVlPMnR1MENYb2M">

My code is this:

    import io
    from googleapiclient.http import MediaIoBaseDownload
    file_id = '0B8C3Gga47u6-MVlPMnR1MENYb2M'
    request = drive_service.files().get_media(fileId=file_id)
    fh = io.BytesIO()
    downloader = MediaIoBaseDownload(fh, request)
    done = False
    while done is False:
        status, done = downloader.next_chunk()
        print("Downloading", status.progress() * 100)

I found this code on Google's official tutorial.

I can access metadata of file but It is giving permission error to download the file. For authentication, I am using the same Google Account from which I am trying to download this file. I have checked metadata of the file and I found in metadata that I have permission to download (because I am an owner).

The following is metadata of file:

{
"kind": "drive#file",
"id": "0B8C3Gga47u6-MVlPMnR1MENYb2M",
"etag": "\"bFBIJPAQTIelBiVAnqXY4FaYwW4/MTUwODI3NjI3NDUzNA\"",
"selfLink": "https://www.googleapis.com/drive/v2/files/0B8C3Gga47u6-MVlPMnR1MENYb2M",
"webContentLink": "https://drive.google.com/uc?id=0B8C3Gga47u6-MVlPMnR1MENYb2M&export=download",
"alternateLink": "https://drive.google.com/file/d/0B8C3Gga47u6-MVlPMnR1MENYb2M/view?usp=drivesdk",
"embedLink": "https://drive.google.com/file/d/0B8C3Gga47u6-MVlPMnR1MENYb2M/preview?usp=drivesdk",
"iconLink": "https://drive-thirdparty.googleusercontent.com/16/type/application/zip",
"title": "folder2-20171017T213157Z-001.zip",
"mimeType": "application/zip",
"labels": {
  "starred": false,
  "hidden": false,
  "trashed": false,
  "restricted": false,
  "viewed": true
},
"createdDate": "2017-10-17T21:37:54.534Z",
"modifiedDate": "2017-10-17T21:37:54.534Z",
"modifiedByMeDate": "2017-10-17T21:37:54.534Z",
"lastViewedByMeDate": "2017-10-18T09:18:25.901Z",
"markedViewedByMeDate": "1970-01-01T00:00:00.000Z",
"version": "4",
"parents": [
  {
    "kind": "drive#parentReference",
    "id": "0AMC3Gga47u6-Uk9PVA",
    "selfLink": "https://www.googleapis.com/drive/v2/files/0B8C3Gga47u6-MVlPMnR1MENYb2M/parents/0AMC3Gga47u6-Uk9PVA",
    "parentLink": "https://www.googleapis.com/drive/v2/files/0AMC3Gga47u6-Uk9PVA",
    "isRoot": true
  }
],
"userPermission": {
  "kind": "drive#permission",
  "etag": "\"bFBIJPAQTIelBiVAnqXY4FaYwW4/InBwySrqVAcXWoXoJ-e22C6as5M\"",
  "id": "me",
  "selfLink": "https://www.googleapis.com/drive/v2/files/0B8C3Gga47u6-MVlPMnR1MENYb2M/permissions/me",
  "role": "owner",
  "type": "user"
},
"originalFilename": "folder2-20171017T213157Z-001.zip",
"fileExtension": "zip",
"md5Checksum": "a97c09989b2c14e3c5866c8390da1daa",
"fileSize": "777",
"quotaBytesUsed": "777",
"ownerNames": [
  "Abacus Gla"
],
"owners": [
  {
    "kind": "drive#user",
    "displayName": "Abacus Gla",
    "picture": {
      "url": "https://lh3.googleusercontent.com/-f5pVFhZGOyo/AAAAAAAAAAI/AAAAAAAAAA0/idMajpfWGVA/s64/photo.jpg"
    },
    "isAuthenticatedUser": true,
    "permissionId": "16118565815584902923",
    "emailAddress": "abacussocietygla@gmail.com"
  }
],
"lastModifyingUserName": "Abacus Gla",
"lastModifyingUser": {
  "kind": "drive#user",
  "displayName": "Abacus Gla",
  "picture": {
    "url": "https://lh3.googleusercontent.com/-f5pVFhZGOyo/AAAAAAAAAAI/AAAAAAAAAA0/idMajpfWGVA/s64/photo.jpg"
  },
  "isAuthenticatedUser": true,
  "permissionId": "16118565815584902923",
  "emailAddress": "abacussocietygla@gmail.com"
},
"capabilities": {
  "canCopy": true,
  "canEdit": true
},
"editable": true,
"copyable": true,
"writersCanShare": true,
"shared": false,
"explicitlyTrashed": false,
"appDataContents": false,
"headRevisionId": "0B8C3Gga47u6-WmEvTGx4cms0eGtJOTVaRXZtU2gwN2JoTG1FPQ",
"spaces": [
  "drive"
]

How can I fix this problem? Is there any alternate way to download a file using Python3?

PRATEEK AGRAWAL
  • 319
  • 1
  • 5
  • 17

2 Answers2

1

Finally, I found the reason. Actually, when I got credentials Using Google OAuth 2.0 authentication, I added only Read MetaData scope. I was supposed to add more scopes. So by adding more scopes, it worked for me :)

PRATEEK AGRAWAL
  • 319
  • 1
  • 5
  • 17
0

Based from the Python documentation, urlopen will raise an HTTPError for those it can’t handle like `‘403’ (request forbidden).

From list of error codes,

403: ('Forbidden',
          'Request forbidden -- authorization will not help').

With this, try using urlretrieve() which will copy a network object denoted by a URL to a local file.

See this related SO post for additional insights.

Teyam
  • 7,686
  • 3
  • 15
  • 22