首页 > 解决方案 > 无法在不出现 404 错误的情况下更改 Google Sheets API 中的 SCOPES:未找到请求的实体

问题描述

每当我尝试运行该代码时都会收到此错误: HttpError 404 when requesting https://sheets.googleapis.com/v4/spreadsheets/1KpJw640oNNwVfEasGoSffrfoyC7i1ryHakdWZmr-AX4/values:batchUpdate?alt=json返回“未找到请求的实体。”>我正在尝试借助此 API 更新 Google Sheet 文件,但我管理使用的唯一范围是“只读”。我尝试删除 token.pickle 文件:我已被识别,因此该请求不需要身份验证凭据。但我不知道为什么我仍然会收到错误。所以基本上我能做的就是从工作表中提取数据而不是覆盖单元格。

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

 SCOPES = ['https:// www.googleapis.com/auth/spreadsheets'] #what I have changed from the Python Quickstart code from the Google Sheets API website

 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()
    with open('token.pickle', 'wb') as token:
        pickle.dump(creds, token)

 service = discovery.build('sheets', 'v4', credentials=creds)

 spreadsheet_id = '1KpJw640oNNwVfEasGoSffrfoyC7i1ryHakdWZmr-AX4' #this id is correct and still I get an error

 batch_update_values_request_body = {
     'value_input_option': 'USER_ENTERED',

     'data': [
     {
       "range": "Control Panel!A:B",
       "majorDimension": "ROWS",
       "values": [[1,2]]
     }
    ], 
   }

 request = service.spreadsheets().values().batchUpdate(spreadsheetId=spreadsheet_id, 
 body=batch_update_values_request_body)
 response = request.execute()

 pprint(response)

标签: pythongoogle-sheets-api

解决方案


推荐阅读