0

When I try to connect, I get an error:

<HttpError 403 when requesting returned "Request had insufficient authentication scopes."

enter image description here

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.

Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
  • you are unable to type the 3 short lines of text in the image – rioV8 Jul 17 '23 at 23:42
  • Not that familiar with python myself, but the error is telling you you are missing the API key. You might be able to get what you need from [here](https://stackoverflow.com/questions/44899425/read-a-google-sheet-with-python-using-api-key-instead-of-oauth-client-id-python) – Miguel Rivera Rios Jul 19 '23 at 00:47

0 Answers0