在令牌中使用gspread结果无效 - AuthSub令牌的范围错误

时间:2016-10-27 17:47:12

标签: python google-api-python-client gspread

尝试访问我的组织帐户下的电子表格。我已经运行了Google API Python客户端快速入门并替换了相应的部分以使用我的电子表格,一切正常。当我改变使用gspread时,我收到令牌无效 - AuthSub令牌的范围错误。

我被提示批准访问权限,当调用c.open_by_key('key_here')时会发生错误。

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')

同样,快速入门代码有效。我为驱动器和电子表格启用了apis,并且我使用的是相同的OAuth客户端ID。我希望这里有人有一些见解。 gspread的示例主要关注服务帐户,这些帐户通常不适用于我们的组织帐户。

我收到的错误:

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>

0 个答案:

没有答案