1. 程式人生 > >google sheets寫入數據時報錯:Request had insufficient authentication scopes

google sheets寫入數據時報錯:Request had insufficient authentication scopes

sta PE 示例 dev shee 刪除 ati http ID

問題

如果直接使用的是google sheets官方示例文檔quickstart.py,在寫入數據是會報錯:

Request had insufficient authentication scopes.

說明

google sheets api提供的權限有多種,具體查看authorizing中的scope說明表。

quickstart.py中使用的scope=‘https://www.googleapis.com/auth/spreadsheets.readonly‘,如下代碼段

SCOPES = 'https://www.googleapis.com/auth/spreadsheets.readonly'
CLIENT_SECRET_FILE = 'client_secret.json' APPLICATION_NAME = 'Google Sheets API Python Quickstart'

只讀權限,所以之前生成的credentials.json的權限是只讀權限。

解決

步驟1:將代碼中的scope修改為authorizing中的scope說明表中對應你想要的權限。
如,需要讀寫權限,修改scope:

scope = 'https://www.googleapis.com/auth/spreadsheets'

步驟2:然後,刪除本地已有的credentials.json,然後運行寫入數據的腳本,重新生成credentials.json文件,新生成的credentials.json就是讀寫權限都有的了。

註意:步驟2,很重要,很重要,很重要。

google sheets寫入數據時報錯:Request had insufficient authentication scopes