使用Google SDK获取表格时出现TypeError: 'str' object is not callable错误
GCP服务账号Impersonation调用Sheets API报错:TypeError: 'str' object is not callable
问题背景
我正尝试使用Google SDK获取电子表格,当前登录账号拥有Service Account Token Creator角色,目标服务账号已具备该电子表格的访问权限。
代码示例
def build_gcp_sheets_service(): target_scopes = ['https://www.googleapis.com/auth/spreadsheets.readonly'] source_credentials, _ = google.auth.default() target_credentials = impersonated_credentials.Credentials( source_credentials=source_credentials, target_principal=config.gcp.local_service_account, target_scopes=target_scopes, lifetime=60 ) service = build('sheets', 'v4', credentials=target_credentials) return service service = build_gcp_sheets_service() sheet = service.spreadsheets() request = sheet.values().get(spreadsheetId=file_id, range=sheet_range) result = request.execute() # Error occurs here
报错信息
调用request.execute()时触发以下错误:
Traceback (most recent call last): File "/aircraft-detection/ml/gcp.py", line 69, in _fetch_spreadsheet result = request.execute() ^^^^^^^^^^^^^^^^^ File "/usr/local/lib/python3.11/site-packages/googleapiclient/_helpers.py", line 130, in positional_wrapper return wrapped(*args, **kwargs) ^^^^^^^^^^^^^^^^^^^^^^^^ File "/usr/local/lib/python3.11/site-packages/googleapiclient/http.py", line 923, in execute resp, content = _retry_request( ^^^^^^^^^^^^^^^ File "/usr/local/lib/python3.11/site-packages/googleapiclient/http.py", line 191, in _retry_request resp, content = http.request(uri, method, *args, **kwargs) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "/usr/local/lib/python3.11/site-packages/google_auth_httplib2.py", line 207, in request self.credentials.before_request(self._request, method, uri, request_headers) File "/usr/local/lib/python3.11/site-packages/google/auth/credentials.py", line 228, in before_request self._blocking_refresh(request) File "/usr/local/lib/python3.11/site-packages/google/auth/credentials.py", line 191, in _blocking_refresh self.refresh(request) File "/usr/local/lib/python3.11/site-packages/google/auth/credentials.py", line 365, in refresh self._refresh_token(request) File "/usr/local/lib/python3.11/site-packages/google/auth/impersonated_credentials.py", line 289, in _refresh_token self._source_credentials._refresh_token(request) TypeError: 'str' object is not callable
怀疑方向
我认为问题出在gcloud配置而非代码,理由:
- 这段代码之前可正常运行,无代码改动情况下突然报错;
- 代码在其他开发者的机器上可正常运行。
已尝试过重装gcloud SDK并重新配置,但问题仍未解决。
排查与解决建议
- 检查本地gcloud认证状态
执行gcloud auth list确认当前活跃账号是否为拥有Service Account Token Creator角色的账号;执行gcloud auth application-default print-access-token验证默认凭证是否有效,若返回错误则重新执行gcloud auth application-default login刷新默认凭证。 - 排查环境变量干扰
检查是否存在GOOGLE_APPLICATION_CREDENTIALS环境变量,若该变量指向无效的服务账号密钥文件(或路径字符串未被正确解析为凭证对象),会导致google.auth.default()返回异常类型;执行unset GOOGLE_APPLICATION_CREDENTIALS(Linux/macOS)或set GOOGLE_APPLICATION_CREDENTIALS=(Windows)临时清除该变量后重新测试。 - 验证Impersonation权限
手动测试服务账号模拟是否可行:执行gcloud auth print-access-token --impersonate-service-account=[目标服务账号邮箱],若报错则需重新确认当前账号的Service Account Token Creator角色是否绑定在目标服务账号的正确层级。 - 对齐依赖版本
对比其他正常运行机器的google-auth、google-api-python-client版本,执行pip freeze | grep google-查看本地版本,若版本不一致则安装对应版本:pip install google-auth==[对应版本] google-api-python-client==[对应版本]
内容的提问来源于stack exchange,提问作者Szymon Gołębiowski
相关产品推荐
相关产品推荐

