Error 403 The request is missing a valid API key - Unable to write data into Google Sheet

 

I want to write data into Google Sheet using Python script. Most importantly, I want to auotemate it by using Google Cloud Function. However, even I could run the code successfully using my local computer environment by pointing to the path of the credentials, I receive error when running it in Google Cloud Function. It is sayig that 'The request is missing a valid API key. Your application is authenticating by using local Application Default Credentials. The sheets.googleapis.com API requires a quota project, which is not set by default'. You could review the below script I use. Note that I am using a service account to get access to Google Sheet, not OAuth Client ID. Any thoughts?

 

import pygsheets
import pandas as pd

import google.auth
from googleapiclient.discovery import build
from oauth2client.service_account import ServiceAccountCredentials


def gen_google_sheet():
    creds, project = google.auth.default(scopes=scopes)
    service = build('sheets', 'v4', credentials=creds)
   
    spreadsheet_id = 'xxx'
    range_name = 'Sheet1A1:B2'
 
   
    #Read data
    result=service.spreadsheets().values().get(
         spreadsheetId=spreadsheet_id,
         range=range_name
        ).execute()
    values=result.get('values',[])

    return print('Done')
Solved Solved
2 1 2,829
1 ACCEPTED SOLUTION

Have the exact same requirement, and the same error. Its been a year and i still cant find a way to do it. Was hoping the integration would be easier with GCP - but thats definitely not the case! Think its time to abandon it.

View solution in original post

1 REPLY 1

Have the exact same requirement, and the same error. Its been a year and i still cant find a way to do it. Was hoping the integration would be easier with GCP - but thats definitely not the case! Think its time to abandon it.

Top Labels in this Space