caproki
caproki

Reputation: 410

Google SheetsAPI: ValueError: Client secrets must be for a web or installed app

Very similar to this question: ValueError: Client secrets must be for a web or installed app but with a twist: I'm trying to do this through a Google Cloud Virtual Machine.

Recently, the Out-Of-Band (OOB) flow stopped working for me (it seems the reason may lie here: oob-migration. Until then, I was able to easily run the Google Sheets API on the Virtual Machine to both read/write on Google Sheet files

Now, I'm trying to follow this Python quickstart for google sheets which is almost identical to the code I already had, under the "Configure the sample" section.

My code on Python right now is:

        scopes = ['https://www.googleapis.com/auth/spreadsheets']
        creds = None       
        # The file token.json stores the user's access and refresh tokens, and is
        # created automatically when the authorization flow completes for the first
        # time.
        if os.path.exists('token.json'):
            creds = Credentials.from_authorized_user_file('token.json', scopes)
        # If there are no (valid) credentials available, let the user log in.
        if not creds or not creds.valid:
            if creds and creds.expired and creds.refresh_token:
                creds.refresh(Request())
            else:
                flow = InstalledAppFlow.from_client_secrets_file(
                    credentials_path, scopes)
                creds = flow.run_local_server(port=0)
            # Save the credentials for the next run
            with open('token.json', 'w') as token:
                token.write(creds.to_json())
        #Store creds in object
        my_creds = creds
        #Create service
        build('sheets', 'v4', credentials=my_creds)

But every time Ì get this error:

ValueError: Client secrets must be for a web or installed app.

For the record, I did create the credentials under "OAuth 2.0 Client IDs" on Google Cloud, and the application type is "Web application". If that's not the type, I don't know which one it should be.

Thank you so much for your help, really appreciated.

Upvotes: 1

Views: 1736

Answers (1)

Linda Lawton - DaImTo
Linda Lawton - DaImTo

Reputation: 117261

The code you are are using was designed for an installed. Which is exactly what your error message is saying. The QuickStart clearly states Click Application type > Desktop app.

While i agree the error message states installed or web, i am not sure that code can be used for a web application.

Client secrets must be for a web or installed app.

Open the file denoted by credentials_path the file should have the following format.

credentials.json

{
  "installed": {
    "client_id": "[redacted]",
    "project_id": "daimto-tutorials-101",
    "auth_uri": "https://accounts.google.com/o/oauth2/auth",
    "token_uri": "https://oauth2.googleapis.com/token",
    "auth_provider_x509_cert_url": "https://www.googleapis.com/oauth2/v1/certs",
    "client_secret": "[redacted]",
    "redirect_uris": [
      "http://localhost"
    ]
  }
}

Points to check.

  1. it must say "installed"
  2. redirect_uris must not include anything like urn:ietf:wg:oauth:2.0:oob

Here is a of video which will show you how to create the proper credentials file for use with that code. This should be done though Google cloud console

Note: I highly doubt that this issue is due to oob, you would have a different error message if it was.

update Web app works.

I was able to test this with using a web app credentials. the only change i had to make was to denote the port i wanted the code to run on in order to get a static port I needed to add a redirect uri to the developer console project.

I made no other changes to the standard quickstart.

flow = InstalledAppFlow.from_client_secrets_file(
            CREDENTIALS_FILE_PATH, SCOPES)
        creds = flow.run_local_server(port=53911)

Upvotes: 2

Related Questions