python - 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(社区版)
解决方案
我想我可能已经解决了我自己的问题......
找到关于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
不再工作了吗?
推荐阅读
- r - R-参数中的检查点函数错误意味着不同的行数:1、38、37
- python-2.7 - 删除最后一个窗口配置
- java - java.security.cert.CertPathValidatorException:证书链接错误
- python - 如何获取锚标签内的元素?
- python-3.x - 按重量从列表中获取随机响应
- javascript - D3 Canvas Network 不出现,除非手动刷新
- angular - 在 Angular 6 和 RxJS 中未发送 Rest-api 调用
- c# - Windows 10 中缺少 makecert.exe,如何获取和使用它
- javascript - 循环最多 x 个数字,或更少
- css - 使用 CSS 突出显示值