python)谷歌电子表格:更新API不适用于403
我正在关注这个官方链接的教程: https : //developers.google.com/sheets/quickstart/python
我执行了“quickstart.py”进行身份validation。 之后,我再次运行“quickstart.py”,看到本教程中获得的来自“ https://docs.google.com/spreadsheets/d/1BxiMVs0XRA5nFMdKvBdBZjgmUUqptlbs74OgvE2upms/edit#gid=0 ”的数据。
我确实将电子表格ID更改为我自己的ID,并通过以下方法从我的电子表格中获取数据: service.spreadsheets().values().get().execute()
但是我的目标是将数据添加到我的电子表格中,所以我使用了如下的方法'update':
rangeName = 'A2:D' body['range'] = rangeName body['majorDimension'] = 'ROWS' body['values'] = ['test','test','test','test'] result = service.spreadsheets().values().update( spreadsheetId=spreadsheetId, range=rangeName, body=body).execute() print('result:'+str(result))
然后我得到一个错误:
googleapiclient.errors.HttpError:https://sheets.googleapis.com/v4/spreadsheets/MY_SPREADSHEET_ID/values/A2%3AD?alt=json返回“请求的身份validation范围不足”。>
我不知道为什么这个错误发生时,试图更新我的工作表,以及为什么这个错误不会发生时,试图从我的工作表中获取数据(如果这是由authentication造成的,方法'get'也应该导致它! )
谢谢。
quickstart.py示例将范围设置为:
https://www.googleapis.com/auth/spreadsheets.readonly
要更新电子表格,您需要将范围设置为:
https://www.googleapis.com/auth/spreadsheets
您可以通过首先删除〜/ .credentials(即树莓上的位置)中的现有身份validation文件来执行此操作。 它可能会被称为“sheets.googleapis.com-python-quickstart.json。
删除之后,您需要重新进行身份validation,重新运行该脚本时会自动发生。