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.
Advertisement
Answer
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.