Config
- Python 3.7
- google-auth 1.11.0
- google-auth-oauthlib 0.4.1
- Google Service Account credentials
- GSheet shared with ServiceAccount
- GSheet exposed as Webapp
- Gsheet has a doGet method which reads a sheet and converts to CSV and returns it
- Works fine when you hit it directly in the browser
- Doesnt work when using python
Can see the call from python hitting the webapp - can see the webapp processing. Getting a response returned to the client which states -
b'<!DOCTYPE html><html><head><link rel="shortcut icon"
href="//ssl.gstatic.com/docs/script/images/favicon.ico"><title>Error</title><style
type="text/css">body {background-color: #fff; margin: 0; padding: 0;}.errorMessage {font-
family: Arial,sans-serif; font-size: 12pt; font-weight: bold; line-height: 150%; padding-top:
25px;}</style></head><body style="margin:20px"><div><img alt="Google Apps Script"
src="//ssl.gstatic.com/docs/script/images/logo.png"></div><div style="text-
align:center;font-family:monospace;margin:50px auto 0;max-width:600px">We're sorry, a
server error occurred. Please wait a bit and try again.</div></body></html>'
No errors in logs.
Python code doing the work:-
from __future__ import print_function
from google.oauth2 import service_account
from google.auth.transport.urllib3 import AuthorizedHttp
SCOPES = ['https://www.googleapis.com/auth/spreadsheets',
'https://www.googleapis.com/auth/drive',
'https://www.googleapis.com/auth/drive.readonly']
credentials = service_account.Credentials.from_service_account_file(
'service_account.json', scopes=SCOPES)
def main():
try:
authed_http = AuthorizedHttp(credentials)
response = authed_http.request(
'GET', "https://script.google.com/macros/s/AKfycbzmr5-g2ZIlsGFL5SDYdCYEKmhyqH_-
QcAhFeBnfN0_D291kRA/exec")
print(response._body)
except BaseException as err_base2:
print(err_base2)
if __name__ == '__main__':
main()
Is this approach supported. Feel like i'm missing something obvious.
Service Account Role Permissions to GSheet Project
Service Account Permissions to GSheet
Put some logging into the requests but it doesnt tell us anymore. DEBUG:google.auth.transport.urllib3:Making request: POST https://oauth2.googleapis.com/token DEBUG:urllib3.connectionpool:Starting new HTTPS connection (1): oauth2.googleapis.com:443 DEBUG:urllib3.connectionpool:https://oauth2.googleapis.com:443 "POST /token HTTP/1.1" 200 None DEBUG:urllib3.connectionpool:Starting new HTTPS connection (1): script.google.com:443 DEBUG:urllib3.connectionpool:https://script.google.com:443 "GET /macros/s/AKfycbzmr5-g2ZIlsGFL5SDYdCYEKmhyqH_-QcAhFeBnfN0_D291kRA/exec HTTP/1.1" 500 None