Python: ошибки при обращения к Google Sheets
Подскажите пожалуйста, что не так делаю, почему в этом коде:
import httplib2
import apiclient.discovery
from oauth2client.service_account import ServiceAccountCredentials
CREDENTIALS_FILE = 'fastpoverka-v2-08eef534af66.json' # Имя файла с закрытым ключом, вы должны подставить свое
# Читаем ключи из файла
credentials = ServiceAccountCredentials.from_json_keyfile_name(CREDENTIALS_FILE, ['https://www.googleapis.com/auth/spreadsheets', 'https://www.googleapis.com/auth/drive'])
httpAuth = credentials.authorize(httplib2.Http()) # Авторизуемся в системе
service = apiclient.discovery.build('sheets', 'v4', http = httpAuth) # Выбираем работу с таблицами и 4 версию API
spreadsheetId = '19eU_PwEk-zDDKkdPidRTLfi1hpnBbJVHOKwGTLU9X_c' # сохраняем идентификатор файла
print('https://docs.google.com/spreadsheets/d/' + spreadsheetId)
# Получаем список листов, их Id и название
spreadsheet = service.spreadsheets().get(spreadsheetId=spreadsheetId).execute()
sheetList = spreadsheet.get('sheets')
for sheet in sheetList:
print(sheet['properties']['sheetId'], sheet['properties']['title'])
sheetId = sheetList[0]['properties']['sheetId']
Выдаёт такие ошибки:
Traceback (most recent call last):
File "/Users/turremail.ru/PycharmProjects/documets/test.py", line 17, in <module>
spreadsheet = service.spreadsheets().get(spreadsheetId=spreadsheetId).execute()
File "/Users/turremail.ru/PycharmProjects/documets/venv/lib/python3.9/site-packages/googleapiclient/_helpers.py", line 130, in positional_wrapper
return wrapped(*args, **kwargs)
File "/Users/turremail.ru/PycharmProjects/documets/venv/lib/python3.9/site-packages/googleapiclient/http.py", line 856, in execute
raise HttpError(resp, content, uri=self.uri)
googleapiclient.errors.HttpError: <HttpError 403 when requesting https://sheets.googleapis.com/v4/spreadsheets/19eU_PwEk-zDDKkdPidRTLfi1hpnBbJVHOKwGTLU9X_c?alt=json returned "The caller does not have permission">
Спасибо.
Ответы (1 шт):
Автор решения: Михаил Таратенко
→ Ссылка
Большое спасибо за помощь. Разобрался - нужно было дать доступ к файлу сервисному аккаунту и всё заработало. Ссылка на скрин: https://drive.google.com/file/d/1bSZYP1dBkkQrHsZGsEhrF82Llc_EPlEM/view?usp=sharing