ITGarry
ITGarry

Reputation: 23

404 error when accessing a shared drive via Google Drive API

I'm trying to list the metadata of a shared drive. Below is the code:

from __future__ import print_function
import os.path
from googleapiclient.discovery import build
from google_auth_oauthlib.flow import InstalledAppFlow
from google.auth.transport.requests import Request
from google.oauth2.credentials import Credentials

SCOPES = [
'https://www.googleapis.com/auth/drive',
'https://www.googleapis.com/auth/drive.file',
'https://www.googleapis.com/auth/drive.metadata'
]
creds = None

if os.path.exists('token.json'):
    creds = Credentials.from_authorized_user_file('token.json', SCOPES)
    
if not creds or not creds.valid:
    if creds and creds.expired and creds.refresh_token:
        creds.refresh(Request())
    else:
        flow = InstalledAppFlow.from_client_secrets_file('credentials.json', SCOPES)
        creds = flow.run_local_server(port=8080)
    with open('token.json', 'w') as token:
        token.write(creds.to_json())

The code above works; it can authenticate successfully.

service = build('drive', 'v3', credentials=creds)
file_id = '0AFmX-a2BvgHBUk9PVA'
results = service.permissions().list(fileId=file_id).execute()

The output of the code is the following:

Traceback (most recent call last): File "<pyshell#23>", line 1, in results = service.permissions().list(fileId=file_id).execute() File "/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/site-packages/googleapiclient/_helpers.py", line 134, in positional_wrapper return wrapped(*args, **kwargs) File "/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/site-packages/googleapiclient/http.py", line 935, in execute raise HttpError(resp, content, uri=self.uri) googleapiclient.errors.HttpError: <HttpError 404 when requesting https://www.googleapis.com/drive/v3/files/0AFmX-a2BvgHBUk9PVA/permissions?alt=json returned "File not found: 0AFmX-a2BvgHBUk9PVA.". Details: "[{'domain': 'global', 'reason': 'notFound', 'message': 'File not found: 0AFmX-a2BvgHBUk9PVA.', 'locationType': 'parameter', 'location': 'fileId'}]">

The file definitely exists. Is the code wrong? Am I using the right permission scope? Do I need to use a service account instead of an OAuth client ID?

Upvotes: 2

Views: 2538

Answers (1)

Tanaike
Tanaike

Reputation: 201378

I believe your goal and your current situation as follows.

  • You want to retrieve the permission list from the shared Drive.
  • You can access to the shared Drive.

In this case, please include supportsAllDrives to the query parameter for the request as follows.

supportsAllDrives: Whether the requesting application supports both My Drives and shared drives. (Default: false)

From:

results = service.permissions().list(fileId=file_id).execute()

To:

results = service.permissions().list(fileId=file_id, supportsAllDrives=True).execute()

Reference:

Upvotes: 6

Related Questions