CJNE / ha-porscheconnect

Porsche Connect custom component for Home Assistant
MIT License
29 stars 12 forks source link

{'error': 'invalid_grant', 'error_description': 'Invalid authorization code'} #186

Closed kgroshert closed 1 year ago

kgroshert commented 1 year ago

Version of the custom_component

0.0.14

Describe the bug

Adding of integrations fails without real error message. Tried 0.0.13, 0.0.14 and main.

Debug log / Description

This morning found the integration entities unavailable with these in the logs:

2023-06-15 06:51:40.774 ERROR (MainThread) [custom_components.porscheconnect] Unexpected error fetching porscheconnect data: tuple index out of range
  File "/config/custom_components/porscheconnect/__init__.py", line 248, in _async_update_data
  File "/config/custom_components/porscheconnect/__init__.py", line 146, in _update_data_for_vehicle
  File "/usr/local/lib/python3.11/site-packages/pyporscheconnectapi/client.py", line 483, in getServices
  File "/usr/local/lib/python3.11/site-packages/pyporscheconnectapi/connection.py", line 200, in get
  File "/usr/local/lib/python3.11/site-packages/pyporscheconnectapi/connection.py", line 254, in _createhead
  File "/usr/local/lib/python3.11/site-packages/pyporscheconnectapi/connection.py", line 167, in _requestToken
2023-06-15 06:56:40.649 ERROR (MainThread) [custom_components.porscheconnect] Unexpected error fetching porscheconnect data: tuple index out of range
  File "/config/custom_components/porscheconnect/__init__.py", line 248, in _async_update_data
  File "/config/custom_components/porscheconnect/__init__.py", line 146, in _update_data_for_vehicle
  File "/usr/local/lib/python3.11/site-packages/pyporscheconnectapi/client.py", line 483, in getServices
  File "/usr/local/lib/python3.11/site-packages/pyporscheconnectapi/connection.py", line 200, in get
  File "/usr/local/lib/python3.11/site-packages/pyporscheconnectapi/connection.py", line 254, in _createhead
  File "/usr/local/lib/python3.11/site-packages/pyporscheconnectapi/connection.py", line 167, in _requestToken

Saw there was an update to 0.0.14 available so I updated to 0.0.14. Still no improvement after installing and restarting. So I deleted the integration to re-add it (just to be sure).

Now if I enter the credentials, the logs show:

2023-06-15 08:40:00.547 DEBUG (MainThread) [custom_components.porscheconnect.config_flow] Attempt login...
2023-06-15 08:40:01.139 WARNING (MainThread) [homeassistant.util.async_] Detected blocking call to sleep inside the event loop. This is causing stability issues. Please report issue to the custom integration author for porscheconnect doing blocking calls at custom_components/porscheconnect/config_flow.py, line 35: await conn._login()
2023-06-15 08:40:03.831 WARNING (MainThread) [homeassistant.util.async_] Detected blocking call to sleep inside the event loop. This is causing stability issues. Please report issue to the custom integration author for porscheconnect doing blocking calls at custom_components/porscheconnect/config_flow.py, line 35: await conn._login()

and after a few seconds the setup shows up again like this:

image

Not sure where to go from here. Debug for the integration is already enabled. Can I enable debugging for HA configflow somehow?

kgroshert commented 1 year ago

Enabled debug for pyporscheconnectapi. Full log, redacted (xxx) auth-tokens. Hope I didn't redact too much:

2023-06-15 09:36:58.237 DEBUG (MainThread) [pyporscheconnectapi.connection] New connection prepared
2023-06-15 09:36:58.237 DEBUG (MainThread) [custom_components.porscheconnect.config_flow] Attempt login...
2023-06-15 09:36:58.237 DEBUG (MainThread) [pyporscheconnectapi.connection] Start authentication, get initial state from auth server
2023-06-15 09:36:58.478 DEBUG (MainThread) [pyporscheconnectapi.connection] {'state': ['xxx'], 'client': ['xxx'], 'protocol': ['oauth2'], 'response_type': ['code'], 'code_challenge_method': ['S256'], 'redirect_uri': ['https://my.porsche.com/'], 'ui_locales': ['de-DE'], 'audience': ['https://api.porsche.com'], 'scope': ['openid']}
2023-06-15 09:36:58.478 DEBUG (MainThread) [pyporscheconnectapi.connection] {'state': 'xxx', 'client': 'xxx'}
2023-06-15 09:36:58.478 DEBUG (MainThread) [pyporscheconnectapi.connection] POST authentication details....
2023-06-15 09:36:58.678 DEBUG (MainThread) [pyporscheconnectapi.connection] 200
2023-06-15 09:36:58.678 DEBUG (MainThread) [pyporscheconnectapi.connection] <form method="post" name="hiddenform" action="https://identity.porsche.com/login/callback">
    <input type="hidden" name="wctx" value="xxx">
