SpotifyPlus Integration

My personal opinion, I think its easier from a user standpoint to just add integration and input api credentials there rather than adding credentials manually.
That’s how most other integrations function and i think users would be more familiar.

1 Like

@lerroy_b
I agree, and have updated the wiki docs to reflect that.

The problem will still present itself though for users who want to setup multiple Spotify accounts. The only way to do that is to enter the App Creds manually, as the Add Integration path always defaults to the first credential found.

1 Like

I am still here, despite the automation to update the token every 55 minutes, I always receive this error, if I do not start vocally the first. Music on that specific Google mini device.
The error:
Impossibile eseguire l'azione: script/dormi_linda Validation error: Spotify Cast Application could not be activated on Chromecast device "12643eb50a99ac7b4753ef0412a8b852": Spotify Desktop Player authorization token was not found in the token cache file.

What does your <config>/.storage/spotifyplus_tokens.json file look like? If you post the contents of that file here, make sure you remove most of the access_token and refresh_token values.

Here is an example of what your spotifyplus_tokens.json file should look like, if you have 2 spotifyplus services configured (e.g. 2 different spotify acounts):

{
    "SpotifyWebApiAuthCodePkce/65b708073fc0480ea92a077233ca87bd/YourSpotifyLoginId_1": {
        "access_token": "BQCbwCS7j54 ...",
        "expires_at": 1752420325,
        "expires_in": 3600,
        "refresh_token": "AQBG1kNs1 ...",
        "scope": [
            "streaming"
        ],
        "token_type": "Bearer"
    },
    "SpotifyWebApiAuthCodePkce/65b708073fc0480ea92a077233ca87bd/YourSpotifyLoginId_2": {
        "access_token": "BQAczAB6k32 ...",
        "expires_at": 1752420325,
        "expires_in": 3600,
        "refresh_token": "AQCH4fOr9 ...",
        "scope": [
            "streaming"
        ],
        "token_type": "Bearer"
    }
}

This file is configured using the SpotifyPlus Token Creation Instructions wiki docs.

1 Like

I don’t know what happened, but I actually only have one account configured… even though there were two initially. I have no idea what happened, but I’ll have to start over from the beginning to reconfigure the second account. I apologize for not having discovered this myself.

@Diegocampy
My guess is that the token was revoked by Spotify for some reason. This can happen if another process / account is using the same token, or if you change your Spotify password and forget to re-generate the token in the spotifyplus_tokens.json file.

The token will be removed from the spotifyplus_tokens.json file if Spotify servers return a refresh token revoked message, as the token is no longer valid and would cause issues if just left there.

Does your Spotify login utilize a simple user-id and password for logging into Spotify? Or are you using a Facebook login, or some other mechanism to login to Spotify?

Hi,
I have spotifyplus install for 5 mounts for 2 accounts. for some resone the token file reduce itself from 2 acconts to a single one. (have no idea why)
I’m trying to generate a new token file using the script but I can’t get it to run.
Im trying to set the envirmonet for the script :
pyenv install 3.11.0 fails for me as it doesn’t have this version, so I installed 3.11.0b4 (or 3.11.0a1) , but then ‘pip install spotifywebapipython’ fails with the floowing error :
ERROR: Ignored the following versions that require a different python version: 1.0.242 Requires-Python >3.11.0; 1.0.243 Requires-Python >3.11.0; 1.0.244 Requires-Python >3.11.0; 1.0.251 Requires-Python >3.11.0; 1.0.252 Requires-Python >3.11.0
ERROR: Could not find a version that satisfies the requirement spotifywebapipython (from versions: none)
ERROR: No matching distribution found for spotifywebapipython

any suggestion how to run the token script ?
(trying to run the script on win11)

@AdiKatz5
My apologies - I changed the Python requirements in setup.py to python_requires='>3.11.0', which requires Python 3.11.1 or more. That should have been python_requires='>=3.11.0', which would allow Python v3.11.0 (or any beta / pre-release versions as well).

I just updated the setup.py minimum python version requirement from python_requires='>3.11.0' to python_requires='>=3.11.0' so that the library can be used with Python 3.11.0.

Note that you could also use any version greater than 3.11.0 if you like (e.g. 3.12.x, 3.13.x, etc).

Just issue the following command to update the library (ensure you have the virtual environment active first - env\Scripts\activate.bat) for use with Python v3.11.0 (or v3.11.0xx).

pip install spotifywebapipython -U

Sorry for the trouble.

