RogerSelwyn / O365-HomeAssistant

Office 365 integration for Home Assistant
MIT License
203 stars 15 forks source link

Error: Token Expired, but azure says otherwise and replacing with new token does not work either #223

Closed Holdi601 closed 6 months ago

Holdi601 commented 6 months ago

Good Afternoon

So I am using your Integration for some time, and it has really worked well for most of it. However for a couple of weeks now i get the error:

`Logger: homeassistant.setup Source: setup.py:404 First occurred: 16:16:19 (1 occurrences) Last logged: 16:16:19

Error during setup of component o365 Traceback (most recent call last): File "/usr/local/lib/python3.12/site-packages/O365/connection.py", line 785, in _internal_request response = request_obj.request(method, url, **kwargs) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "/usr/local/lib/python3.12/site-packages/requests_oauthlib/oauth2_session.py", line 483, in request url, headers, data = self._client.add_token( ^^^^^^^^^^^^^^^^^^^^^^^ File "/usr/local/lib/python3.12/site-packages/oauthlib/oauth2/rfc6749/clients/base.py", line 217, in add_token raise TokenExpiredError() oauthlib.oauth2.rfc6749.errors.TokenExpiredError: (token_expired)

During handling of the above exception, another exception occurred:

Traceback (most recent call last): File "/usr/src/homeassistant/homeassistant/setup.py", line 404, in _async_setup_component result = await task ^^^^^^^^^^ File "/config/custom_components/o365/init.py", line 50, in async_setup await _async_setup_account(hass, account, conf_type) File "/config/custom_components/o365/init.py", line 96, in _async_setup_account check_token = await _async_check_token(hass, account, account_name) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "/config/custom_components/o365/init.py", line 113, in _async_check_token await hass.async_add_executor_job(account.get_current_user) File "/usr/local/lib/python3.12/concurrent/futures/thread.py", line 58, in run result = self.fn(*self.args, self.kwargs) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "/usr/local/lib/python3.12/site-packages/O365/account.py", line 132, in get_current_user return directory.get_current_user() ^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "/usr/local/lib/python3.12/site-packages/O365/directory.py", line 328, in get_current_user return self._get_user(url, query=query) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "/usr/local/lib/python3.12/site-packages/O365/directory.py", line 302, in _get_user response = self.con.get(url, params=params) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "/usr/local/lib/python3.12/site-packages/O365/connection.py", line 898, in get return self.oauth_request(url, 'get', params=params, kwargs) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "/usr/local/lib/python3.12/site-packages/O365/connection.py", line 887, in oauth_request return self._internal_request(self.session, url, method, kwargs) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "/usr/local/lib/python3.12/site-packages/O365/connection.py", line 802, in _internal_request if self.refresh_token() is False: ^^^^^^^^^^^^^^^^^^^^ File "/usr/local/lib/python3.12/site-packages/O365/connection.py", line 699, in refresh_token self.session.refresh_token( File "/usr/local/lib/python3.12/site-packages/requests_oauthlib/oauth2_session.py", line 452, in refresh_token self.token = self._client.parse_request_body_response(r.text, scope=self.scope) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "/usr/local/lib/python3.12/site-packages/oauthlib/oauth2/rfc6749/clients/base.py", line 427, in parse_request_body_response self.token = parse_token_response(body, scope=scope) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "/usr/local/lib/python3.12/site-packages/oauthlib/oauth2/rfc6749/parameters.py", line 441, in parse_token_response validate_token_parameters(params) File "/usr/local/lib/python3.12/site-packages/oauthlib/oauth2/rfc6749/parameters.py", line 448, in validate_token_parameters raise_from_error(params.get('error'), params) File "/usr/local/lib/python3.12/site-packages/oauthlib/oauth2/rfc6749/errors.py", line 399, in raise_from_error raise cls(kwargs) oauthlib.oauth2.rfc6749.errors.InvalidGrantError: (invalid_grant) AADSTS70000: The user could not be authenticated as the grant is expired. The user must sign in again. Trace ID: cacc29fe-9829-4581-9229-ba7efc5e7a00 Correlation ID: 1a83d097-18a0-4a4b-85dc-463b8ea2b751 Timestamp: 2024-05-05 14:16:18Z`

But from my config image

and here my Azure stuff image

and Secrets: the top one is the old one that i used but that it told me didnt work anymore and the lower one is the one i freshly created. How can i fix this? image

Cheers

RogerSelwyn commented 6 months ago

Delete the token and re-authenticate - https://rogerselwyn.github.io/O365-HomeAssistant/token.html

Holdi601 commented 6 months ago

Ah thanks i did that but now im getting: image

RogerSelwyn commented 6 months ago

Please re-create/modify the app ensuring you have followed the points on this page - https://rogerselwyn.github.io/O365-HomeAssistant/prerequisites.html - in particular point 3.

RogerSelwyn commented 6 months ago

Currently you are configured to use the alternate method documented here - https://rogerselwyn.github.io/O365-HomeAssistant/authentication.html#alternate-authentication-method

Holdi601 commented 6 months ago

Thanks all of that worked. Really appreciate the lighting fast help. I didnt found the token thing in the error section. I guess i have searched docu better.

RogerSelwyn commented 6 months ago

I just updated the docs to make it clearer.