2

Trying to access my spreadsheet under my organizational account. I have run the Google API Python Client Quickstart and substituted the appropriate pieces to use my spreadsheet and everything worked. When I change things up to use gspread I receive a Token invalid - AuthSub token has wrong scope.

I was prompted to approve access, the error occurs when c.open_by_key('key_here') is called.

import os
import gspread

from oauth2client import client
from oauth2client import tools
from oauth2client.file import Storage

try:
    import argparse
    flags = argparse.ArgumentParser(parents=[tools.argparser]).parse_args()
except ImportError:
    flags = None

SCOPES = ['https://www.googleapis.com/auth/spreadsheets']
CLIENT_SECRET_FILE = 'client_secret.json'
APPLICATION_NAME = 'ENTMQ2'

def get_credentials():
    home_dir = os.path.expanduser('~')
    credential_dir = os.path.join(home_dir, '.credentials')
    if not os.path.exists(credential_dir):
        os.makedirs(credential_dir)
    credential_path = os.path.join(credential_dir,
                                   'sheets.googleapis.com-python-entmq.json')

    store = Storage(credential_path)
    credentials = store.get()
    if not credentials or credentials.invalid:
        flow = client.flow_from_clientsecrets(CLIENT_SECRET_FILE, SCOPES)
        flow.user_agent = APPLICATION_NAME
        if flags:
            credentials = tools.run_flow(flow, store, flags)
        else:
            credentials = tools.run(flow, store)
        print('Storing credentials to ' + credential_path)
    return credentials

credentials = get_credentials()
c = gspread.authorize(credentials)

spreadsheet = c.open_by_key('key_parsed_from_spreadsheet_url')

Again, the quickstart code works. I've enabled apis for drive and spreadsheet and I'm using the same OAuth Client ID. I'm hoping someone here has some insight. The examples for gspread focus on Service Accounts which are generally not available with our organizational account.

The error I receive:

Traceback (most recent call last):
  File "test_cred.py", line 42, in <module>
    spreadsheet = c.open_by_key('key_parsed_from_spreadsheet_url')
  File "build/bdist.linux-x86_64/egg/gspread/client.py", line 102, in open_by_key
  File "build/bdist.linux-x86_64/egg/gspread/client.py", line 165, in get_spreadsheets_feed
  File "build/bdist.linux-x86_64/egg/gspread/httpsession.py", line 76, in get
  File "build/bdist.linux-x86_64/egg/gspread/httpsession.py", line 72, in request
gspread.exceptions.HTTPError: 401: <HTML>
<HEAD>
<TITLE>Token invalid - AuthSub token has wrong scope</TITLE>
</HEAD>
<BODY BGCOLOR="#FFFFFF" TEXT="#000000">
<H1>Token invalid - AuthSub token has wrong scope</H1>
<H2>Error 401</H2>
</BODY>
</HTML>
Eric
  • 636
  • 2
  • 9
  • 23

0 Answers0