如何解决Python中的Google Sheet API错误

如何解决Python中的Google Sheet API错误,python,google-api,google-sheets-api,Python,Google Api,Google Sheets Api,我使用以下代码将Gsheet下载到Python中。它适用于某些图纸,但不是所有图纸 import pickle import os.path from google_auth_oauthlib.flow import InstalledAppFlow from google.auth.transport.requests import Request from googleapiclient.discovery import build def gsheet_api_check(SCOPES)

我使用以下代码将Gsheet下载到Python中。它适用于某些图纸,但不是所有图纸

import pickle
import os.path
from google_auth_oauthlib.flow import InstalledAppFlow
from google.auth.transport.requests import Request
from googleapiclient.discovery import build

def gsheet_api_check(SCOPES):
    creds = None
    if os.path.exists('token.pickle'):
        with open('token.pickle', 'rb') as token:
            creds = pickle.load(token)
    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)
        with open('token.pickle', 'wb') as token:
            pickle.dump(creds, token)
    return creds


def pull_sheet_data(SCOPES,SPREADSHEET_ID,RANGE_NAME):
    creds = gsheet_api_check(SCOPES)
    service = build('sheets', 'v4', credentials=creds)
    sheet = service.spreadsheets()
    #result = copy_file(service, SPREADSHEET_ID, copy_title)
    result = sheet.values().get(spreadsheetId=SPREADSHEET_ID, range=RANGE_NAME).execute()
    values = result.get('values', [])

    if not values:
        print('No data found.')
    else:
        rows = sheet.values().get(spreadsheetId=SPREADSHEET_ID, range=RANGE_NAME).execute()
        data = rows.get('values')
        print("COMPLETE: Data copied")
        return data
在导致错误的工作表上,我收到以下消息:

HttpError: <HttpError 400 when requesting https://sheets.googleapis.com/v4/spreadsheets/1bI_X2ssOj9gTYnKsa_CDajYP0FcFMr0E/values/3-7-14days?alt=json returned "This operation is not supported for this document">
现在我得到以下错误:

<HttpError 403 when requesting https://www.googleapis.com/drive/v3/files/1bI_X2ssOj9gTYnKsa_CDajYP0FcFMr0E/copy?alt=json returned "Insufficient Permission: Request had insufficient authentication scopes.">

在Excel文件上使用Sheets API之前,您需要将其转换为Google电子表格 使用Google sheets打开Excel文件并不等同于转换它

您可以通过编程方式转换文件,例如使用驱动器API的方法

样本请求:

service=build('drive','v3',credentials=creds)
service.files()

将转换后的文件的
id
作为
电子表格id
用于您的请求。

您的意思是使用
电子表格id
请求的文档是.xlsx文件吗?或者它是一个Google Sheets文件,其中一个.xlsx文件已被转换为该文件?您是如何进行转换的?Google sheets转换的是excel。出于某种原因,有些文件可以工作,但有些文件会出现错误。你是如何转换这些文件的?它被发送到我的gmail,我在Gsheets中自动打开了它。非常感谢,但这将在Gsheets API的v4中工作。v3正在贬值,请不要将驱动器API与工作表API混淆。Sheets API v3确实不推荐使用,但对于驱动器API-v3是最新版本。您甚至可以按原样使用驱动器API v2(未弃用)进行转换。请注意,如果您调用了sheet service
service
,您应该给驱动器服务另一个名称,例如
driveService=build('drive','v3',credentials=creds)
谢谢,在添加驱动器和Sheets API之后,我似乎遇到了更多的麻烦。请参阅上面的我的更新。您需要在GCP控制台中启用驱动器API的作用域,并将这两个作用域分配给变量
scopes
。之后,您需要删除您的令牌文件以触发新的授权流。
<HttpError 403 when requesting https://www.googleapis.com/drive/v3/files/1bI_X2ssOj9gTYnKsa_CDajYP0FcFMr0E/copy?alt=json returned "Insufficient Permission: Request had insufficient authentication scopes.">