使用Google Forms API获取表单时遇RefreshError报错求助
调用Google Forms API时出现RefreshError: No access token in response
我要给社区申请表单做个评分机器人,调用Google Forms API获取表单时碰到了RefreshError报错。
最小复现代码
# Google Credentials from google.oauth2 import service_account SCOPES = ['https://www.googleapis.com/auth/drive', 'https://www.googleapis.com/auth/forms.body'][0] SERVICE_ACCOUNT_FILE = 'key.json' credentials = service_account.Credentials.from_service_account_file(SERVICE_ACCOUNT_FILE, scopes=SCOPES) import googleapiclient.discovery forms = googleapiclient.discovery.build('forms', 'v1', credentials=credentials) form_id = '__omitted__' result = forms.forms().get(formId=form_id).execute() print(result)
报错日志
Traceback (most recent call last): File "/Library/Frameworks/Python.framework/Versions/3.11/lib/python3.11/site-packages/google/oauth2/_client.py", line 323, 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 "__omitted__", line 33, in <module> result = forms.forms().get(formId=form_id).execute() ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "/Library/Frameworks/Python.framework/Versions/3.11/lib/python3.11/site-packages/googleapiclient/_helpers.py", line 130, in positional_wrapper return wrapped(*args, **kwargs) ^^^^^^^^^^^^^^^^^^^^^^^^ File "/Library/Frameworks/Python.framework/Versions/3.11/lib/python3.11/site-packages/googleapiclient/http.py", line 923, in execute resp, content = _retry_request( ^^^^^^^^^^^^^^^ File "/Library/Frameworks/Python.framework/Versions/3.11/lib/python3.11/site-packages/googleapiclient/http.py", line 191, in _retry_request resp, content = http.request(uri, method, *args, **kwargs) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "/Library/Frameworks/Python.framework/Versions/3.11/lib/python3.11/site-packages/google_auth_httplib2.py", line 209, in request self.credentials.before_request(self._request, method, uri, request_headers) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "/Library/Frameworks/Python.framework/Versions/3.11/lib/python3.11/site-packages/google/auth/credentials.py", line 156, in before_request self.refresh(request) ^^^^^^^^^^^^^^^^^^^^^ File "/Library/Frameworks/Python.framework/Versions/3.11/lib/python3.11/site-packages/google/oauth2/service_account.py", line 438, in refresh access_token, expiry, _ = _client.jwt_grant( ^^^^^^^^^^^^^^^^^^ File "/Library/Frameworks/Python.framework/Versions/3.11/lib/python3.11/site-packages/google/oauth2/_client.py", line 328, 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': '__omitted__'})
已做排查:服务账号已加入Owner组,Forms API已启用。
解决方案
1. 修正权限范围(Scopes)
代码里通过[0]只取了Drive权限,但调用Forms API的forms.get接口需要Forms相关权限。修改SCOPES配置,确保包含Forms权限:
# 保留两个权限(如果需要操作Drive的话) SCOPES = ['https://www.googleapis.com/auth/drive', 'https://www.googleapis.com/auth/forms.body'] # 或仅保留Forms权限(无需Drive操作时) # SCOPES = ['https://www.googleapis.com/auth/forms.body']
不要用索引截断列表,否则会丢失Forms权限,导致无法生成正确的访问令牌。
2. 共享目标表单给服务账号
即使服务账号在Owner组,仍需将目标Google表单共享给服务账号邮箱(可在key.json的client_email字段找到),设置为编辑者或查看者权限,确保服务账号能访问该表单。
3. 验证服务账号与API状态
- 确认服务账号确实拥有项目Owner权限
- 重新禁用再启用Forms API,确保API状态生效
内容的提问来源于stack exchange,提问作者openwld
相关产品推荐
相关产品推荐

