使用Python的权限不足的Google表格

时间:2018-03-27 02:33:47

标签: python google-sheets-api gspread

我正在尝试一起使用python和google表。这是我的代码:

import gspread
from oauth2client.service_account import ServiceAccountCredentials

#use creds to create a client to interact with the Google Drive API
scope = ['https://spreadsheets.google.com/feeds']
creds = ServiceAccountCredentials.from_json_keyfile_name('client_secret.json', scope)
client = gspread.authorize(creds)

#Find a workbook by name and open the first sheet
#Make sure you use the right name here.
sheet = client.open("Year 9 Camp").sheet1

#Extract and print all of the values
list_of_hashes = sheet.get_all_records()
print(list_of_hashes)

然而,它不断出现错误:

Traceback (most recent call last):
  File "C:\Users\cswel\OneDrive\Documents\School\13CSC\spreedsheet.py.py", line 12, in <module>
    sheet = client.open("Year 9 Camp").sheet1
  File "C:\Python34\lib\site-packages\gspread\v4\client.py", line 103, in open
    self.list_spreadsheet_files()
  File "C:\Python34\lib\site-packages\gspread\v4\client.py", line 80, in list_spreadsheet_files
    r = self.request('get', url)
  File "C:\Python34\lib\site-packages\gspread\v4\client.py", line 73, in request
    raise APIError(response)
gspread.v4.exceptions.APIError: {
 "error": {
  "errors": [
   {
    "domain": "global",
    "reason": "insufficientPermissions",
    "message": "Insufficient Permission"
   }
  ],
  "code": 403,
  "message": "Insufficient Permission"
 }
}

有人可以帮助我的错误或给我一个非常简单的方法来连接谷歌表和python?我发现YouTube上和在线的教程难以理解。

非常感谢所给出的任何答案。

1 个答案:

答案 0 :(得分:0)

scope = ['https://spreadsheets.google.com/feeds']

只需将驱动器添加到如下所示的范围内,它将对我有效

scope = ['https://spreadsheets.google.com/feeds', 'https://www.googleapis.com/auth/drive']