Google Sheets API v4 in Python: Service Accounts, Batch Updates & Rate Limits

2017-07-08 16:31:00+00:00

There are two ways to access google sheets via API

Google Service Account

A service account is similar to google account, it can receive a document share.

An ordinary Google user account can share his/her document to this service account.

A web developer can create a service account at this page: https://console.developers.google.com/iam-admin/serviceaccounts/. That web page will automatically download a google application credentials for you.

Set an environment variable named GOOGLE_APPLICATION_CREDENTIALS to the path of that downloaded credentials file.

Write your main Python script like this:

from apiclient import discovery

service = discovery.build('sheets', 'v4')
spreadsheetId = 'the-spreadsheet-id'
rangeName = 'The spreadsheet tab name!B4:B100'
result = service.spreadsheets().values().get(spreadsheetId=spreadsheetId, range=rangeName).execute()
values = result.get('values', [])
if not values:
    print('No data found.')
else:
    for row in values:
        print('%s' % (row[0],))

A spreadsheet id is the string you can find at the end of a Google sheet URL: https://docs.google.com/spreadsheets/d/the-spreadsheet-id/

OAuth2

OAuth2 requires interaction with a human user. There are so many documentations about OAuth2. I won't write another article about OAuth2. I will write about how to remove user interactions, so you can access Google sheets via cron job as an ordinary Google user account, not as a service account.

You will need PhantomJS installed in your system, and write a JavaScript to emulate user interaction like this:

/**
  * This script will write oauth2 authorization code to the standard output
  * call this script like this:
  * phantomjs authorize_google.js authorize_url username password
  */
var system = require('system');
var fs = require('fs');
var page = require('webpage').create();
page.onError = function (msg, trace) {
  var msgStack = ['ERROR: ' + msg];
  if (trace && trace.length) {
      msgStack.push('TRACE:');
      trace.forEach(function (t) {
          msgStack.push(' -> ' + t.file + ': ' + t.line + (t.function ? ' (in function "' + t.function + '")' : ''));
      });
  }
  console.error(msgStack.join('\n'));
};
page.viewportSize = {width: 1024, height: 768};
page.open(system.args[1], function (status) {
    if (status !== 'success') {
        phantom.exit();
    }
    var jqueryJs = "https://ajax.googleapis.com/ajax/libs/jquery/2.1.4/jquery.min.js";
    setTimeout(function () {
        page.includeJs(jqueryJs, function () {
            page.evaluate(function (username) {
                $('#Email').val(username);
                $('#next').click();
            }, system.args[2]);
        });
    }, 0);
    setTimeout(function () {
        page.includeJs(jqueryJs, function () {
            page.evaluate(function (password) {
                $('#Passwd').val(password);
                $('#signIn').click();
            }, system.args[3]);
        });
    }, 20000);
    setTimeout(function () {
        page.includeJs(jqueryJs, function () {
            page.evaluate(function () {
                $('#submit_approve_access').click();
            });
        });
    }, 40000);
    setTimeout(function () {
        page.includeJs(jqueryJs, function () {
            var code = page.evaluate(function () {
                return $('#code').val();
            });
            fs.write("/dev/stdout", code, "w");
            phantom.exit();
        });
    }, 80000);
});

Name that PhantomJS script to authorize_google.js or anything else you like.

Install the Google API client:

pip install --upgrade google-api-python-client

You will have this Python module installed:

oauth2client/tools.py

Make a copy of that module, name it to something like google_tools.py.

Modify the run_flow function like this:

@_helpers.positional(3)
def run_flow(flow, storage, flags=None, http=None, username=None, password=None):
    if flags is None:
        flags = argparser.parse_args()
        logging.getLogger().setLevel(getattr(logging, flags.logging_level))
        
    if not flags.noauth_local_webserver:
        success = False
        port_number = 0
        for port in flags.auth_host_port:
            port_number = port
            try:
                httpd = ClientRedirectServer((flags.auth_host_name, port), ClientRedirectHandler)
            except socket.error:
                pass
            else:
                success = True
                break
        flags.noauth_local_webserver = not success
        if not success:
            print(_FAILED_START_MESSAGE)
            if not flags.noauth_local_webserver:
                oauth_callback = 'http://{host}:{port}/'.format(host=flags.auth_host_name, port=port_number)
            else:
                oauth_callback = client.OOB_CALLBACK_URN
                flow.redirect_uri = oauth_callback
                authorize_url = flow.step1_get_authorize_url()
                if not flags.noauth_local_webserver:
                    import webbrowser
                    webbrowser.open(authorize_url, new=1, autoraise=True)
                    print(_BROWSER_OPENED_MESSAGE.format(address=authorize_url))
        else:
            if not flags.phantomjs:
                print(_GO_TO_LINK_MESSAGE.format(address=authorize_url))
                code = None
                if not flags.noauth_local_webserver:
                    httpd.handle_request()
                        if 'error' in httpd.query_params:
                            sys.exit('Authentication request was rejected.')
                            if 'code' in httpd.query_params:
                                code = httpd.query_params['code']
                            else:
                                print('Failed to find "code" in the query parameters of the redirect.')
                                sys.exit('Try running with --noauth_local_webserver.')
                        else:
                            if flags.phantomjs:
                                print(authorize_url)
                                process_out = subprocess.run(['phantomjs', 'authorize_google.js', authorize_url, username, password], stdout=subprocess.PIPE)
                                code = str(process_out.stdout.decode('UTF-8'))
                            else:
                                code = input('Enter verification code: ').strip()
                                try:
                                    credential = flow.step2_exchange(code, http=http)
                                except client.FlowExchangeError as e:
                                    sys.exit('Authentication has failed: {0}'.format(e))
                                storage.put(credential)
                                credential.set_store(storage)
                                print('Authentication successful.')
                                return credential

You can see that the custom run_flow function creates a subprocess to execute our authorize_google.js PhantomJS script. In your main Python script, instead of importing the default tools module, you will import your custom google_tools module.

Write your main Python script like this:

from __future__ import print_function 
import httplib2 
import os  
from apiclient import discovery 
from oauth2client import client 
#from oauth2client import tools 
from core import google_tools as tools 
from oauth2client.file import Storage  
try:     
    import argparse     
    flags = argparse.ArgumentParser(parents=[tools.argparser]).parse_args() 
except ImportError:     
    flags = None  
    
# If modifying these scopes, delete your previously saved credentials 
# at ~/.credentials/sheets.googleapis.com-python-quickstart.json 
SCOPES = 'https://www.googleapis.com/auth/spreadsheets.readonly' 
CLIENT_SECRET_FILE = 'google_client_secret.json' 
APPLICATION_NAME = 'Labster Image Manager'   
def get_credentials():     
    """
    Gets valid user credentials from storage.      
    If nothing has been stored, or if the stored credentials are invalid,     
    the OAuth2 flow is completed to obtain the new credentials.      
    Returns: Credentials, the obtained credential.     
    """     
    home_dir = os.path.expanduser('~')     
    credential_dir = os.path.join(home_dir, '.credentials')     
    if not os.path.exists(credential_dir):         
        os.makedirs(credential_dir)     
        credential_path = os.path.join(credential_dir, 'sheets.googleapis.com-image-manager.json')      
        store = Storage(credential_path)     
        credentials = store.get()     
        if not credentials or credentials.invalid:         
            flow = client.flow_from_clientsecrets(CLIENT_SECRET_FILE, SCOPES)         
            flow.user_agent = APPLICATION_NAME         
            if flags:             
                credentials = tools.run_flow(flow, store, flags, username='a-google-email', password='the-password-of-that-google-account')         
            else: # Needed only for compatibility with Python 2.6             
                credentials = tools.run(flow, store)         
                print('Storing credentials to ' + credential_path)     
        return credentials  
        
def main():     
    """
    Shows basic usage of the Sheets API.      
    Creates a Sheets API service object and prints the names and majors of     
    students in a sample spreadsheet: https://docs.google.com/spreadsheets/d/1BxiMVs0XRA5nFMdKvBdBZjgmUUqptlbs74OgvE2upms/edit     
    """     
    credentials = get_credentials()     
    http = credentials.authorize(httplib2.Http())     
    discoveryUrl = ('https://sheets.googleapis.com/$discovery/rest?version=v4')     
    service = discovery.build('sheets', 'v4', http=http, discoveryServiceUrl=discoveryUrl)      
    spreadsheetId = '1BxiMVs0XRA5nFMdKvBdBZjgmUUqptlbs74OgvE2upms'     
    rangeName = 'Class Data!A2:E'     
    result = service.spreadsheets().values().get(spreadsheetId=spreadsheetId, range=rangeName).execute()     
    values = result.get('values', [])      
    if not values:         
        print('No data found.')     
    else:         
        #print('Name, Major:')         
        print('Finished:')         
    for row in values:             
        # Print columns A and E, which correspond to indices 0 and 4.             
        print('%s, %s' % (row[0], row[4]))   
        
        
