Tuesday, 4 May 2021

Create Sheets API Authentication in Cloud Function with Python

I'm hoping to use the Google Sheets API in a cloud function, which will run from my account's default service account, and I'm working in Python. However, I've only ever authenticated the Sheets library locally, using this bit of code:

import os.path
from googleapiclient.discovery import build
from google_auth_oauthlib.flow import InstalledAppFlow
from google.auth.transport.requests import Request
from google.oauth2.credentials import Credentials    

def gen_creds(path_to_secret: str, rw_vs_ro: str):
    """
    Generate the needed credentials to work with the Sheets v4 API based on your secret
    json credentials file.

    :param path_to_secret: The file path to your credentials json file
    :param rw_vs_ro: A string, 'r_o' or 'r_w', representing whether creds should be readonly or readwrite
    :return: The built service variable
    """
    if rw_vs_ro == 'r_o':
        scopes = ['https://www.googleapis.com/auth/spreadsheets.readonly']
        creds_nm = 'readonly_token.json'
    else:
        scopes = ['https://www.googleapis.com/auth/spreadsheets']
        creds_nm = 'readwrite_token.json'

    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(creds_nm):
        creds = Credentials.from_authorized_user_file(creds_nm, 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(
                path_to_secret, scopes)
            creds = flow.run_local_server(port=0)
        # Save the credentials for the next run
        with open(creds_nm, 'w') as token:
            token.write(creds.to_json())

    return build('sheets', 'v4', credentials=creds)

And I'm not entirely sure how to translate this to something that a cloud function would understand, as the cloud function won't be running as me, and lacks the same type of os path that I have access to locally. Would appreciate any insight into what the translation process looks like here--I was only able to find examples in JS, which wasn't perfect for what I was going for. Then, I would love to understand how to actually implement this code in a cloud function in GCP. Thanks!



from Create Sheets API Authentication in Cloud Function with Python

No comments:

Post a Comment