For my acceptance of the username and password, I have seen that it also works with mail and password. I also used Google access, it remains in use, but to always use my name and password.
My wife with her account ignores how login does. From PC I think I have only logged in for these events. I used username and password

It comforts me to read this, I don’t know or the only one then😅

Thanks!

that solve the script issue, and I was able to run it again and get a fresh token.
once putting the new token file in HA , spotify plus starts to work again.

maybe worth to add some error handling, not to erase the token if you fail to renew it, as maybe it will work in the next try.
or, in order to help identifing the issue, write some error to the log that going to erase account information. ( took me a while to understnd part of the file is missing)

1 Like

I am using tailscale for access to my home assistant will that be an issue forconfiguring this?

This appears to be due to an intermittent issue, most probably the Spotify auth server not responding in a timely manner due to an expired auth token.

Are there any messages in your system log related to the issue? Please so a search of your HA System Log, looking for ANY of the following search keys:

  • token
  • oauth
  • spotifyplus
  • spotifywebapipython

In the meantime, I am working on adding better logging details so that something is logged when a token is removed from the SpotifyPlus token cache file. Will keep you posted on that.

@needbrew99
I am not familiar with tailscale; based on a quick Google search, it appears that is a remote access / VPN type of product used to access HA remotely?

You could try the remote access if you like, but based on past experience with HA OAuth I would suggest doing initial configuration locally using the homeassistant.local:8123 access. After it’s configured, then it should work fine.

@AdiKatz5 @Diegocampy
Just released SpotifyPlus Integration v1.0.181, that updates the spotifywebapipython package, which is where the changes were made.

  • added system log warning messages when removing a token cache file key.
  • the token cache file key will no longer be removed for non-OAuth exceptions, which means if the Spotify auth server has connectivity issues it should still keep the key in the token cache file (the token refresh request would still fail, but not due to invalid grant / other OAuth errors).

You should now see warnings in the HA system log if a key is deleted from the token cache file. Example:

# if token scope was changed
- OAuth2 authorization access scope has changed; token will be destroyed
- Token scope change detected, forcing authorization access; Token scope="old scope", Session scope="new scope"

# if Spotify token refresh failed
- Refresh token error detected for internal storage provider; token will be destroyed.  Exception message: invalid grant, refresh token not supplied, etc.
1 Like

Is there any way to generate Desktop Player tokens with longer expirations? I have 4 Spotify accounts in our house and refreshing them all every 15 days seems pretty tedious. Or get around the need for them?

1 Like

I keep getting this error

“Spotify Cast Application could not be activated on Chromecast device “e87c930aea52d3b98e192ee08f05e1d6”: Spotify Desktop Player authorization token was not found in the token cache file.”

I am not sure what I have done wrong. I tried to remove the integration and set it up again. I also tried to set up just one user instead of multiple and I get the same error no matter what.

1 Like

@mbailey0074
It appears that you have not configured the Spotify Desktop Player Authentication piece, which is required in order to activate Google ChromeCast and Sonos devices. Check out that link for more information.

@BrianL
Tell me more about your Spotify environment …

  1. are you using a username and password to login to Spotify? There have been issues in the past for users that utilize Facebook or other login types with Spotify, in that tokens keep expiring, etc.

  2. Does this only occur when you activate Google Cast devices? or does it happen for your Spotify Connect Zeroconf devices as well? I need to know if it’s the Spotify Desktop Player tokens that are the problem, or the Spotify Developer App tokens used with HA Application Credentials.

  3. have you changed your Spotify password lately? If so, then you will need to refresh the Spotify Desktop Player tokens after changing your password. You will also need to re-auth to your HA Application Credentials as well.

  4. are there any messages in the HA System Log that relate to token or spotifywebapipython? Any messages with oauth or InvalidGrant error maybe?

You should not have to refresh your tokens every 15 days. I have used the same tokens for over a year now with no problems, but I don’t use my Google Cast devices that often.

I did add some code to the latest SpotifyPlus integration release that will hopefully correct most of the token deletions from the spotifyplus_tokens.json file. The older version was deleting the token if there were issues connecting to the Spotify Auth servers (e.g. temporary conditions). The latest release will only delete the token if the Spotify auth server returns an InvalidGrant exception (e.g. a permanent condition relating to the refresh token).

1 Like

I thought regenerating the token would be easy, having already done the procedure a few months ago, but now I can’t. I thought I’d just rerun the script, because everything related to the python part should work, but no, it gives me an error and the script doesn’t work. I’ve tried the whole procedure again from the beginning, but to no avail. Now I’ll try chat gpt if it helps me follow the guide.