Spotify Integration - "500 Internal Server Error"

I can’t integrate Spotiy anymore in Version 0.115.2.

First, it ends in " INVALID_CLIENT: Invalid redirect URI". As written in the documentation, the URL needs to be corrected manually.

After that, the browser error “500 Internal Server Error” is shown.

In the HA log, there is a problem logged “may retries exceeded…”

Has anybody a hint, how to continue?

Hello, did you find a solution. I am having the same issue.

Getting the same issue, has anyone managed to figure out what happened?

Thanks anyone.

I managed to resolve by:

Seemed to work for me this time.

Hi, I seem to be having a similar issue.

I created the Spotify app as per this guide https://siytek.com/home-assistant-and-spotify/ and added my redirect uri which is https://mydomain.duckdns.org/auth/external/callback (I have 443 redirected to the Nginx Proxy Manager add-on which handles my external access and works for Google, Alexa etc)

When I go to config>integrations and click on Spotify, it takes me to Spotify to authenticate and then when I have given the app permission to connect with HA it closes this webpage.

In the HA tab, it shows this: spotify 1
If I click on the Open Webpage button it pastes the response from Spotify and I get an HA 500 error:

I have removed and recreated the Spotify app several times and updated the client_id and secret each time with a server reboot each time.

I managed to capture the URL that the Spotify integration sends me to and also the response it gave after authentication and they seem to be ok:

https://accounts.spotify.com/authorize?response_type=code&client_id=correct-client-id&redirect_uri=https://mydomain.duckdns.org/auth/external/callback&state=eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJmbG93X2lkIjoiNmE2ODcwMWUyODQ0NDU1YWJhOWYwNzNkMTQzMWY4MzQiLCJyZWRpcmVjdF91cmkiOiJodHRwczovL2hhc3MtcGouZHVja2Rucy5vcmcvYXV0aC9leHRlcm5hbC9jYWxsYmFjayJ9.bu4YzQbCg-CKlmM7CmVHGuggIguB-dcuGD8VKBN_b_w&scope=user-modify-playback-state,user-read-playback-state,user-read-private,playlist-read-private,playlist-read-collaborative,user-library-read,user-top-read,user-read-playback-position,user-read-recently-played,user-follow-read

response:
https://mydomain.duckdns.org/auth/external/callback?code=AQAHGZJxFeXA26FYZbH7jEpV33A0UfBwpmWBc6fcnPP1bvly8knZJMXs9I1r1PeY-5wUNCn7DB9BDkQIO-j5h9PGblbC2okZ6pxIDxSuvXltumJqbeigY9_bJEFR8RrH0z_z9NzXFDmjZeG1c-QxaHCdEDFtxQFrsQxHSFIm24UHqDS7u6HcWTlgO2hHj8PDR9cmfZCUNxaWQVHkFDaapUkz2DtCMr6954Tl5HPsfbMp9PvW3yFrDb7abcU8xQTTymu85vg1KRmUYimByqX55ZIjtzzGEhi4LxNbz7Y3proJm3NIKZekET93Ma66YC8nayqqejcjjlozx_8E0c5FSneKrvA7utFLLhcLVrrnuxK33eZVgGkSucc5i9OEqw0r0xNbLPpxlPgsdZK4W5YRX0WbCZMdTXZ9M2cm-oPnT7AXEX55MqROukGDFNSsEm3m5YMLpY6wUCBrycaoXI9KnxO94S1ToruuBR3f8RtimxtyfjnHIbUQe7atxsPtHSU&state=eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJmbG93X2lkIjoiOGQwYTBlNjA1YzUyNDFlN2FiYTMxNmQyMzUxMzJkYzkiLCJyZWRpcmVjdF91cmkiOiJodHRwczovL2hhc3MtcGouZHVja2Rucy5vcmcvYXV0aC9leHRlcm5hbC9jYWxsYmFjayJ9.1vQIL_Tw7xCgS84xBoOt3SD9GP3BrBNEPnENg8Yr594

I also found this in my HA log file but nothing in Supervisor, Core or config>log:

2021-01-01 18:13:23 ERROR (MainThread) [aiohttp.server] Error handling request
Traceback (most recent call last):
  File "/usr/local/lib/python3.8/site-packages/aiohttp/web_protocol.py", line 422, in _handle_request
    resp = await self._request_handler(request)
  File "/usr/local/lib/python3.8/site-packages/aiohttp/web_app.py", line 499, in _handle
    resp = await handler(request)
  File "/usr/local/lib/python3.8/site-packages/aiohttp/web_middlewares.py", line 118, in impl
    return await handler(request)
  File "/usr/src/homeassistant/homeassistant/components/http/request_context.py", line 18, in request_context_middleware
    return await handler(request)
  File "/usr/src/homeassistant/homeassistant/components/http/ban.py", line 72, 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 129, in handle
    result = await result
  File "/usr/src/homeassistant/homeassistant/components/config/config_entries.py", line 160, in get
    return await super().get(request, flow_id)
  File "/usr/src/homeassistant/homeassistant/helpers/data_entry_flow.py", line 92, in get
    result = await self._flow_mgr.async_configure(flow_id)
  File "/usr/src/homeassistant/homeassistant/data_entry_flow.py", line 155, 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 213, in _async_handle_step
    result: Dict = await getattr(flow, method)(user_input)
  File "/usr/src/homeassistant/homeassistant/helpers/config_entry_oauth2_flow.py", line 286, 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 165, in async_resolve_external_data
    return await self._token_request(
  File "/usr/src/homeassistant/homeassistant/helpers/config_entry_oauth2_flow.py", line 194, in _token_request
    resp.raise_for_status()
  File "/usr/local/lib/python3.8/site-packages/aiohttp/client_reqrep.py", line 1002, in raise_for_status
    raise ClientResponseError(
aiohttp.client_exceptions.ClientResponseError: 400, message='Bad Request', url=URL('https://accounts.spotify.com/api/token')

I am running HassOS 5.9, Supervisor 2020.12.7 and HA 2020.12.2

Any help or ideas on how to fix would be gratefully received

@ludeeus, are you able to have a look at this issue I am having please?

Thank you

Having the exact same issue. At some point (may have been months ago) my integration failed and I was looking to re-add today. Removed the developer app from Spotify etc and followed the guide, having the same problem as you whereby the callback presents a “500 Internal Server Error”. As you’ve done too, I ensured everything is correct several times to no avail :frowning:

arch x86_64
chassis vm
dev false
docker true
docker_version 19.03.11
hassio true
host_os HassOS 4.12
installation_type Home Assistant OS
os_name Linux
os_version 5.4.56
python_version 3.8.3
supervisor 2020.12.7
timezone Europe/London
version 0.114.0
virtualenv false

Update: I noticed my version was wildly out of date, so went to update it. The update failed as I’ve blocked the Home Assistant doing stupid amounts of DNS lookups to a non-configured DNS server (1.1.1.1) which is not aligned to my network security policy of routing DNS through a pi-hole. I put in an exception rule and updated to the latest version but also kept the rule in place while I attempted Spotify integration which also worked.

Either the update fixed it, or allowing Home Assistant to use the non-configured DNS servers fixed it. Something for you to try, @riddledaxis ?

I managed to link to my Spotify account today.

I followed the advice from another post and opened Chrome on my tablet, logged into my HA server with my external address (which was also set as my Spotify app URI), went to add Spotify and it opened in another Chrome tab and allowed me to authenticate and link my account without an issue.

The only other difference today was that I had removed !secret client_id and client_secret and reluctantly put my client id and secret into my config.

1 Like

Thank you @riddledaxis using Chrome made all the difference for me, with Safari was impossible.

1 Like

This worked for me also.

I have the exact same error. I have tried every possible solution to fix this. Spotify autenticates, but an error 500 is generated as mentioned above. Can this be related to a bug with Aiohttp?

I have tried to downgrade HA still with no luck.

FYI

Today i managed to fix this error by downgrading to 2021.1.5 and upgraded to latest version shortly after. I guess there has been an incomplete upgrade somwhere betweeen latest version and 2021.1.5, causing the problem. I cant find another logic explanation to this.

After downgrading to 2021.1.5 did you restart Home Assistant or did you let it run in cache in between downgrading and upgrading to the latest version again?

[FIXED] It was a DNS issue in my case, a reboot fixed it …

I just had the same problem. I’ve noticed that when I got the 500 error on the browser after trying to add the integration, there would be a new error on the logs with Cannot connect to host accounts.spotify.com:443 (full log entry below)

Since this looked like a name resolution issue, I logged into the HA instance via SSH and tried a simple lookup:

$ nslookup accounts.spotify.com
;; connection timed out; no servers could be reached

Indeed, something’s out of whack with DNS in the instance. Not knowing much about troubleshooting DNS in a dockerized environment, I just restarted the host, and that solved the problem.

Name lookup now working:

$ nslookup accounts.spotify.com
Server:         127.0.0.11
Address:        127.0.0.11#53

Non-authoritative answer:
accounts.spotify.com    canonical name = edge-web-split-geo.dual-gslb.spotify.com.
Name:   edge-web-split-geo.dual-gslb.spotify.com
Address: 35.186.224.25
Name:   edge-web-split-geo.dual-gslb.spotify.com
Address: 2600:1901:1:c36::

And I was able to complete the configuration of the Spotify integration.

For reference, this is the full error entry which caught my attention and made me check DNS resolution:

Log Details (ERROR)
Logger: aiohttp.server
Source: helpers/config_entry_oauth2_flow.py:197
First occurred: 6:22:35 PM (2 occurrences)
Last logged: 6:22:45 PM

Error handling request
Traceback (most recent call last):
  File "/usr/local/lib/python3.8/site-packages/aiohttp/connector.py", line 999, in _create_direct_connection
    hosts = await asyncio.shield(host_resolved)
  File "/usr/local/lib/python3.8/site-packages/aiohttp/connector.py", line 865, in _resolve_host
    addrs = await self._resolver.resolve(host, port, family=self._family)
  File "/usr/local/lib/python3.8/site-packages/aiohttp/resolver.py", line 31, in resolve
    infos = await self._loop.getaddrinfo(
  File "/usr/local/lib/python3.8/asyncio/base_events.py", line 825, in getaddrinfo
    return await self.run_in_executor(
  File "/usr/local/lib/python3.8/concurrent/futures/thread.py", line 57, in run
    result = self.fn(*self.args, **self.kwargs)
  File "/usr/local/lib/python3.8/socket.py", line 918, in getaddrinfo
    for res in _socket.getaddrinfo(host, port, family, type, proto, flags):
socket.gaierror: [Errno -3] Try again

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
  File "/usr/local/lib/python3.8/site-packages/aiohttp/web_protocol.py", line 422, in _handle_request
    resp = await self._request_handler(request)
  File "/usr/local/lib/python3.8/site-packages/aiohttp/web_app.py", line 499, in _handle
    resp = await handler(request)
  File "/usr/local/lib/python3.8/site-packages/aiohttp/web_middlewares.py", line 119, in impl
    return await handler(request)
  File "/usr/src/homeassistant/homeassistant/components/http/security_filter.py", line 56, in security_filter_middleware
    return await handler(request)
  File "/usr/src/homeassistant/homeassistant/components/http/request_context.py", line 18, in request_context_middleware
    return await handler(request)
  File "/usr/src/homeassistant/homeassistant/components/http/ban.py", line 74, in ban_middleware
    return await handler(request)
  File "/usr/src/homeassistant/homeassistant/components/http/auth.py", line 135, in auth_middleware
    return await handler(request)
  File "/usr/src/homeassistant/homeassistant/components/http/view.py", line 131, in handle
    result = await result
  File "/usr/src/homeassistant/homeassistant/components/config/config_entries.py", line 146, in get
    return await super().get(request, flow_id)
  File "/usr/src/homeassistant/homeassistant/helpers/data_entry_flow.py", line 95, in get
    result = await self._flow_mgr.async_configure(flow_id)
  File "/usr/src/homeassistant/homeassistant/data_entry_flow.py", line 199, 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 257, in _async_handle_step
    result: FlowResult = await getattr(flow, method)(user_input)
  File "/usr/src/homeassistant/homeassistant/helpers/config_entry_oauth2_flow.py", line 298, 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 169, in async_resolve_external_data
    return await self._token_request(
  File "/usr/src/homeassistant/homeassistant/helpers/config_entry_oauth2_flow.py", line 197, in _token_request
    resp = await session.post(self.token_url, data=data)
  File "/usr/local/lib/python3.8/site-packages/aiohttp/client.py", line 520, in _request
    conn = await self._connector.connect(
  File "/usr/local/lib/python3.8/site-packages/aiohttp/connector.py", line 535, in connect
    proto = await self._create_connection(req, traces, timeout)
  File "/usr/local/lib/python3.8/site-packages/aiohttp/connector.py", line 892, in _create_connection
    _, proto = await self._create_direct_connection(req, traces, timeout)
  File "/usr/local/lib/python3.8/site-packages/aiohttp/connector.py", line 1011, in _create_direct_connection
    raise ClientConnectorError(req.connection_key, exc) from exc
aiohttp.client_exceptions.ClientConnectorError: Cannot connect to host accounts.spotify.com:443 ssl:default [Try again]

Using my external address to get to my HA instance also worked. With the local IP it does not work and it breaks the redirect_uri.

I think the documentation needs to be adapted, it does not work with the local IP.

1 Like

I followed this topic as this was the error that I received while trying to integrate Spotify. However, in my case the cause was something completly different. By looking at the log files I found the following error: …init() got an unexpected keyword argument ‘allowed_methods’… and based on it I arrived to this GitHub page: TypeError: __init__() got an unexpected keyword argument 'allowed_methods' · Issue #647 · plamere/spotipy · GitHub. Apparently I had an old version of urllib3>=1.26 and requests>=2.25 due to the fact that I messed around the docker container by installing PyP100 in an attempt to have some Tapo P100 sockets operational. I’m posting this here maybe someone will find it usefull.