首页 > 解决方案 > KeyError:“access_token”和 google.auth.exceptions.RefreshError

问题描述

对 Python 非常陌生。我正在关注本教程,它使用 Google Drive 和 Sheets API,但在运行文件时出现以下错误:

C:\Users\[ME]\AppData\Local\Programs\Python\Python39\python.exe C:/Users/[ME]/Documents/.../main.py
Traceback (most recent call last):
  File "C:\Users\[ME]\AppData\Local\Programs\Python\Python39\lib\site-packages\google\oauth2\_client.py", line 156, in jwt_grant
    access_token = response_data["access_token"]
KeyError: 'access_token'

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
  File "C:/Users/[ME]/Documents/.../main.py", line 13, in <module>
    sheet = client.open('ABC').sheet1
  File "C:\Users\[ME]\AppData\Local\Programs\Python\Python39\lib\site-packages\gspread\client.py", line 119, in open
    self.list_spreadsheet_files(title),
  File "C:\Users\[ME]\AppData\Local\Programs\Python\Python39\lib\site-packages\gspread\client.py", line 95, in list_spreadsheet_files
    res = self.request('get', url, params=params).json()
  File "C:\Users\[ME]\AppData\Local\Programs\Python\Python39\lib\site-packages\gspread\client.py", line 61, in request
    response = getattr(self.session, method)(
  File "C:\Users\[ME]\AppData\Local\Programs\Python\Python39\lib\site-packages\requests\sessions.py", line 555, in get
    return self.request('GET', url, **kwargs)
  File "C:\Users\[ME]\AppData\Local\Programs\Python\Python39\lib\site-packages\google\auth\transport\requests.py", line 460, in request
    self.credentials.before_request(auth_request, method, url, request_headers)
  File "C:\Users\[ME]\AppData\Local\Programs\Python\Python39\lib\site-packages\google\auth\credentials.py", line 133, in before_request
    self.refresh(request)
  File "C:\Users\[ME]\AppData\Local\Programs\Python\Python39\lib\site-packages\google\oauth2\service_account.py", line 361, in refresh
    access_token, expiry, _ = _client.jwt_grant(request, self._token_uri, assertion)
  File "C:\Users\[ME]\AppData\Local\Programs\Python\Python39\lib\site-packages\google\oauth2\_client.py", line 159, in jwt_grant
    six.raise_from(new_exc, caught_exc)
  File "<string>", line 3, in raise_from
google.auth.exceptions.RefreshError: ('No access token in response.', {'id_token': '[string of letters and numbers]'})

文件代码如下:

import gspread
from oauth2client.service_account import ServiceAccountCredentials

scope = ['https://www.googlapis.com/feeds',
         'https://www.googlapis.com/auth/spreadsheets',
         'https://www.googleapis.com/auth/drive.file',
         'https://www.googlapis.com/auth/drive']

creds = ServiceAccountCredentials.from_json_keyfile_name('client_secret.json', scopes=scope)

client = gspread.authorize(creds)

sheet = client.open('ABC').sheet1

stuff = sheet.get_all_records()
print(stuff)

使用sheet = client.open_by_key('ABC').sheet1而不是sheet = client.open('ABC').sheet1返回相同的错误。使用默认范围 ( scope = ['https://www.googleapis.com/auth/spreadsheets', 'https://www.googleapis.com/auth/drive']) 会产生相同的错误。

我的client_secret.json文件没有access_token字段。这是问题吗?

我应该使用其他东西代替from oauth2client.service_account import ServiceAccountCredentials吗?

Python 3.9.0 / PyCharm 2020.2.3(社区版)

标签: pythonpython-3.xgoogle-apipycharmgoogle-oauth

解决方案


我想我可能已经解决了我自己的问题......

找到关于Authentication的 gspread 文档。在我的计算机中搜索gspread文件夹,将client_secret.json文件移动到那里,并使用以下代码:

import gspread

scope = ['https://www.googlapis.com/feeds',
         'https://www.googlapis.com/auth/spreadsheets',
         'https://www.googleapis.com/auth/drive.file',
         'https://www.googlapis.com/auth/drive']

gc = gspread.service_account(filename="client_secret.json")

sheet = gc.open("ABC").sheet1

data = sheet.get_all_records()
print(data)

但是为什么原始代码不起作用?from oauth2client.service_account import ServiceAccountCredentials不再工作了吗?



推荐阅读