pythongoogle-sheetsweb-applicationsgoogle-oauthservice-accounts

Correct setup for accessing a GSheet published as a webapp via a service account


I have a GSheet published as a webapp and I am trying to allow access to it from a service account

Trying to access it using the following python code and getting a 401. Following examples here

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']
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/a/<DOMAIN_NAME_REDACTED>/macros/s/AKfycbwv53GaXRCvjQMYizjqI4PsLeNMAcmCtHjRhZE9AlheIp0qE_s/exec')
    print(response)
except BaseException as err_base2:
    print(err_base2)

if __name__ == '__main__':
    main()

I think I am missing something obvious. Any ideas?


Solution

  • If my understanding is correct, how about this modification? In this case, please modify the scope as follows.

    From:

    SCOPES = ['https://www.googleapis.com/auth/spreadsheets']
    

    To:

    SCOPES = ['https://www.googleapis.com/auth/drive']
    

    and/or

    SCOPES = ['https://www.googleapis.com/auth/drive.readonly']
    

    Note:

    If this was not the result you want, I apologize.