Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

homeassistant.exceptions.HomeAssistantError: Could not get spotify token #319

Closed
umutcelebi opened this issue Apr 8, 2022 · 27 comments
Closed

Comments

@umutcelebi
Copy link

Deze fout is ontstaan door een aangepaste integratie.

Logger: homeassistant
Source: custom_components/spotcast/spotcast_controller.py:151
Integration: Spotcast (documentation, issues)
First occurred: 18:09:37 (1 occurrences)
Last logged: 18:09:37

Error doing job: Task exception was never retrieved
Traceback (most recent call last):
File "/config/custom_components/spotcast/spotcast_controller.py", line 145, in get_spotify_token
self._access_token, self._token_expires = st.start_session(
File "/usr/local/lib/python3.9/site-packages/spotify_token.py", line 18, in start_session
response = session.get("https://open.spotify.com/get_access_token?reason=transport&productType=web_player",
File "/usr/local/lib/python3.9/site-packages/requests/sessions.py", line 542, in get
return self.request('GET', url, **kwargs)
File "/usr/local/lib/python3.9/site-packages/requests/sessions.py", line 529, in request
resp = self.send(prep, **send_kwargs)
File "/usr/local/lib/python3.9/site-packages/requests/sessions.py", line 667, in send
history = [resp for resp in gen]
File "/usr/local/lib/python3.9/site-packages/requests/sessions.py", line 667, in
history = [resp for resp in gen]
File "/usr/local/lib/python3.9/site-packages/requests/sessions.py", line 166, in resolve_redirects
raise TooManyRedirects('Exceeded {} redirects.'.format(self.max_redirects), response=resp)
requests.exceptions.TooManyRedirects: Exceeded 30 redirects.

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
File "/config/custom_components/spotcast/helpers.py", line 91, in run
return await loop.run_in_executor(executor, pfunc)
File "/usr/local/lib/python3.9/concurrent/futures/thread.py", line 58, in run
result = self.fn(*self.args, **self.kwargs)
File "/config/custom_components/spotcast/init.py", line 98, in get_devices
client = spotcast_controller.get_spotify_client(account)
File "/config/custom_components/spotcast/spotcast_controller.py", line 179, in get_spotify_client
return spotipy.Spotify(auth=self.get_token_instance(account).access_token)
File "/config/custom_components/spotcast/spotcast_controller.py", line 139, in access_token
self.ensure_token_valid()
File "/config/custom_components/spotcast/spotcast_controller.py", line 135, in ensure_token_valid
self.get_spotify_token()
File "/config/custom_components/spotcast/spotcast_controller.py", line 151, in get_spotify_token
raise HomeAssistantError("Could not get spotify token")
homeassistant.exceptions.HomeAssistantError: Could not get spotify token

@umutcelebi
Copy link
Author

Hi,

Can you help me with this?

@r11a
Copy link

r11a commented Apr 8, 2022

same issue

This error originated from a custom integration.

Logger: homeassistant
Source: custom_components/spotcast/spotcast_controller.py:151
Integration: Spotcast (documentation, issues)
First occurred: 21:48:40 (2 occurrences)
Last logged: 21:48:57

Error doing job: Task exception was never retrieved
Traceback (most recent call last):
File "/config/custom_components/spotcast/spotcast_controller.py", line 145, in get_spotify_token
self._access_token, self._token_expires = st.start_session(

File "/usr/local/lib/python3.9/site-packages/requests/sessions.py", line 542, in get
return self.request('GET', url, **kwargs)
File "/usr/local/lib/python3.9/site-packages/requests/sessions.py", line 529, in request
resp = self.send(prep, **send_kwargs)
File "/usr/local/lib/python3.9/site-packages/requests/sessions.py", line 667, in send
history = [resp for resp in gen]
File "/usr/local/lib/python3.9/site-packages/requests/sessions.py", line 667, in
history = [resp for resp in gen]
File "/usr/local/lib/python3.9/site-packages/requests/sessions.py", line 166, in resolve_redirects
raise TooManyRedirects('Exceeded {} redirects.'.format(self.max_redirects), response=resp)
requests.exceptions.TooManyRedirects: Exceeded 30 redirects.

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
File "/config/custom_components/spotcast/helpers.py", line 91, in run
return await loop.run_in_executor(executor, pfunc)
File "/usr/local/lib/python3.9/concurrent/futures/thread.py", line 58, in run
result = self.fn(*self.args, **self.kwargs)
File "/config/custom_components/spotcast/init.py", line 98, in get_devices
client = spotcast_controller.get_spotify_client(account)
File "/config/custom_components/spotcast/spotcast_controller.py", line 179, in get_spotify_client
return spotipy.Spotify(auth=self.get_token_instance(account).access_token)
File "/config/custom_components/spotcast/spotcast_controller.py", line 139, in access_token
self.ensure_token_valid()
File "/config/custom_components/spotcast/spotcast_controller.py", line 135, in ensure_token_valid
self.get_spotify_token()
File "/config/custom_components/spotcast/spotcast_controller.py", line 151, in get_spotify_token
raise HomeAssistantError("Could not get spotify token")
homeassistant.exceptions.HomeAssistantError: Could not get spotify token

@nrdeworks
Copy link

Same issue. Tried re-pulling the cookies and updating config but didn't help.

@r11a
Copy link

r11a commented Apr 9, 2022

Update :
Operating System
Home Assistant OS 8.0.rc1

This may be the cause of the issue?

@umutcelebi
Copy link
Author

Update :
Operating System
Home Assistant OS 8.0.rc1

This may be the cause of the issue?

Nope, mine says i am on 7.6

@hmmbob
Copy link
Contributor

hmmbob commented Apr 9, 2022

Not seeing this on my install. Are you all on 2022.4 by any chance? I'm still on 2022.3

@umutcelebi
Copy link
Author

Not seeing this on my install. Are you all on 2022.4 by any chance? I'm still on 2022.3

Yes i am on 2022.4 but had this on 2022.3 aswell. I have updated to 2022.4 before i posted this to see if it would fix my issue.

@nrdeworks
Copy link

Not seeing this on my install. Are you all on 2022.4 by any chance? I'm still on 2022.3

Yes i am on 2022.4 but had this on 2022.3 aswell. I have updated to 2022.4 before i posted this to see if it would fix my issue.

I did the same as updating has solved issues in the past. Were you able to solve this?

@umutcelebi
Copy link
Author

Not seeing this on my install. Are you all on 2022.4 by any chance? I'm still on 2022.3

Yes i am on 2022.4 but had this on 2022.3 aswell. I have updated to 2022.4 before i posted this to see if it would fix my issue.

I did the same as updating has solved issues in the past. Were you able to solve this?

Unfortunately, this did not solve my issue.

@nrdeworks
Copy link

The update to HassOS this morning and supervisor fixed it for me.

@fcusson
Copy link
Collaborator

fcusson commented Apr 11, 2022

looks like a change in spotify token management. I'll look into it. Maybe a change from Spotify API

@fcusson
Copy link
Collaborator

fcusson commented Apr 12, 2022

After testing I am unable to replicate the issue. At first glance, it would be problem with dependency resolving. Since all people that seem to have the problem run HassOS, it could be a bad config in the update that wiped the dependencies, i.e. Spotify_Token in our case.

The problem is that when we try to call spotify_token (the script we use to get the spotify_token) we raise an error if anything fails.

To be able to diagnose further I would need more testing data from someone that has the issue and would be willing to test a modified version of Spotcast that deliberately doesn't handle error for that portion.

If anyone is confortable to test that simply respond @ing me in the message.

@r11a
Copy link

r11a commented Apr 12, 2022

Hi,

any updates?

Thanks

@umutcelebi
Copy link
Author

After testing I am unable to replicate the issue. At first glance, it would be problem with dependency resolving. Since all people that seem to have the problem run HassOS, it could be a bad config in the update that wiped the dependencies, i.e. Spotify_Token in our case.

The problem is that when we try to call spotify_token (the script we use to get the spotify_token) we raise an error if anything fails.

To be able to diagnose further I would need more testing data from someone that has the issue and would be willing to test a modified version of Spotcast that deliberately doesn't handle error for that portion.

If anyone is confortable to test that simply respond @ing me in the message.

Sure, i can test.

@r11a
Copy link

r11a commented Apr 12, 2022

After testing I am unable to replicate the issue. At first glance, it would be problem with dependency resolving. Since all people that seem to have the problem run HassOS, it could be a bad config in the update that wiped the dependencies, i.e. Spotify_Token in our case.
The problem is that when we try to call spotify_token (the script we use to get the spotify_token) we raise an error if anything fails.
To be able to diagnose further I would need more testing data from someone that has the issue and would be willing to test a modified version of Spotcast that deliberately doesn't handle error for that portion.
If anyone is confortable to test that simply respond @ing me in the message.

Sure, i can test.

@fcusson
Copy link
Collaborator

fcusson commented Apr 13, 2022

Hi @r11a , @umutcelebi, here is the change to make. I don't want to push an update because reinstalling Spotcast might resolve the issue, but I want to make sure we can get to the root of the problem first.

You would need to modify the spotcast_controller.py file it should be in config/custom_components/spotcast/spocast_controller.py

The change to make is from line 150 to 152:

except Exception as e:  # noqa: E722
            _LOGGER.error(e)
            raise HomeAssistantError("Could not get spotify token")

Basically:

  • add Exception as e after except
  • add a new code line _LOGGER.error(e) right after

This should give us more details on what is going on

@r11a
Copy link

r11a commented Apr 13, 2022

Hi @r11a , @umutcelebi, here is the change to make. I don't want to push an update because reinstalling Spotcast might resolve the issue, but I want to make sure we can get to the root of the problem first.

You would need to modify the spotcast_controller.py file it should be in config/custom_components/spotcast/spocast_controller.py

The change to make is from line 150 to 152:

except Exception as e:  # noqa: E722
            _LOGGER.error(e)
            raise HomeAssistantError("Could not get spotify token")

Basically:

  • add Exception as e after except
  • add a new code line _LOGGER.error(e) right after

This should give us more details on what is going on

this is what I'm getting:

This error originated from a custom integration.

Logger: custom_components.spotcast.spotcast_controller
Source: custom_components/spotcast/spotcast_controller.py:151
Integration: Spotcast (documentation, issues)
First occurred: 16:25:45 (1 occurrences)
Last logged: 16:25:45

Exceeded 30 redirects.

@fcusson
Copy link
Collaborator

fcusson commented Apr 13, 2022

@r11a ok thank you, so there seems to be a problem with how your Spotify app integration is setup. Spotcast is trying to acquire a token from the Spotify integration but is getting in a redirect loop.

  1. Is your Spotify integration working correctly?
  2. How did you setup the Spotify integration, with nabu casa or with the spotify developper app?

@r11a
Copy link

r11a commented Apr 13, 2022

@fcusson Thank you for your help :)

  1. Yes, the Spotify integration is working fine.
  2. I'm using the native home assistant integration developper app.

@umutcelebi
Copy link
Author

Logger: custom_components.spotcast.spotcast_controller
Source: custom_components/spotcast/spotcast_controller.py:151
Integration: Spotcast (documentation, issues)
First occurred: 09:38:07 (1 occurrences)
Last logged: 09:38:07

Exceeded 30 redirects.

Logger: homeassistant.components.websocket_api.http.connection
Source: custom_components/spotcast/spotcast_controller.py:152
Integration: Home Assistant WebSocket API (documentation, issues)
First occurred: 09:38:07 (1 occurrences)
Last logged: 09:38:07

[140291256728736] Could not get spotify token
Traceback (most recent call last):
File "/config/custom_components/spotcast/spotcast_controller.py", line 145, in get_spotify_token
self._access_token, self._token_expires = st.start_session(
File "/usr/local/lib/python3.9/site-packages/spotify_token.py", line 18, in start_session
response = session.get("https://open.spotify.com/get_access_token?reason=transport&productType=web_player",
File "/usr/local/lib/python3.9/site-packages/requests/sessions.py", line 542, in get
return self.request('GET', url, **kwargs)
File "/usr/local/lib/python3.9/site-packages/requests/sessions.py", line 529, in request
resp = self.send(prep, **send_kwargs)
File "/usr/local/lib/python3.9/site-packages/requests/sessions.py", line 667, in send
history = [resp for resp in gen]
File "/usr/local/lib/python3.9/site-packages/requests/sessions.py", line 667, in
history = [resp for resp in gen]
File "/usr/local/lib/python3.9/site-packages/requests/sessions.py", line 166, in resolve_redirects
raise TooManyRedirects('Exceeded {} redirects.'.format(self.max_redirects), response=resp)
requests.exceptions.TooManyRedirects: Exceeded 30 redirects.

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
File "/usr/src/homeassistant/homeassistant/components/websocket_api/commands.py", line 193, in handle_call_service
await hass.services.async_call(
File "/usr/src/homeassistant/homeassistant/core.py", line 1634, in async_call
task.result()
File "/usr/src/homeassistant/homeassistant/core.py", line 1671, in _execute_service
await cast(Callable[[ServiceCall], Awaitable[None]], handler.job.target)(
File "/usr/src/homeassistant/homeassistant/components/script/init.py", line 282, in service_handler
await script_entity.async_turn_on(
File "/usr/src/homeassistant/homeassistant/components/script/init.py", line 386, in async_turn_on
await coro
File "/usr/src/homeassistant/homeassistant/components/script/init.py", line 412, in _async_run
return await self.script.async_run(script_vars, context)
File "/usr/src/homeassistant/homeassistant/helpers/script.py", line 1275, in async_run
await asyncio.shield(run.async_run())
File "/usr/src/homeassistant/homeassistant/helpers/script.py", line 359, in async_run
await self._async_step(log_exceptions=False)
File "/usr/src/homeassistant/homeassistant/helpers/script.py", line 379, in _async_step
await getattr(self, handler)()
File "/usr/src/homeassistant/homeassistant/helpers/script.py", line 582, in _async_call_service_step
await service_task
File "/usr/src/homeassistant/homeassistant/core.py", line 1634, in async_call
task.result()
File "/usr/src/homeassistant/homeassistant/core.py", line 1677, in _execute_service
await self._hass.async_add_executor_job(
File "/usr/local/lib/python3.9/concurrent/futures/thread.py", line 58, in run
result = self.fn(*self.args, **self.kwargs)
File "/config/custom_components/spotcast/init.py", line 170, in start_casting
client = spotcast_controller.get_spotify_client(account)
File "/config/custom_components/spotcast/spotcast_controller.py", line 180, in get_spotify_client
return spotipy.Spotify(auth=self.get_token_instance(account).access_token)
File "/config/custom_components/spotcast/spotcast_controller.py", line 139, in access_token
self.ensure_token_valid()
File "/config/custom_components/spotcast/spotcast_controller.py", line 135, in ensure_token_valid
self.get_spotify_token()
File "/config/custom_components/spotcast/spotcast_controller.py", line 152, in get_spotify_token
raise HomeAssistantError("Could not get spotify token")
homeassistant.exceptions.HomeAssistantError: Could not get spotify token

Is your Spotify integration working correctly? Via the media browser this does not work either. Same error.
How did you setup the Spotify integration, with nabu casa or with the spotify developper app? Via the developer app.

@Gramatus
Copy link
Contributor

I have (had) the same issue. I did some testing by cloning the "spotify_token" package and adding some additional debugging to the requests. See attached debug.log, it seems the requests gets a 302 response pointing to the same url, most relevant bit below:

send: 
	GET /get_access_token?reason=transport&productType=web_player HTTP/1.1
	Host: open.spotify.com
reply:
	'HTTP/1.1 302 Found'
	header: location: /get_access_token

I then tried to use new values for sp_dc and sp_key, and got a 200.

I then tried updating my sp_dc and sp_key in HA and restarted, and now it seems to work.

I think the issue for me at least must have been expired values for sp_dc and sp_key (if the expiration is one year, that might fit well for me). I saw someone else did not fix this by getting new values, but perhaps try again just to be absolutely certain it might not be the same problem? Also, if so - perhaps an error of "too many redirects" could be caught and push a more detailed error to HA, e.g. "Could not get spotify token - might be expired values for sp_dc and sp_key, try getting new values"?

@fcusson
Copy link
Collaborator

fcusson commented Apr 14, 2022

Wow thank you @Gramatus I was still debugging from our end at this point.

We could filter the error type and send a more specific message based on that. I'll work on PR for that.

In the meantime, @r11a @umutcelebi, can you confirm updating your sp_dc and sp_key resolves the issue?

@r11a
Copy link

r11a commented Apr 14, 2022

@fcusson unfraternally no :(

attached new log file after spotcast reinstall and new sp_dc and sp_key

This error originated from a custom integration.

Logger: homeassistant
Source: custom_components/spotcast/spotcast_controller.py:151
Integration: Spotcast (documentation, issues)
First occurred: 15:24:37 (2 occurrences)
Last logged: 15:27:34

Error doing job: Task exception was never retrieved
Traceback (most recent call last):
File "/config/custom_components/spotcast/spotcast_controller.py", line 145, in get_spotify_token
self._access_token, self._token_expires = st.start_session(
File "/usr/local/lib/python3.9/site-packages/spotify_token.py", line 18, in start_session
response = session.get("https://open.spotify.com/get_access_token?reason=transport&productType=web_player",
File "/usr/local/lib/python3.9/site-packages/requests/sessions.py", line 542, in get
return self.request('GET', url, **kwargs)
File "/usr/local/lib/python3.9/site-packages/requests/sessions.py", line 529, in request
resp = self.send(prep, **send_kwargs)
File "/usr/local/lib/python3.9/site-packages/requests/sessions.py", line 667, in send
history = [resp for resp in gen]
File "/usr/local/lib/python3.9/site-packages/requests/sessions.py", line 667, in
history = [resp for resp in gen]
File "/usr/local/lib/python3.9/site-packages/requests/sessions.py", line 166, in resolve_redirects
raise TooManyRedirects('Exceeded {} redirects.'.format(self.max_redirects), response=resp)
requests.exceptions.TooManyRedirects: Exceeded 30 redirects.

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
File "/config/custom_components/spotcast/helpers.py", line 91, in run
return await loop.run_in_executor(executor, pfunc)
File "/usr/local/lib/python3.9/concurrent/futures/thread.py", line 58, in run
result = self.fn(*self.args, **self.kwargs)
File "/config/custom_components/spotcast/init.py", line 98, in get_devices
client = spotcast_controller.get_spotify_client(account)
File "/config/custom_components/spotcast/spotcast_controller.py", line 179, in get_spotify_client
return spotipy.Spotify(auth=self.get_token_instance(account).access_token)
File "/config/custom_components/spotcast/spotcast_controller.py", line 139, in access_token
self.ensure_token_valid()
File "/config/custom_components/spotcast/spotcast_controller.py", line 135, in ensure_token_valid
self.get_spotify_token()
File "/config/custom_components/spotcast/spotcast_controller.py", line 151, in get_spotify_token
raise HomeAssistantError("Could not get spotify token")
homeassistant.exceptions.HomeAssistantError: Could not get spotify token

@r11a
Copy link

r11a commented Apr 14, 2022

@fcusson I have tried to get new sp_dc and sp_key again and now it works :)

@umutcelebi
Copy link
Author

Wow thank you @Gramatus I was still debugging from our end at this point.

We could filter the error type and send a more specific message based on that. I'll work on PR for that.

In the meantime, @r11a @umutcelebi, can you confirm updating your sp_dc and sp_key resolves the issue?

Yes, i can confirm this is working. I have checked the new sp_dc and sp_key via the settings and pasted the new values. After restarting the instance all is working fine. Thanks!

@fcusson
Copy link
Collaborator

fcusson commented Apr 14, 2022

with the information we have I made a change to the get_spotify_token method:

def get_spotify_token(self) -> tuple[str, int]:
        try:
            self._access_token, self._token_expires = st.start_session(
                self.sp_dc, self.sp_key
            )
            expires = self._token_expires - int(time.time())
            return self._access_token, expires
        except TooManyRedirects:
            _LOGGER.error("Could not get spotify token. sp_dc and sp_key could be expired. Please update in config.")
            raise HomeAssistantError("Expired sp_dc, sp_key")
        except:  # noqa: E722
            raise HomeAssistantError("Could not get spotify token.")

Now the error message will clearly point toward an expired token for this particular behavior.

@r11a
Copy link

r11a commented Apr 15, 2022

@fcusson @hmmbob @Gramatus @umutcelebi Thank you very much!

@fcusson fcusson closed this as completed Apr 17, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

6 participants