首页 > 解决方案 > google sheet API batchUpdate在python中返回错误

问题描述

我正在尝试使用 python 3.6 中的以下代码将值从一张表粘贴到另一张表中。

from apiclient.discovery import build
from httplib2 import Http
from oauth2client import file, client, tools

SCOPES = 'https://www.googleapis.com/auth/spreadsheets'
store = file.Storage('credentials.json')
creds = store.get()
if not creds or creds.invalid:
    flow = client.flow_from_clientsecrets('client_secret.json', SCOPES)
    creds = tools.run_flow(flow, store)

service = build('sheets', 'v4', http=creds.authorize(Http()))
spreadsheet_id = 'XXXXXXXXXXXXXXXXX'

request_body = {
    'requests': [{
    'copyPaste':{
      "source": {
                "sheetId": XXXXXX,
                "startRowIndex": 0,
                "endRowIndex": 1000,
                "startColumnIndex": 0,
                "endColumnIndex": 5
              },
              "destination": {
                "sheetId": XXXXXXXX,
                "startRowIndex": 0,
                "endRowIndex": 1000,
                "startColumnIndex": 0,
                "endColumnIndex": 5
              },
              "pasteOrientation": "NORMAL",
              "pasteType": "PASTE_VALUES"
    }
}]
}

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

pprint(response)

但是我不断收到以下错误,我尝试添加更多范围 URL,但无济于事。

---------------------------------------------------------------------------

---> 41 response = request.execute()

HttpError: <HttpError 403 when requesting https://sheets.googleapis.com/v4/spreadsheets/xxxxxxxxxxxxxxxxxx?alt=json returned "Request had insufficient authentication scopes.">

标签: pythongoogle-apis-explorer

解决方案


推荐阅读