ReneNulschDE / mbapi2020

Custom Component to integrate MercedesME devices into Home-Assistant
Other
142 stars 27 forks source link

Authentication failed. Please reauthenticate. #208

Closed jlatvala closed 4 months ago

jlatvala commented 4 months ago

The problem

Every now and then (about once per week) authentication fails and I have to delete and add integration again.

Logs: 2024-03-01 13:12:44.028 ERROR (MainThread) [custom_components.mbapi2020.oauth] Error requesting: https://id.mercedes-benz.com/as/token.oauth2 - 0 - {"error_description":"unknown, invalid, or expired refresh token","error":"invalid_grant"} 2024-03-01 13:12:44.029 ERROR (MainThread) [custom_components.mbapi2020] Authentication failed. Please reauthenticate. 2024-03-01 13:12:44.114 ERROR (MainThread) [homeassistant] Error doing job: Task exception was never retrieved Traceback (most recent call last): File "/usr/src/homeassistant/homeassistant/config_entries.py", line 948, in async_init flow, result = await task ^^^^^^^^^^ File "/usr/src/homeassistant/homeassistant/config_entries.py", line 976, in _async_init result = await self._async_handle_step(flow, flow.init_step, data) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "/usr/src/homeassistant/homeassistant/data_entry_flow.py", line 501, in _async_handle_step result: FlowResult = await getattr(flow, method)(user_input) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "/config/custom_components/mbapi2020/config_flow.py", line 128, in async_step_reauth self._reauth_entry = self.hass.config_entries.async_get_entry(self.context["entry_id"])


KeyError: 'entry_id'

### What version of MBAPI2020 do use?

v0.11.1

### What was the last working version of MBAPI2020?

_No response_

### What type of installation are you running?

Home Assistant OS

### Diagnostics information

_No response_

### Example YAML snippet

_No response_

### Anything in the logs that might be useful for us?

_No response_

### Additional information

_No response_
ReneNulschDE commented 4 months ago

Hi,

thanks. The Key-error is fixed in the latest beta.

The root cause is still unknown. I had this once this week and currently try to find out the root cause. In case you want help, please set the log level for the component to debug and share the log via email mbdev.i208@nulsch.de after it happens again.

kneip68 commented 4 months ago

Just had the reauthentification issue after installing latest stable, then installed latest beta. Now works again.

Unfortunatly Debug log downloaded does not contain the error messages before restart, but at least I can confirm beta is working for me.

Thx for all your work.

KR

Kneip

fvbrandt commented 4 months ago

I had the same KeyError: 'entry_id' and thus upgraded to v0.11.2-beta.1 but now I have a different problem: I still get Authentication failed. Please reauthenticate. but it is persistent now (i.e. the error appears each time after I re-authenticate) and the exception is this:

 ERROR (MainThread) [custom_components.mbapi2020.oauth] Error requesting: https://id.mercedes-benz.com/as/token.oauth2
 - 0 - {"error_description":"unknown, invalid, or expired refresh token","error":"invalid_grant"}

Is this a wholly separate issue? Any ideas how to fix it?

dersch81 commented 4 months ago

I had the same KeyError: 'entry_id' and thus upgraded to v0.11.2-beta.1 but now I have a different problem: I still get Authentication failed. Please reauthenticate. but it is persistent now (i.e. the error appears each time after I re-authenticate) and the exception is this:

 ERROR (MainThread) [custom_components.mbapi2020.oauth] Error requesting: https://id.mercedes-benz.com/as/token.oauth2
 - 0 - {"error_description":"unknown, invalid, or expired refresh token","error":"invalid_grant"}

Is this a wholly separate issue? Any ideas how to fix it?

Same here. I can't reauthenticate now anymore with the same error. I also reinstalled the last stable but same issue.

ReneNulschDE commented 4 months ago

I have published a new beta version v0.11.2-beta2

This adds some special handling to hopefully address the issue.

dersch81 commented 4 months ago

I have published a new beta version v0.11.2-beta2

This adds some special handling to hopefully address the issue.

Unfortunatley not.

2024-03-04 13:26:03.728 DEBUG (MainThread) [custom_components.mbapi2020] Start async_setup_entry.
2024-03-04 13:26:03.728 DEBUG (MainThread) [custom_components.mbapi2020.client] WSL not detected - running in rlock mode
2024-03-04 13:26:03.728 DEBUG (MainThread) [custom_components.mbapi2020.oauth] Start async_get_cached_token()
2024-03-04 13:26:03.729 DEBUG (MainThread) [custom_components.mbapi2020.oauth] custom_components.mbapi2020.oauth token expired -> start refresh
2024-03-04 13:26:03.729 INFO (MainThread) [custom_components.mbapi2020.oauth] Start async_refresh_access_token() with refresh_token
2024-03-04 13:26:03.729 INFO (MainThread) [custom_components.mbapi2020.oauth] Auth token refresh preflight request 1
2024-03-04 13:26:03.731 DEBUG (MainThread) [custom_components.mbapi2020] Can not connect to MB OAuth API https://id.mercedes-benz.com. Will try again.
ReneNulschDE commented 4 months ago

