title: Downloading files from public Google Drive in python: scoping issues?
body: <p>Using my answer to <a href="https://stackoverflow.com/questions/68270332/automatically-download-large-files-in-public-gdrive-folder">my question</a> on how to download files from a public Google drive I managed in the past to download images using their IDs from a python script and Google API v3 from a public drive using the following bock of code:</p>
  <pre class="lang-py prettyprint-override"><code>from google_auth_oauthlib.flow import Flow, InstalledAppFlow
  from googleapiclient.discovery import build
  from googleapiclient.http import MediaFileUpload, MediaIoBaseDownload
  from google.auth.transport.requests import Request
  import io
  import re
  SCOPES = ['https://www.googleapis.com/auth/drive']
  CLIENT_SECRET_FILE = &quot;myjson.json&quot;
  authorized_port = 6006 # authorize URI redirect on the console
  flow = InstalledAppFlow.from_client_secrets_file(CLIENT_SECRET_FILE, SCOPES)
  cred = flow.run_local_server(port=authorized_port)
  drive_service = build(&quot;drive&quot;, &quot;v3&quot;, credentials=cred)
  regex = &quot;(?&lt;=https://drive.google.com/file/d/)[a-zA-Z0-9]+&quot;
  for i, l in enumerate(links_to_download):
      url = l
      file_id = re.search(regex, url)[0]
      request = drive_service.files().get_media(fileId=file_id)
      fh = io.FileIO(f&quot;file_{i}&quot;, mode='wb')
      downloader = MediaIoBaseDownload(fh, request)
      done = False
      while done is False:
          status, done = downloader.next_chunk()
          print(&quot;Download %d%%.&quot; % int(status.progress() * 100))
  </code></pre>
  <p>In the mean time I discovered <a href="https://github.com/googlearchive/PyDrive" rel="nofollow noreferrer">pydrive</a> and <a href="https://github.com/iterative/PyDrive2" rel="nofollow noreferrer">pydrive2</a>, two wrappers around Google API v2 that allows to do very useful things such as listing files from folders and basically allows to do the same thing with a lighter syntax:</p>
  <pre class="lang-py prettyprint-override"><code>from pydrive.auth import GoogleAuth
  from pydrive.drive import GoogleDrive
  import io
  import re
  CLIENT_SECRET_FILE = &quot;client_secrets.json&quot;
  
  gauth = GoogleAuth()
  gauth.LocalWebserverAuth()
  drive = GoogleDrive(gauth)
  regex = &quot;(?&lt;=https://drive.google.com/file/d/)[a-zA-Z0-9]+&quot;
  for i, l in enumerate(links_to_download):
      url = l
      file_id = re.search(regex, url)[0]
      file_handle = drive.CreateFile({'id': file_id})
      file_handle.GetContentFile(f&quot;file_{i}&quot;)
  </code></pre>
  <p>However now whether I use pydrive or the raw API <strong>I cannot seem to be able to download the same files</strong> and instead I am met with:</p>
  <pre><code>googleapiclient.errors.HttpError: &lt;HttpError 404 when requesting https://www.googleapis.com/drive/v3/files/fileID?alt=media returned &quot;File not found: fileID.&quot;. Details: &quot;[{'domain': 'global', 'reason': 'notFound', 'message': 'File not found: fileID.', 'locationType': 'parameter', 'location': 'fileId'}]&quot;&gt;
  </code></pre>
  <p>I tried everything and registered 3 different apps using Google console it seems it might be (or not) a question of scoping (see for instance <a href="https://stackoverflow.com/a/54492150/4844184">this answer</a>, with apps having access to only files in my Google drive or created by this app). However I did not have this issue before (last year).</p>
  <p>When going to the <a href="https://console.cloud.google.com/apis/credentials/consent?authuser=1" rel="nofollow noreferrer">Google console</a> explicitly giving <code>https://www.googleapis.com/auth/drive</code> as a scope to the API mandates filling a ton of fields with application's website/conditions of use/confidentiality rules/authorized domains and youtube videos explaining the app. However I will be the sole user of this script.
  So I could only give explicitly the following scopes:</p>
  <pre><code>/auth/drive.appdata
  /auth/drive.file
  /auth/drive.install
  </code></pre>
  <p>Is it because of scoping ? Is there a solution that doesn't require creating a homepage and a youtube video ?</p>
  <p><strong>EDIT 1:</strong>
  Here is an example of <code>links_to_download</code>:</p>
  <pre><code>links_to_download = [&quot;https://drive.google.com/file/d/fileID/view?usp=drivesdk&amp;resourcekey=0-resourceKeyValue&quot;]
  </code></pre>
  <p><strong>EDIT 2:</strong>
  It is super instable sometimes it works without a sweat sometimes it doesn't. When I relaunch the script multiple times I get different results. Retry policies are working to a certain extent but sometimes it fails multiple times for hours.</p>