Spotify Config

I’ve managed to get my configuration working after some hints in this discussion. The problem seems to be, as always, the overly sensitive configuration parser.

I’ve been using Home Assistant for about a week and I really love most about it. But the configuration is appalling. I struggled for a few days to understand that numeric_state = 10 was wrong, it must be numeric_state = ‘10’. A numeric string?? But my temperature sensor would not work this way because somehow it’s a float. above = ‘10.0’ is apparently the correct numeric definition. If I get a more accurate sensor, would I then have to define the correct number of decimals?

Enough ranting…

The base_url seems to be the main issue. I could get a working authorization by replacing the IP-address in the call to Spotify. But then I got an internal server error for “spotipy.oauth2.SpotifyOauthError: Bad Request”
It seems that the base_url must be defined without the trailing slash. Otherwise the callback will not be correct.

I hope this helps someone.

1 Like

I managed to get this working also, I added in the redirect with and without the trailing slash, originally i did not have it but after reading @wahlis’s post i thought id try it with, i probably don’t need both but as it’s working i don’t want to remove just incase.

http://xxx.xxx.xxx.xxx:8123/api/spotify
http://xxx.xxx.xxx.xxx:8123/api/spotify/

are added to spotify as my Redirect URIs.

Thanks for the help, and hope this information helps someone else.

2 Likes

Got mine working.

I had to change the base_url to [local_ip]:8123, run the configurator and then changed it back to how it was previously.

3 Likes

Cant get this to work. I changed my base_url to “http://192.168.1.XX:8123/” and wrote http://192.168.1.xx:8123/api/spotify in the Spotify app redirect uri and saved. Still getting “INVALID_CLIENT: Invalid redirect URI”…

Edit: Removed the last “/” in my base_url and finally got it working!

1 Like

Have exactly the same behaviour as your original post but the re-installation method didn’t work for me.
What are you running your env on? I’m on RPI3 / hassbian.

No matter what I do and on which interface I try (wlan/eth0) the https://IP:port/api/spotify gives me error 500. I bealive this is the source of the issue.

@a_ndy
Which version are you running? You have File "/usr/local/lib/python3.5/site-packages/spotipy/oauth2.py", line 218, in get_access_token raise SpotifyOauthError(response.reason) while my install (HA 0.43.0) refers to oauth.py

here goes my error msg (when trying to just open /api/spotify)

17-04-24 00:19:28 ERROR (MainThread) [aiohttp.server] Error handling request
Traceback (most recent call last):
  File "/srv/homeassistant/lib/python3.4/site-packages/aiohttp/web_protocol.py", line 417, in start
    resp = yield from self._request_handler(request)
  File "/srv/homeassistant/lib/python3.4/site-packages/aiohttp/web.py", line 289, in _handle
    resp = yield from handler(request)
  File "/usr/lib/python3.4/asyncio/coroutines.py", line 143, in coro
    res = yield from res
  File "/usr/lib/python3.4/asyncio/coroutines.py", line 143, in coro
    res = yield from res
  File "/srv/homeassistant/lib/python3.4/site-packages/homeassistant/components/http/ban.py", line 58, in ban_middleware_handler
    return (yield from handler(request))
  File "/srv/homeassistant/lib/python3.4/site-packages/homeassistant/components/http/__init__.py", line 422, in handle
    result = handler(request, **request.match_info)
  File "/srv/homeassistant/lib/python3.4/site-packages/homeassistant/components/media_player/spotify.py", line 115, in get
    self.oauth.get_access_token(request.GET['code'])
  File "multidict/_multidict.pyx", line 124, in multidict._multidict._Base.__getitem__ (multidict/_multidict.c:3486)
  File "multidict/_multidict.pyx", line 119, in multidict._multidict._Base._getone (multidict/_multidict.c:3421)
KeyError: "Key not found: 'code'"

As stupid as it may sound, did you hit the SAVE button at the bottom of the page after adding the redirect URL?

Under normal circumstances, I wouldn’t even suggest this, but it is a bit counter intuitive that you have to click a button to add the URL and then again click to save the configuration.

And given that’s what I forgot the first time, I can ask the question now :wink:

2 Likes

I am on 0.43.0 vert

The other thing you may have to look at is the base_url under http:

Mine is base_ulr: https://xxx.duck.net:8123

0.43

I changed the base_url to 192.168.x.x:8123 and added http://192.168.x.x:8123/api/spotify to the URI and it completed successfully. Then changed the base_url back to the domain name I had previously and it continues to work.

I made some updates to the component page (created PR - hasn’t been merged) to reflect the correct URI (and port if applicable) and adding the base_url to http. Also that you can ignore the empty “spotify” file that authorization may prompt you to download.

1 Like

I had this issue as well and it was a combination of things for me.

I am running HASS in a docker container which I think plays a part.

I originally was getting an internal server error 500, this was due to an incorrect base url in my http component configuration. I have haproxy with letsencrypt in front of my hass instance to enforce / proxy ssl. I had to set my http component base url to https://myhassurl.net

