Search code examples
pythongoogle-drive-api

Having Trouble finding the correct scope for my API request to Google Drive API


This is likely just a matter of me not knowing how to find the correct scope but I need help all the same.

from google.oauth2 import service_account
from googleapiclient.discovery import build
import requests
import pprint
from dotenv import load_dotenv
import os

forcewinds_folder_url = 'fake-folder-url'

load_dotenv()
api_key = os.getenv('JAKE_KEY')
json_key_location = os.getenv('API_KEY_JSON_PATH')
# Replace 'YOUR_JSON_FILE.json' with the path to your downloaded JSON credentials file
credentials = service_account.Credentials.from_service_account_file(json_key_location, scopes=['https://www.googleapis.com/auth/documents.readonly'])

# Create a Google Drive API service
drive_service = build('drive', 'v3', credentials=credentials)

folder_name = 'fake-folder-id'

results = drive_service.files().list(q=f"name = '{folder_name}' and mimeType = 'application/vnd.google-apps.folder'").execute()
folders = results.get('files', [])

# Check if the folder was found and retrieve its ID
if folders:
    folder_id = folders[0]['id']
    print(f"The folder ID for '{folder_name}' is: {folder_id}")
else:
    print(f"No folder found with the name '{folder_name}'")

This code is giving me this error

File "get_txt.py", line 21, in <module>
    results = drive_service.files().list(q=f"name = '{folder_name}' and mimeType = 'application/vnd.google-apps.folder'").execute()
  File "/home/zoctavous/.local/lib/python3.8/site-packages/googleapiclient/_helpers.py", line 130, in positional_wrapper    return wrapped(*args, **kwargs)
  File "/home/zoctavous/.local/lib/python3.8/site-packages/googleapiclient/http.py", line 938, in execute
    raise HttpError(resp, content, uri=self.uri)
googleapiclient.errors.HttpError: <HttpError 403 when requesting https://www.googleapis.com/drive/v3/files?q=name+%3D+%271RyT2IS-dhGLCNcod_LVt246463PBR3bH%27+and+mimeType+%3D+%27application%2Fvnd.google-apps.folder%27&alt=json returned "Request had insufficient authentication scopes.". Details: "[{'message': 'Insufficient Permission', 'domain': 'global', 'reason': 'insufficientPermissions'}]">

Which indicates a permissions issue. I have a json for the credentials of my project that has Google Drive API enabled here enter image description here

I've made sure I've updated my authentication json every time I add permissions to that account and I've given it everything that seems to make sense to me to access a google drive folder on my account. Im fairly sure that I am authenticating and creating my credentials correctly so im fairly stuck.

What am I doing wrong here?


Solution

  • It seems that in your showing script, https://www.googleapis.com/auth/documents.readonly is used as the scope. But, in your script, you are trying to use Drive API. I thought that this might be the reason for your current issue of Request had insufficient authentication scopes.. In this case, please modify the scope. Please select the scope you want to use from the following scopes.

    If you use only "Method: files.list of Drive API v3", you can use the following scope.

    • https://www.googleapis.com/auth/drive.metadata.readonly

    or, if you want to retrieve the file content, please use the following scope.

    • https://www.googleapis.com/auth/drive.readonly

    or, if you want to retrieve and update the files, please use the following scope.

    • https://www.googleapis.com/auth/drive

    References: