When I try to connect, I get an error:
<HttpError 403 when requesting returned "Request had insufficient authentication scopes."
import os
from google.auth.transport.requests import Request
from google.oauth2.credentials import Credentials
from google_auth_oauthlib.flow import InstalledAppFlow
from googleapiclient.discovery import build
from googleapiclient.errors import HttpError
SCOPES = ['https://www.googleapis.com/auth/spreadsheets']
SPREADSHEET_ID = "-"
def main():
credentials = None
if os.path.exists("token.json"):
credentials = Credentials.from_authorized_user_file("token.json", SCOPES)
if not credentials or not credentials.valid:
if credentials and credentials.expired and credentials.refresh_token:
credentials.refresh(Request())
else:
flow = InstalledAppFlow.from_client_secrets_file("credentials.json", SCOPES)
credentials = flow.run_local_server(port=0)
with open("token.json", "w") as token:
token.write(credentials.to_json())
try:
service = build('sheets', 'v4', credentials=credentials)
sheets = service.spreadsheets()
result = sheets.values().get(spreadsheetId=SPREADSHEET_ID, range="testlist!A1:C6").execute()
values = result.get("values", [])
for row in values:
print(row)
except HttpError as error:
print(error)
if __name__ == "__main__":
main()
I searched all over the internet and didn't find a solution, please help. I did everything according to the documentation, but still the error.
Tried everything. Help me please.