alexa_media_player: Error while setting up platform alexa_media - KeyError: 'csrf'

Describe the bug TTS was not working anymore. I suspected an old cookie etc, deleted the .pickle file and restarted homeassistant which always worked. Configuration with 2FA seemed to work. Afterwards I started to see the following errors in the log:

Error while setting up platform alexa_media
Traceback (most recent call last):
  File "/usr/src/app/homeassistant/helpers/entity_platform.py", line 126, in _async_setup_platform
    SLOW_SETUP_MAX_WAIT)
  File "/usr/local/lib/python3.7/asyncio/tasks.py", line 416, in wait_for
    return fut.result()
  File "/usr/local/lib/python3.7/concurrent/futures/thread.py", line 57, in run
    result = self.fn(*self.args, **self.kwargs)
  File "/config/custom_components/alexa_media/alarm_control_panel.py", line 35, in setup_platform
    hass)  \
  File "/config/custom_components/alexa_media/alarm_control_panel.py", line 73, in __init__
    self.alexa_api = AlexaAPI(self, login)
  File "/usr/local/lib/python3.7/site-packages/alexapy/alexaapi.py", line 51, in __init__
    csrf = self._session.cookies.get_dict()['csrf']
KeyError: 'csrf'

To Reproduce Steps to reproduce the behavior:

  1. Delete .pickle
  2. Restart HA
  3. Configure (Login)
  4. Error appear

Expected behavior Media Players should appear along the functionality

Screenshots If applicable, add screenshots to help explain your problem.

System details

  • Home-assistant (version): 0.95.4 (docker)
  • Hassio (Yes/No): No
  • alexa_media (version from const.py or HA startup): v1.3.1
  • alexapy (version from pip show alexapy or HA startup): 0.7.0

Additional context Add any other context about the problem here.

About this issue

  • Original URL
  • State: closed
  • Created 5 years ago
  • Reactions: 7
  • Comments: 57 (5 by maintainers)

Most upvoted comments

First a huge thx @jfautley now works again!

@erichter2018 the simplest way would be to go to the “Configurator” then clic on settings and on “Execute shell Command” Paste here the command given by Jon: pip3 install --target /config --no-dependencies git+https://gitlab.com/jfautley/alexapy

Then don’t forget to delete your .pickle file in your /config directory and reboot 😉

It looks like Amazon have tweaked the API, and the AlexaPy library no longer gets the CSRF token from the login URL (and there’s no error checking in the init function there).

Given the upstream maintainers lack of time, I’ve forked the AlexaPy repo and updated the site - I’d raise a PR but it’s a one line change and I’ve not done anything to actually test it - but it works for me.

You can find the updated repo at https://gitlab.com/jfautley/alexapy - I’ve tested this against an Amazon UK account using Hass.IO.

To install (on Hass.IO), SSH into your instance and run: pip3 install --target /config --no-dependencies git+https://gitlab.com/jfautley/alexapy

For other Home-Assistant installs, you should be able to use --target /path/to/config/deps but for some reason the PYTHONPATH on Hass.IO is messed up… 🤷‍♂

@keatontaylor I’m actually reviewing if we can make a more robust fix so we can feed multiple urls to iterate through.

Ensure you delete the .pickle file out of your config folder.

Just tested - fix works fine for me too. Thanks @jfautley

Ah yes, working now, thank you