LKM*_*LKM 4 python excel google-sheets google-spreadsheet-api
我正在通过以下官方链接关注本教程:https : //developers.google.com/sheets/quickstart/python
我确实执行了“ quickstart.py”进行身份验证。之后,我再次运行“ quickstart.py”,并在本教程获得时看到了来自“ https://docs.google.com/spreadsheets/d/1BxiMVs0XRA5nFMdKvBdBZjgmUUqptlbs74OgvE2upms/edit#gid=0 ” 的数据。
我确实将电子表格ID更改为自己的ID,并通过以下方法从电子表格中获取数据:service.spreadsheets().values().get().execute()
但是我的目标是向电子表格中添加数据,因此我使用了“更新”方法,如下所示:
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))
Run Code Online (Sandbox Code Playgroud)
然后我得到一个错误:
googleapiclient.errors.HttpError:https://sheets.googleapis.com/v4/spreadsheets/MY_SPREADSHEET_ID/values/A2%3AD?alt=json返回“请求的身份验证范围不足。
我不知道为什么在尝试更新我的工作表时会出现这种错误,为什么在尝试从我的工作表中获取数据时却不会发生此错误。 )
谢谢。
quickstart.py示例将范围设置为:
https://www.googleapis.com/auth/spreadsheets.readonly
Run Code Online (Sandbox Code Playgroud)
要更新电子表格,您需要将范围设置为:
https://www.googleapis.com/auth/spreadsheets
Run Code Online (Sandbox Code Playgroud)
为此,您可以先删除〜/ .credentials中的现有身份验证文件(即树莓派上的位置)。它可能会被称为“ sheets.googleapis.com-python-quickstart.json”。
删除它之后,您将需要重新进行身份验证,这在您重新运行脚本时会自动发生。
| 归档时间: |
|
| 查看次数: |
1453 次 |
| 最近记录: |