Spotify: INVALID_CLIENT: Invalid redirect URI because wrong ip/url is inserted

Greetings,

I’ve seen the other post following the new Spotify integration regarding the INVALID_CLIENT error.
But I have a slightly different issue:
My Setup:

  • Home Assistant in Docker (HTTP only)
  • Nginx handling the subdomain https://hassio.[mydomain]/ and enforcing SSL. (Standart 443 port)
    So my home assistant is reachable at https://hassio.domain/

But the Spotify Integration creates a link

https://accounts.spotify.com/authorize?response_type=code&client_id=[id]&redirect_uri=http://[some-ip]/auth/external/callback&state=[…]&scope=user-modify-playback-state,user-read-playback-state,user-read-private

This [some-ip] is no IP I know, especially neither the public nor local IP address of the home assistant server. Even if so, it won’t work, as Nginx doesn’t redirect access to the IP without hostname to the hassio subdomain. And as you note, it is also, not SSL.
I would expect https://hassio.[mydomain]/auth/external/callback... (which I added to the Spotify Developer settings)

If I manually change the link it won’t work either with following error in the home assistant log:


2020-06-03 19:51:05 ERROR (MainThread) [aiohttp.server] Error handling request
Traceback (most recent call last):
  File "/usr/local/lib/python3.7/site-packages/aiohttp/web_protocol.py", line 418, in start
    resp = await task
  File "/usr/local/lib/python3.7/site-packages/aiohttp/web_app.py", line 458, in _handle
    resp = await handler(request)
  File "/usr/local/lib/python3.7/site-packages/aiohttp/web_middlewares.py", line 119, in impl
    return await handler(request)
  File "/usr/src/homeassistant/homeassistant/components/http/real_ip.py", line 39, in real_ip_middleware
    return await handler(request)
  File "/usr/src/homeassistant/homeassistant/components/http/ban.py", line 73, in ban_middleware
    return await handler(request)
  File "/usr/src/homeassistant/homeassistant/components/http/auth.py", line 127, in auth_middleware
    return await handler(request)
  File "/usr/src/homeassistant/homeassistant/components/http/view.py", line 125, in handle
    result = await result
  File "/usr/src/homeassistant/homeassistant/helpers/config_entry_oauth2_flow.py", line 376, in get
    flow_id=state["flow_id"], user_input=request.query["code"]
  File "/usr/src/homeassistant/homeassistant/data_entry_flow.py", line 153, in async_configure
    result = await self._async_handle_step(flow, cur_step["step_id"], user_input)
  File "/usr/src/homeassistant/homeassistant/data_entry_flow.py", line 201, in _async_handle_step
    result: Dict = await getattr(flow, method)(user_input)
  File "/usr/src/homeassistant/homeassistant/helpers/config_entry_oauth2_flow.py", line 248, in async_step_creation
    token = await self.flow_impl.async_resolve_external_data(self.external_data)
  File "/usr/src/homeassistant/homeassistant/helpers/config_entry_oauth2_flow.py", line 142, in async_resolve_external_data
    "redirect_uri": self.redirect_uri,
  File "/usr/src/homeassistant/homeassistant/helpers/config_entry_oauth2_flow.py", line 167, in _token_request
    resp.raise_for_status()
  File "/usr/local/lib/python3.7/site-packages/aiohttp/client_reqrep.py", line 946, in raise_for_status
    headers=self.headers)
aiohttp.client_exceptions.ClientResponseError: 400, message='Bad Request', url='https://accounts.spotify.com/api/token

Does someone have an idea on this?

An Update: The IP address is the local IP inside docker. So it seems to be more a home assistant <-> docker problem

I got the answer:
Set following or similar:

homeassistant:
  internal_url: https://hassio.[mydomain]/

The reason: The Spotify component uses get_url() without prefere_external, so using the internal IP address, which defaults to the local (docker) IP if not set.

1 Like