J'essaye de comprendre quelle est cette erreur :

  File "C:\...\googleapiclient\http.py", line 856, in execute
    raise HttpError(resp, content, uri=self.uri)
googleapiclient.errors.HttpError: <HttpError 403 when requesting https://www.googleapis.com/drive/v3/files/19XYXTA2xER982xIHnDqH0cOtjBtSZtgC?alt=media returned "Insufficient Permission: Request had insufficient authentication scopes.">

Que signifie « Autorisation insuffisante : la demande avait des étendues d'authentification insuffisantes ». moyenne?

Ci-dessous le code complet : credentials.json et client_secrets.json sont tous deux présents dans mon répertoire.

from __future__ import print_function
import pickle
import os.path
from pydrive.auth import GoogleAuth
from pydrive.drive import GoogleDrive
from googleapiclient.discovery import build
from google_auth_oauthlib.flow import InstalledAppFlow
from google.auth.transport.requests import Request
import os
g_login = GoogleAuth()
g_login.LocalWebserverAuth()  # This sucker might break [NK]
drive = GoogleDrive(g_login)
#end of tempCodeForEaseOfUse [NK]

# If modifying these scopes, delete the file token.pickle.
SCOPES = ['https://www.googleapis.com/auth/spreadsheets https://www.googleapis.com/auth/documents'] # https://www.googleapis.com/auth/drive.metadata.readonly https://www.googleapis.com/auth/documents.readonly']

# The ID and range of a sample spreadsheet.
SAMPLE_SPREADSHEET_ID = '1eb0DIJ7QglHqCrvIW94MUbMvFdbGKGXBSwaH49ILCvQ/edit?usp=sharing'
SAMPLE_RANGE_NAME = 'Class Data!A2:E'

creds = None
# The file token.pickle stores the user's access and refresh tokens, and is
# created automatically when the authorization flow completes for the first
# time.
if os.path.exists('token.pickle'):
    with open('token.pickle', 'rb') as token:
        creds = pickle.load(token)
# If there are no (valid) credentials available, let the user log in.
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=0)
    # Save the credentials for the next run
    with open('token.pickle', 'wb') as token:
        pickle.dump(creds, token)


# TODO: fileID extracted manually, must automate [NK]
fileId = '19XYXTA2xER982xIHnDqH0cOtjBtSZtgC/view?usp=sharing'  # Please set the file ID of text file. [StackTanaike]

sheets = build('sheets', 'v4', credentials=creds)
drive = build('drive', 'v3', credentials=creds)

# Retrieve data from Google Drive and parse data as an array. [StackTanaike]
data = drive.files().get_media(fileId=fileId).execute()
1
5Daydreams 24 janv. 2020 à 00:26

1 réponse

Meilleure réponse

Vos étendues ne sont pas définies correctement. Remplacez votre SCOPES = ['https:// ... par ce qui suit :

SCOPES = ['https://www.googleapis.com/auth/spreadsheets', 'https://www.googleapis.com/auth/drive']

1
Sam 23 janv. 2020 à 21:32