diff --git a/homeassistant/helpers/config_entry_oauth2_flow.py b/homeassistant/helpers/config_entry_oauth2_flow.py index 526a774cc39..c4d7de3839e 100644 --- a/homeassistant/helpers/config_entry_oauth2_flow.py +++ b/homeassistant/helpers/config_entry_oauth2_flow.py @@ -191,6 +191,13 @@ class LocalOAuth2Implementation(AbstractOAuth2Implementation): data["client_secret"] = self.client_secret resp = await session.post(self.token_url, data=data) + if resp.status >= 400 and _LOGGER.isEnabledFor(logging.DEBUG): + body = await resp.text() + _LOGGER.debug( + "Token request failed with status=%s, body=%s", + resp.status, + body, + ) resp.raise_for_status() return cast(dict, await resp.json()) diff --git a/tests/helpers/test_config_entry_oauth2_flow.py b/tests/helpers/test_config_entry_oauth2_flow.py index 157bbf3bc23..f2f2db37d7f 100644 --- a/tests/helpers/test_config_entry_oauth2_flow.py +++ b/tests/helpers/test_config_entry_oauth2_flow.py @@ -3,6 +3,7 @@ import asyncio import logging import time +import aiohttp import pytest from homeassistant import config_entries, data_entry_flow, setup @@ -546,3 +547,32 @@ async def test_implementation_provider(hass, local_impl): assert await config_entry_oauth2_flow.async_get_implementations( hass, mock_domain_with_impl ) == {TEST_DOMAIN: local_impl, "cloud": provider_source[mock_domain_with_impl]} + + +async def test_oauth_session_refresh_failure( + hass, flow_handler, local_impl, aioclient_mock +): + """Test the OAuth2 session helper when no refresh is needed.""" + flow_handler.async_register_implementation(hass, local_impl) + + aioclient_mock.post(TOKEN_URL, status=400) + + config_entry = MockConfigEntry( + domain=TEST_DOMAIN, + data={ + "auth_implementation": TEST_DOMAIN, + "token": { + "refresh_token": REFRESH_TOKEN, + "access_token": ACCESS_TOKEN_1, + # Already expired, requires a refresh + "expires_in": -500, + "expires_at": time.time() - 500, + "token_type": "bearer", + "random_other_data": "should_stay", + }, + }, + ) + + session = config_entry_oauth2_flow.OAuth2Session(hass, config_entry, local_impl) + with pytest.raises(aiohttp.client_exceptions.ClientResponseError): + await session.async_request("post", "https://example.com")