I'm trying to fetch a spreadsheet with Google SDK. I'm logged into an account with a Service Account Token Creator role and the service account has access rights to the spreadsheet. This is my code:
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
Calling request.execute() results in this error:
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
I suspect that something is wrong with my gcloud configuration, not the code, because:
This code used to work in the past and the error started to occur despite no changes in the code.
The code works on other machines (other developers)
I reinstalled gcloud sdk and configured it again but it didn't help.