Google Sheets API batchUpdate在python中返回错误

时间:2018-06-20 09:55:53

标签: python google-apis-explorer

我正尝试使用python 3.6中的以下代码将值从一张纸粘贴到google电子表格中的另一张纸上。

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.">

0 个答案:

没有答案