Skip to content
Advertisement

Can I using Google Sheet API only with API Key or using Client ID and Client secret, but without client_secret.json?

In this provided Python code in this quickstart, it using credentials.json such in this line:

flow = InstalledAppFlow.from_client_secrets_file('credentials.json', SCOPES)

I have enabled the Sheet and Drive API. I created credentials from the APIs & Services menu, then on the Credentials tab, click on the CREATE CREDENTIALS button, then click on the OAuth client ID, and on the Application type I selected the Desktop app, then I download its JSON file, then set the credentials.json file to the right path, like this:

flow = InstalledAppFlow.from_client_secrets_file('client_secret_274233513361-l7vpffd7g9oree4tg5tledq9keqrevk3.apps.googleusercontent.com.json', SCOPES)

Then, when I run the quickstart code above, it shows a new browser pop-up that requires me to log in. After successfully login, yea, I can run the Python code successfully without any error.

But I don’t want a new pop-up that requires me to log in first.

So my question: Can I use Google Sheet API only with my credentials Client ID and Client secret enter image description here

or only with API Key

enter image description here

If we can do it, how to do it? is there any documentation on how to achieve that?

Advertisement

Answer

As @Tanaike suggests, I use the Google Service Account instead. It doesn’t require me to login into a browser pop-up anymore. I can access private and public spreadsheets, not only public sheets if using API like mentioned by @daimto.

On how to create the service account and get the JSON file you can follow the instruction on my question here.

For example to create a new spreadsheet I use this code:

from apiclient import discovery
from google.oauth2 import service_account

def create_spreadsheet(spreadsheet_title)
    scopes = [
        "https://www.googleapis.com/auth/drive",
        "https://www.googleapis.com/auth/drive.file",
        "https://www.googleapis.com/auth/spreadsheets"
        ]


    # Open the JSON file that you have downloaded from the service account, and paste it here.
    service_account_json_credentials = {
          "type": "service_account",
          "project_id": "odoo-spreadsheet-371808",
          "private_key_id": "321ee08baexample6ae125918examplea69ddbec64",
          "private_key": "-----BEGIN PRIVATE KEY-----nMIIEvQIBADANBgkqhkiG9w0BAQEFAASCBKcwggSjAgEAAoIBAQDIzR5iPZEQqjDhnFJlSfQOd0wJfwbgHt+V3gosd2zT5FV8gBHfZXqEz0Nd+nFqOSebI2XxwpF99tOXon+PR+tetOGkY5wP8LE1GVZstyyDa/xQp7eE82Yexample9mAsDwpgEt9MQnxGE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LWxT7fCzIujbsH7dZvZGd/iSV7Pqm3y5+tfYGWmbkexampleaL9UppMkrkjvxPh95sM6h4ahM1dnCaHHVAqvoP9QnxnZ1CKGuIamqxEUpes7VnFZwN8hZYfLWBug5WumlgHLrPkI6WBjF/u1YuPA9PAoGAYg7GUkefdLyc8O5u20dmn8AzGEzsWlJh2cEqJ1iYmOS42s9jw7fxQUbIH9K1UWORIShdQTxZilRDGvfbSqWH5nLSjr8j0iR7WlvB9KxrSSAnKtt+vkdJz8YqL6/XzogG/C+ruh8f5T8wPNPh6tDtcjn2Z/gZ6cOD8dVrDTFQebdrMI=n-----END PRIVATE KEY-----n",
          "client_email": "odoo12@odoo-spreadsheet-371808.iam.gserviceaccount.com",
          "client_id": "101569example94608",
          "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_x509_cert_url": "https://www.googleapis.com/robot/v1/metadata/x509/odoo12%40odoo-spreadsheet-371808.iam.gserviceaccount.com"
        }

    credentials = service_account.Credentials.from_service_account_info(service_account_json_credentials, scopes=scopes)
    spreadsheet_service = discovery.build('sheets', 'v4', credentials=credentials)

    spreadsheet = {
        'properties': {
            'title': spreadsheet_title
        }
        }
    creation_response = spreadsheet_service.spreadsheets().create(body=spreadsheet, fields='spreadsheetId').execute()

    spreadsheet_id = creation_response.get('spreadsheetId')
    return spreadsheet_id
    
create_spreadsheet('a new apreadsheet')

Hope this can help someone who facing a similar or wants to achieve the same things. Again, thank you to @Tanaike and everyone that has been taking your time to shed some light on the case I’m facing.

User contributions licensed under: CC BY-SA
2 People found this is helpful
Advertisement