'How do I read Google API credentials from a TOML file with Python?

I'm trying to pull some data using the Google Sheets Api. This is the beginning of the code:


# Import the python libraries.
import gspread
from oauth2client.service_account import ServiceAccountCredentials
from pathlib import Path
import os
import json


# Get JSON_DATA from the build environment.
jsondict = json.loads(os.environ['JSON_DATA'])

# Use creds to create a client to interact with the Google Drive API
scope = ['https://spreadsheets.google.com/feeds','https://www.googleapis.com/auth/drive']
creds = ServiceAccountCredentials.from_json_keyfile_dict(jsondict, scope)
client = gspread.authorize(creds)

# Open the Google Sheet by ID.
sheet1 = client.open_by_key("somekey").sheet1

# Extract all of the records for each row.
sheetdata1 = sheet1.get_all_records()

In the tutorial I am referring to, this is what the author says about the JSON_DATA object:

Note: the ‘JSON_DATA’ variable in the python code is a Netlify build environment variable that I set with JSON format Google API credential information to keep my secret stuff out of the script.

My netlify.toml, which contains the build environment variable has this:

[build]
command = "hugo"
publish = "public"
[build.environment]
HUGO_VERSION = "0.80.0"

[context]
[context.branch-deploy]
command = "hugo -F -b $DEPLOY_PRIME_URL"
[context.deploy-preview]
command = "hugo -F -b $DEPLOY_PRIME_URL"
[context.production]
[context.production.environment]
HUGO_ENV = "production"

I know that to include my credentials downloaded from Google (in a JSON file), I have to put this in the netlify.toml:

[installed]
client_id = "something.apps.googleusercontent.com"
project_id = "someiD"
auth_uri = "https://accounts.google.com/o/oauth2/auth"
token_uri = "whatevergoeshere"
client_secret = "somesecret"
redirect_uris = [ "something", "http://localhost" ]

but how do I read in these credentials for the Python code? That line seems to indicated that it wants a JSON file only.



Solution 1:[1]

Okay, so you need to copy paste the contents of the json key file in the actual environment variable itself:

JSON_DATA = '{"key":"secret"}'

Not the most straight forward way, but it works.

In order to maintain sanity I would use the web interface to work with those variables.

https://docs.netlify.com/configure-builds/environment-variables/

The first line parses whatever is in the environment variable to a dict. There's no need in this case to parse the toml itself, as it has already been parsed for you.

Solution 2:[2]

The JSON_DATA object simply includes more information than the credentials object received from Google. But the methods called that require the credentials from Google will simply ignore the additional parameters that they don't need. So as long as you keep the names ob the credentials objects the same, methods such as ServiceAccountCredentials.from_json_keyfile_dict() will simply ignore those it doesn't need.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1
Solution 2 Aerials