Then I began encountering the blank spotify file download issue when trying to authorize. Adding cache_path: /config/.spotify-token-cache to my spotify component configuration fixed that. I think this issue may be specific to running the docker container.

My browser gave me a blank “spotify” file after clicking “Okay”, but HA also created the .spotify-token-cache at the same time.

I get this error:

17-04-28 13:32:44 INFO (MainThread) [homeassistant.components.http] Serving /api/spotify to 192.168.1.1 (auth: False)
17-04-28 13:32:44 ERROR (MainThread) [aiohttp.server] Error handling request
Traceback (most recent call last):
File “/home/homeassistant/.homeassistant/deps/aiohttp/web_protocol.py”, line 417, in start
resp = yield from self._request_handler(request)
File “/home/homeassistant/.homeassistant/deps/aiohttp/web.py”, line 289, in _handle
resp = yield from handler(request)
File “/usr/lib/python3.4/asyncio/coroutines.py”, line 143, in coro
res = yield from res
File “/usr/lib/python3.4/asyncio/coroutines.py”, line 143, in coro
res = yield from res
File “/srv/homeassistant/lib/python3.4/site-packages/homeassistant/components/http/ban.py”, line 58, in ban_middleware_handler
return (yield from handler(request))
File “/srv/homeassistant/lib/python3.4/site-packages/homeassistant/components/http/init.py”, line 422, in handle
result = handler(request, **request.match_info)
File “/srv/homeassistant/lib/python3.4/site-packages/homeassistant/components/media_player/spotify.py”, line 115, in get
self.oauth.get_access_token(request.GET[‘code’])
File “multidict/_multidict.pyx”, line 124, in multidict._multidict._Base.getitem (multidict/_multidict.c:3486)
File “multidict/_multidict.pyx”, line 119, in multidict._multidict._Base._getone (multidict/_multidict.c:3421)
KeyError: “Key not found: ‘code’”

I am:

  • Running Version 43.2
  • …with HTTPS
  • Set my Redirect to https://****.duckdns.org/api/spotify

Any ideas?

Assuming that all the steps in the setup went smoothly - no errors connecting back to your HA install from Spotify.

Do you have a .spotify-token-cache file in the same folder as your config YAML? It should contain some JSON that includes tokens to connect.

When I try this I get as far as clicking the popup link that takes me to a page to “Link Spotify Account”. This doesn’t originally work as I get an “INVALID_CLIENT: Invalid redirect URI” message. This appears to be because it uses the IP address of my rPi - If i change the part of the URL to say “hassbian” instead it gets as far as redirecting to my local rPi based Hassbian installation’s URL but then comes up with a “500 Internal Server Error. Server got itself in trouble” message.

Looking through these posts there’s mention of the .spotify-token-cache file in the same folder as the config YAML file. I didn’t have one so i created one manually. The trouble is, i don’t know what it’s supposed to say. I know it’s some JSON but what exactly? I imagine that it stores a key which is the very long string after this in the URL: “http://hassbian:8123/api/spotify?code=xxxx”.

Any idea what the JSON is in this file so I can try and manually sort it out?

Thanks!

I was getting these same errors as well until I added base_url to my http config. Then I restarted and I was able to authorize Spotify via the frontend.

http:
  base_url: http://192.168.1.3:8123
1 Like

I’m having the same issue. Did you find a way to fix it ?

This is very frustrating. I am having the same issue and I can’t figure out what the different IPs and callbacks are supposed to do.

  • There is a base_url which represents what? What does this have to do with Spotify?
  • There is a callback URL in Spotify that is called for a token but if it’s my internal networks IP then that means nothing to the public Spotify server issuing the callback.

I am a developer and would like to contribute to improving the Spotify integration but need some help in understanding all the moving parts. It seems easy enough to use the Spotify Web API via Postman yet I can’t seem to figure out how Home Assistant does it.

The base_url is only employed in some cases, for example if you have a reverse proxy set up.

The callback url can be your internal network. Why? Because the oauth authorization process happens client side in your browser, and your browser can (presumably) access your internal network.

Thanks guys, as above my authorisation error was solved by removing :443 from my http base_url and I could download the file (that I don’t need)… but whenever I try to interact with Spotify within HA it says:

spotipy.client.SpotifyException: http status: 403, code:-1 - https://api.spotify.com/v1/me/player/pause:
Forbidden.

Yet it’s updating the song and picture in HA! Anyone else come across this?

I changed the client secret key to try and force a re-authotisation but that didn’t seem to do anything either.

EDIT: Adding cache_path: /config/.spotify-token-cache brings up the Configurator in HA and allows me to go through the authorisation again, but the .spotify-token-cache file is never created in my HA config directory.

Had the same problem. Adding base_url solved the problem for me.

http:
api_password: !secret http_password
ssl_certificate: xxx
ssl_key: xxx
base_url: https://mydomain.duckdns.org:443