如果不出现404错误,就无法更改googlesheetsapi中的作用域:找不到请求的实体

2024-04-20 05:19:35 发布

您现在位置:Python中文网/ 问答频道 /正文

每当我尝试运行该代码时,就会出现此错误:HttpError 404 when requesting https://sheets.googleapis.com/v4/spreadsheets/1KpJw640oNNwVfEasGoSffrfoyC7i1ryHakdWZmr-AX4/values:batchUpdate?alt=jsonreturned“Requested entity was not found.”>; 我试图用这个API来更新googlesheet文件,但是我使用的唯一作用域是“readonly”。我试着删除令牌。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)

Tags: andfromimporttokenauthifrequestnot