You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I wanted to write a small Python script for myself, but found that whenever I use Sheets service I get the following error: AttributeError: 'Credentials' object has no attribute 'request'.
I am relatively new to Python, so bear with me if I missed something obvious.
importos.pathfromioimportBytesIOfromgoogle.auth.transport.requestsimportRequestfromgoogle.oauth2.credentialsimportCredentialsfromgoogle_auth_oauthlib.flowimportInstalledAppFlowfromgoogleapiclient.httpimportMediaIoBaseUploadfromgoogleapiclient.discoveryimportResource, buildSCOPES= [
"https://www.googleapis.com/auth/documents",
"https://www.googleapis.com/auth/drive",
"https://www.googleapis.com/auth/spreadsheets",
]
defauthenticate(
scopes: list[str],
*,
token_filepath: str="token.json",
credentials_filepath: str="credentials.json",
):
creds=Noneifos.path.exists(token_filepath):
creds=Credentials.from_authorized_user_file(token_filepath)
ifnotcreds.has_scopes(scopes):
print("Token scopes do not match the required scopes. Regenerating token.")
os.remove(token_filepath)
creds=None# If there are no credentials available or not all scopes are avaliable, let the user log in.ifnotcreds:
print("No credentials available. Requesting new token")
flow=InstalledAppFlow.from_client_secrets_file(credentials_filepath, scopes)
creds=flow.run_local_server(port=0)
# Save the credentials for the next runwithopen(token_filepath, "w") astoken:
token.write(creds.to_json())
ifnotcreds.validandcreds.expiredandcreds.refresh_token:
creds.refresh(Request())
returncredsif__name__=="__main__":
creds=authenticate(SCOPES)
sheets_service=build("sheets", "v4", creds)
spreadsheet= {"properties": {"title": "test"}}
spreadsheet= ( # <--- error occurs because of this linesheets_service.spreadsheets()
.create(body=spreadsheet, fields="spreadsheetId")
.execute()
)
Stack trace
~/projects/repros/app ❯ poetry run python -m app
Traceback (most recent call last):
File "<frozen runpy>", line 198, in _run_module_as_main
File "<frozen runpy>", line 88, in _run_code
File "/Users/demian/projects/repros/app/app/__main__.py", line 56, in <module>
.execute()
^^^^^^^^^
File "/Users/demian/Library/Caches/pypoetry/virtualenvs/app-PJXYrkgl-py3.12/lib/python3.12/site-packages/googleapiclient/_helpers.py", line 130, in positional_wrapper
return wrapped(*args, **kwargs)
^^^^^^^^^^^^^^^^^^^^^^^^
File "/Users/demian/Library/Caches/pypoetry/virtualenvs/app-PJXYrkgl-py3.12/lib/python3.12/site-packages/googleapiclient/http.py", line 923, in execute
resp, content = _retry_request(
^^^^^^^^^^^^^^^
File "/Users/demian/Library/Caches/pypoetry/virtualenvs/app-PJXYrkgl-py3.12/lib/python3.12/site-packages/googleapiclient/http.py", line 191, in _retry_request
resp, content = http.request(uri, method, *args, **kwargs)
^^^^^^^^^^^^
AttributeError: 'Credentials' object has no attribute 'request'```
First I thought perhaps your issue was related to my issue, but diving deeper into your problem I don't think that was the case.
The reason why I had to dive deeper into your problem is because you are using a different authentication scope then I do.
In addition, you also use a service account, and I use an access token.
I think this is important information. Oh, also, I'm just shooting here, I'm not sure if this is the reason why your Credentials don't work. But let me share anyways:
The first thing that I noticed, was that your scope difference from mine. Mine looks like this
The scope is obtained from the access token in my single sign on flow. I had to know if you simply forgot some keywords in your scope, since I have email, profile and openid. So I checked the docs about scopes.
In my honest opinion, google docs are a bit strange and overwhelming, but you can click the Apps Script API, v1 title above the table where your scopes are located.
It opens this page, with a very annoying warning; Warning: The Apps Script API does not work with [service accounts](https://developers.google.com/identity/protocols/OAuth2ServiceAccount).
My guess, is that your Credentials have no request attribute because service accounts probably cannot use the API at all. Which explains why there is no request attribute.
I do think the library should give you a proper warning / error when this happens if I'm correct
I wouldn't mind a second opinion to confirm my way of thoughts
@0x78f1935 thanks! You gave me idea to check if quickstart code works for me, and it did! Then I started to partially replace it with my code and found that the issue is in how I pass creds to build command, I passed it as normal argument instead of named one: build("sheets", "v4", creds) -> build("sheets", "v4", credentials=creds)
This small typing mismatch caused the entire error.
That brings me to another important point: type safety seems lacking in the google-api-python-client library. The build function doesn't specify parameter types, making it hard to catch these mistakes. I discovered there is a google-api-python-client-stubs package that solves this issue, but it’s not mentioned in the docs.
Uh oh!
There was an error while loading. Please reload this page.
I wanted to write a small Python script for myself, but found that whenever I use Sheets service I get the following error:
AttributeError: 'Credentials' object has no attribute 'request'
.I am relatively new to Python, so bear with me if I missed something obvious.
Environment details
google-api-python-client
version: 2.155.0Steps to reproduce
poetry new app
poetry add google-api-python-client google-auth-httplib2 google-auth-oauthlib
credentials.json
filepoetry run python -m app
Code example
app/__main__.py
:Stack trace
Poetry dependencies:
The text was updated successfully, but these errors were encountered: