尝试打开电子表格时,google.oauth2“没有访问令牌响应”错误

问题描述

我正在尝试通过gspread打开Goog​​le电子表格,但是我的代码因错误而失败:

Traceback (most recent call last):
  File "/home/memcpy/.local/lib/python3.6/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 "email_sender.py",line 20,in <module>
    fetch_users()
  File "email_sender.py",line 16,in fetch_users
    sheet = client.open('some')

  some more tracebacks....

  File "/home/memcpy/.local/lib/python3.6/site-packages/google/auth/transport/requests.py",line 444,in request
    self.credentials.before_request(auth_request,method,url,request_headers)
  File "/home/memcpy/.local/lib/python3.6/site-packages/google/auth/credentials.py",line 133,in before_request
    self.refresh(request)
  File "/home/memcpy/.local/lib/python3.6/site-packages/google/oauth2/service_account.py",line 359,in refresh
    access_token,expiry,_ = _client.jwt_grant(request,self._token_uri,assertion)
  File "/home/memcpy/.local/lib/python3.6/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': 'there was some token so I hided it'})

我尝试通过Google搜索该错误,但未找到任何有关此错误的信息。这是我的代码:

import gspread
from google.oauth2.service_account import Credentials

scope = [
    'https://googleapis.com/auth/drive','https://googleapis.com/auth/spreadsheets'
]

creds = Credentials.from_service_account_file('creds.json',scopes=scope)
client = gspread.authorize(creds)
sheet = client.open('some')

解决方法

我遵循了此处链接的本教程:https://www.youtube.com/watch?v=cnPlKLEGR7E&list=PLdu6wGxXWFgX9XlahY9gHEMdZ7CL5CHMY&index=5&ab_channel=TechWithTim

确保将scope变量正确定义为: 范围= [“ https://spreadsheets.google.com/feeds”,'https://www.googleapis.com/auth/spreadsheets',“ https://www.googleapis.com/auth/drive.file” ,“ https://www.googleapis.com/auth/drive”]

相关问答

依赖报错 idea导入项目后依赖报错,解决方案:https://blog....
错误1:代码生成器依赖和mybatis依赖冲突 启动项目时报错如下...
错误1:gradle项目控制台输出为乱码 # 解决方案:https://bl...
错误还原:在查询的过程中,传入的workType为0时,该条件不起...
报错如下,gcc版本太低 ^ server.c:5346:31: 错误:‘struct...