if __name__ == '__main__':     
    main()

Actually, that main Python script is almost an exact copy of the example provided by Google, except we replace the import tools with our import google_tools.

Service-Account OAuth2 & Batch-Update Rate-Limit Handling for Google Sheets API v4

Coalesce cell updates into single `spreadsheets.values.batchUpdate` calls with exponential backoff so automated financial reporting pipelines never hit per-minute quota errors.

Modern Production Pattern (2026): Keyless Workload Identity & Single-Call batchUpdate

In production workloads architected by a Google Cloud Certified GCP Professional Cloud Architect, GCP Professional Data Engineer, and Backend Engineer, avoid downloading long-lived JSON private keys whenever running on Cloud Run, GKE, or GitHub Actions. Use Application Default Credentials (google.auth.default) via Workload Identity Federation and coalesce thousands of cell writes into one spreadsheets.values.batchUpdate request:

# sheets_sync/batch_writer.py
import time
import random
import google.auth
from googleapiclient.discovery import build
from googleapiclient.errors import HttpError

SCOPES = ["https://www.googleapis.com/auth/spreadsheets"]

def write_financial_summary_batch(spreadsheet_id: str, tab_ranges: dict[str, list[list]]) -> int:
    creds, _ = google.auth.default(scopes=SCOPES)
    service = build("sheets", "v4", credentials=creds, cache_discovery=False)
    body = {
        "valueInputOption": "USER_ENTERED",
        "data": [
            {"range": rng, "values": rows}
            for rng, rows in tab_ranges.items()
        ],
    }
    for attempt in range(5):
        try:
            resp = (
                service.spreadsheets()
                .values()
                .batchUpdate(spreadsheetId=spreadsheet_id, body=body)
                .execute()
            )
            return int(resp.get("totalUpdatedCells", 0))
        except HttpError as err:
            if err.resp.status in (429, 500, 503):
                time.sleep((2 ** attempt) + random.uniform(0.2, 0.9))
                continue
            raise
    raise RuntimeError("Google Sheets API v4 quota retry ceiling exceeded")

Frequently Asked Questions — Google Sheets API v4 in Python

Should a backend server use a Google Service Account or OAuth 2.0 User Consent for Google Sheets API?

Use a Google Cloud Service Account (ideally attached via keyless Workload Identity on Cloud Run/GKE) for unattended server-to-server pipelines, and share the target Google Sheet directly with the service account's @project-id.iam.gserviceaccount.com identifier. Use 3-legged OAuth 2.0 only when acting on behalf of individual end users' personal Drive files.

How do you avoid HTTP 429 RATE_LIMIT_EXCEEDED errors on Google Sheets API v4?

Google Sheets API v4 enforces a per-minute per-project and per-user read/write request quota (typically 60–300 requests/minute). Replace row-by-row loops with a single spreadsheets.values.batchGet or spreadsheets.values.batchUpdate call, and wrap execution in truncated exponential backoff with random jitter.

When should we replace Google Sheets sync with BigQuery Connected Sheets?

Once operational exports exceed 100,000 rows or multiple analysts run concurrent pivot formulas that stall spreadsheet recalculation, stream the data into BigQuery and expose live governed views to finance teams via BigQuery Connected Sheets—or book a 1-Day GCP & Python Engineering Sprint to automate the pipeline.


About the Author — Hire Aldian Fazrihady (Cloud Engineer, GCP Professional Cloud Architect, GCP Professional Data Engineer, Backend, AI & Full Stack Engineer)

Aldian Fazrihady is a Top-Rated Senior Cloud Engineer, GCP Professional Cloud Architect (Google Cloud Certified), GCP Professional Data Engineer (Google Cloud Certified), Backend Engineer (Python, Django, FastAPI, Go, PostgreSQL), AI Application Engineer (LLMs, RAG, MCP, Google ADK, LangGraph), and Full Stack Engineer (React, Next.js, TypeScript) with 25+ years of production software engineering experience.

Hire Aldian Fazrihady on Upwork | Book 1-Day Programming Service ($100 USD / Rp 1.000.000) | View Live AI Portfolio | Send a Direct Message

Related Engineering Articles

Engineering Role Pillar Guides & Category Hubs