2023-06-15 09:36:58.679 DEBUG (MainThread) [pyporscheconnectapi.connection] {'wa': 'wsignin1.0', 'wresult': 'xxx', 'wctx': '{"strategy":"auth0","auth0Client":"","tenant":"porsche-production","connection":"Username-Password-Authentication","client_id":"xxx","response_type":"code","redirect_uri":"https://my.porsche.com/","state":"xxx","sid":"xxx","audience":"https://api.porsche.com","realm":"Username-Password-Authentication"}'}
2023-06-15 09:36:58.679 DEBUG (MainThread) [pyporscheconnectapi.connection] POST authentication verification...
2023-06-15 09:36:58.838 DEBUG (MainThread) [pyporscheconnectapi.connection] Resume at /authorize/resume?state=xxx
2023-06-15 09:36:58.838 DEBUG (MainThread) [pyporscheconnectapi.connection] Sleeping 2.5s...

2023-06-15 09:36:58.843 WARNING (MainThread) [homeassistant.util.async_] Detected blocking call to sleep inside the event loop. This is causing stability issues. Please report issue to the custom integration author for porscheconnect doing blocking calls at custom_components/porscheconnect/config_flow.py, line 35: await conn._login()
2023-06-15 09:37:01.543 DEBUG (MainThread) [pyporscheconnectapi.connection] {'code': ['xxx']}
2023-06-15 09:37:01.543 DEBUG (MainThread) [pyporscheconnectapi.connection] Got code xxx
2023-06-15 09:37:01.543 DEBUG (MainThread) [pyporscheconnectapi.connection] Sleeping 2.5s...
2023-06-15 09:37:01.545 WARNING (MainThread) [homeassistant.util.async_] Detected blocking call to sleep inside the event loop. This is causing stability issues. Please report issue to the custom integration author for porscheconnect doing blocking calls at custom_components/porscheconnect/config_flow.py, line 35: await conn._login()
2023-06-15 09:37:04.046 DEBUG (MainThread) [pyporscheconnectapi.connection] POST to acces token endpoint...
2023-06-15 09:37:04.046 DEBUG (MainThread) [pyporscheconnectapi.connection] {'client_id': 'xxx', 'grant_type': 'authorization_code', 'code': 'xxx', 'redirect_uri': 'https://my.porsche.com/'}
2023-06-15 09:37:04.046 DEBUG (MainThread) [pyporscheconnectapi.connection] Requesting access token for client id xxx
2023-06-15 09:37:04.228 DEBUG (MainThread) [pyporscheconnectapi.connection] Response status 200
2023-06-15 09:37:04.229 DEBUG (MainThread) [pyporscheconnectapi.connection] {'access_token': 'xxx', 'id_token': 'xxx', 'scope': 'openid', 'expires_in': 43200, 'token_type': 'Bearer'}
2023-06-15 09:37:04.229 DEBUG (MainThread) [pyporscheconnectapi.connection] Token: {'access_token': 'xxx', 'id_token': 'xxx', 'scope': 'openid', 'expires_in': 43200, 'token_type': 'Bearer', 'expiration': 1686825424, 'decoded_token': {'https://identity.porsche.com/ciamId': 'xxx', 'ciamId': 'xxx', 'https://identity.porsche.com/jti': 'xxx', 'porsche_jti': 'xxx', 'iss': 'https://identity.porsche.com/', 'sub': 'auth0|xxx', 'aud': ['https://api.porsche.com', 'https://porsche-production.porsche-production.auth0app.com/userinfo'], 'iat': xxx, 'exp': xxx, 'azp': 'xxx', 'scope': 'openid'}, 'apikey': 'xxx'}
2023-06-15 09:37:04.231 DEBUG (MainThread) [pyporscheconnectapi.connection] POST to acces token endpoint...
2023-06-15 09:37:04.232 DEBUG (MainThread) [pyporscheconnectapi.connection] {'client_id': 'xxx', 'grant_type': 'authorization_code', 'code': 'xxx', 'redirect_uri': 'https://my.porsche.com/'}
2023-06-15 09:37:04.232 DEBUG (MainThread) [pyporscheconnectapi.connection] Requesting access token for client id xxx
2023-06-15 09:37:04.345 DEBUG (MainThread) [pyporscheconnectapi.connection] Response status 403
2023-06-15 09:37:04.345 DEBUG (MainThread) [pyporscheconnectapi.connection] {'error': 'invalid_grant', 'error_description': 'Invalid authorization code'}
CJNE commented 1 year ago

I just pushed version 0.0.15 that should fix the login problems

kgroshert commented 1 year ago

As fast as a Porsche 😄 It works again. Thx!