Greetings,
I’m seeking to deploy an app to Streamlit Cloud that makes API calls to Google Sheets. While the Streamlit guide for Google Sheets is helpful, my script uses a different method to call Google Sheets, in this case pygsheets
(link) instead of gsheetsdb
. For authorizing access to Google Sheets, pygsheets
requires the Google API credentials in json format. I attempted to implement this in my app’s secret.toml
file in the format recommended in the TOML guidelines:
[[gcp_service_account]]
type = 'service_account'
project_id = 'name'
private_key_id = 'private_key_id'
private_key = '''private_key'''
client_email = 'email'
client_id = 'id'
auth_uri = 'https://accounts.google.com/o/oauth2/auth'
token_uri = 'https://oauth2.googleapis.com/token'
auth_provider_x509_cert_url = 'https://www.googleapis.com/oauth2/v1/certs'
client_x509_cert_url = 'url'
My app script for initializing the authorization to Google Sheets is:
import streamlit as st
import pygsheets
credentials = st.secrets["gcp_service_account"]
gc = pygsheets.authorize(custom_credentials=credentials)
sh0 = gc.open('users') # opens Google Sheet 'users'
wks0 = sh0[0] # opens first worksheet in spreadsheet
When loading the app, the following error is thrown, with the callback below: AttributeError: st.secrets has no attribute "before_request".
File "/home/appuser/venv/lib/python3.7/site-packages/streamlit/scriptrunner/script_runner.py", line 443, in _run_script
exec(code, module.__dict__)
File "/app/bacon_bot/Baconbot_1_6_7.py", line 26, in <module>
sh0 = gc.open('users')
File "/home/appuser/venv/lib/python3.7/site-packages/pygsheets/client.py", line 120, in open
spreadsheet = list(filter(lambda x: x['name'] == title, self.drive.spreadsheet_metadata()))[0]
File "/home/appuser/venv/lib/python3.7/site-packages/pygsheets/drive.py", line 127, in spreadsheet_metadata
q=query, pageSize=500, orderBy='recency')
File "/home/appuser/venv/lib/python3.7/site-packages/pygsheets/drive.py", line 85, in list
response = self._execute_request(self.service.files().list(**kwargs))
File "/home/appuser/venv/lib/python3.7/site-packages/pygsheets/drive.py", line 364, in _execute_request
return request.execute(num_retries=self.retries)
File "/home/appuser/venv/lib/python3.7/site-packages/googleapiclient/_helpers.py", line 131, in positional_wrapper
return wrapped(*args, **kwargs)
File "/home/appuser/venv/lib/python3.7/site-packages/googleapiclient/http.py", line 931, in execute
headers=self.headers,
File "/home/appuser/venv/lib/python3.7/site-packages/googleapiclient/http.py", line 190, in _retry_request
resp, content = http.request(uri, method, *args, **kwargs)
File "/home/appuser/venv/lib/python3.7/site-packages/google_auth_httplib2.py", line 209, in request
self.credentials.before_request(self._request, method, uri, request_headers)
File "/home/appuser/venv/lib/python3.7/site-packages/streamlit/secrets.py", line 68, in __getattr__
raise AttributeError(_missing_attr_error_message(attr_name))
Any suggestions on how to approach this?