Sorry, my mistake - the dev proxy was still active. I have published a new beta release v0.11.2-beta.3

dersch81 commented 4 months ago

image

image

Dieser Fehler wurde von einer benutzerdefinierten Integration verursacht

Logger: custom_components.mbapi2020.oauth Source: custom_components/mbapi2020/oauth.py:317 Integration: MercedesME 2020 (documentation, issues) First occurred: 14:15:47 (2 occurrences) Last logged: 14:19:57

Error requesting: https://id.mercedes-benz.com/as/token.oauth2 - 0 - {"error_description":"unknown, invalid, or expired refresh token","error":"invalid_grant"}

When i then try again, i don't receive a new PIN anymore via mail and the integration will not stop to ask for a PIN which i never receive.

ReneNulschDE commented 4 months ago

Are you sure that you installed the latest beta version 0.11.2-beta.3? Please enable the debug log and restart HA. Then try again to request a pin.

dersch81 commented 4 months ago

Yes

image

ReneNulschDE commented 4 months ago

Could you open a new issue and share the debug log, system information, please - we have to analyze why you don't get a pin.

dersch81 commented 4 months ago

Sure i can do, but i don't think it has anything to do with the mail itself. Still getting this:

2024-03-04 15:48:58.577 DEBUG (MainThread) [custom_components.mbapi2020] Start async_setup_entry.
2024-03-04 15:48:58.577 DEBUG (MainThread) [custom_components.mbapi2020.client] WSL not detected - running in rlock mode
2024-03-04 15:48:58.577 DEBUG (MainThread) [custom_components.mbapi2020.oauth] Start async_get_cached_token()
2024-03-04 15:48:58.578 DEBUG (MainThread) [custom_components.mbapi2020.oauth] custom_components.mbapi2020.oauth token expired -> start refresh
2024-03-04 15:48:58.578 INFO (MainThread) [custom_components.mbapi2020.oauth] Start async_refresh_access_token() with refresh_token
2024-03-04 15:48:58.578 INFO (MainThread) [custom_components.mbapi2020.oauth] Auth token refresh preflight request 1
ReneNulschDE commented 4 months ago

Did you rerun the reauth procedure? What is the log entry after preflight?

dersch81 commented 4 months ago

Dieser Fehler wurde von einer benutzerdefinierten Integration verursacht

Logger: custom_components.mbapi2020.oauth Source: custom_components/mbapi2020/oauth.py:317 Integration: MercedesME 2020 (documentation, issues) First occurred: 15:49:07 (2 occurrences) Last logged: 16:24:10

Error requesting: https://id.mercedes-benz.com/as/token.oauth2 - 0 - {"error_description":"unknown, invalid, or expired refresh token","error":"invalid_grant"}

ReneNulschDE commented 4 months ago

Could please try to be a little bit more descriptive otherwise I cant help you? This error message is not an reauth-error. If you don't want to do this, then you can only delete the integration from the HA integration page and re-add it to get a clean state.

dersch81 commented 4 months ago

But this was the answer of your request. I don't had any other error in the log right after the preflight.

Also i think there is no other request for a new PIN if no Pin or a wrong or a expired pin has been entered. This need to be handled by the integration i guess. But in my case without a PIN or an expired there is no way to request a new one.

fvbrandt commented 4 months ago

Thank you @ReneNulschDE for taking the time to help us. I'll try to add what I can.

So far I am hesitant to just remove and install the integration again because I am worried that some entities might end up with different names or similar which would break my code. Let us know if you feel like you can't easily fix this, then I'll just take the bullet. ;)

ReneNulschDE commented 4 months ago

@fvbrandt : Thanks for the details. Do you have the option to click on the "configure" link on the integration page?

fvbrandt commented 4 months ago

@fvbrandt : Thanks for the details. Do you have the option to click on the "configure" link on the integration page?

💯 That was the right hint! Under the three-dot-menu there was the "configure" option and there I only chose "Delete authentication token file now. require a restart of Home Assistant after save." and pressed submit. Then restart and one final re-authentication and this time it worked. So I guess it was a missing cache invalidation in some place?

ReneNulschDE commented 4 months ago

Thanks - let me try to find out why HA does not store the new token. We see in your log that after 3ms the reload starts, I assume that the token is not saved.

@dersch81 : Can you try the same?

dersch81 commented 4 months ago

Thanks - let me try to find out why HA does not store the new token. We see in your log that after 3ms the reload starts, I assume that the token is not saved.

@dersch81 : Can you try the same?

Yes, that fixed it! Many thanks! I did never notice that option but now i will never forget :)

Jealy91 commented 4 months ago

Beta version & re-auth sorted this for me. Thanks as always @ReneNulschDE!