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

Error while setting up platform alexa_media - KeyError: 'csrf' #258

Closed
eifinger opened this issue Jul 25, 2019 · 57 comments · Fixed by #267
Closed

Error while setting up platform alexa_media - KeyError: 'csrf' #258

eifinger opened this issue Jul 25, 2019 · 57 comments · Fixed by #267
Labels
alexapy Issue relates to the API bug Something isn't working

Comments

@eifinger
Copy link

eifinger commented Jul 25, 2019

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.

@alandtse
Copy link
Owner

Thanks for reporting. I suspect Amazon changed something with the login. Will have to look into it. Unfortunately I'm traveling for a couple weeks so can't do much till I'm back.
You have total loss of functionality right?

@eifinger
Copy link
Author

Unfortunately yes, total loss.

@tlpbu
Copy link

tlpbu commented Jul 27, 2019

Have the same issue on hassio as well with latest HA.

@anrada73
Copy link

same problem.

@jmutnick
Copy link

same problem here

@jfautley
Copy link

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... 🤷‍♂

@alandtse
Copy link
Owner

alandtse commented Jul 27, 2019

Thanks @jfautley . I'm currently traveling abroad until August 2 but should be able to make the fix when I'm back. @keatontaylor any chance you can test/implement before then?

@alandtse alandtse added alexapy Issue relates to the API bug Something isn't working labels Jul 27, 2019
@jmutnick
Copy link

@alandtse, I downloaded the new file and it worked for me! Thanks @jfautley

@genem2
Copy link

genem2 commented Jul 28, 2019

@jfautley , Thank you!
Alexa can get back to keeping my kids on task!

@erichter2018
Copy link

Not fully understanding how to install this... Should I completely uninstall the original plugin and install this instead? or just install it over it?

@jkpe
Copy link

jkpe commented Jul 28, 2019

@jfautley thank you
I replaced the following directory with the files here and restarted but am still getting the same error mentioned in the first post, what am I doing wrong?

homeassistant@homeassistant:~$ locate alexalogin.py
/srv/homeassistant/lib/python3.6/site-packages/alexapy/alexalogin.py

@Ryanmt-Deep3
Copy link

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

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

@jkpe
Copy link

jkpe commented Jul 28, 2019

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

@Xaos1990
Copy link

Its working again, thanks @jfautley

@erichter2018
Copy link

I'm sorry for being a noob but could someone just give me quick pointers about how to install this? I installed the original media player via HACS on Hassio, but I'm not sure how to upgrade/replace alexapy.

@fogarty
Copy link

fogarty commented Jul 28, 2019

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 ;-)

@petestanley112

This comment has been minimized.

@yinkastihl

This comment has been minimized.

@yinkastihl

This comment has been minimized.

@petestanley112

This comment has been minimized.

@yinkastihl

This comment has been minimized.

@petestanley112

This comment has been minimized.

@yinkastihl

This comment has been minimized.

@petestanley112

This comment has been minimized.

@yinkastihl

This comment has been minimized.

@alandtse alandtse pinned this issue Jul 28, 2019
@mrnix1001
Copy link

@jfautley This fixed it for me as well! Thank you!

@petestanley112

This comment has been minimized.

@petestanley112

This comment has been minimized.

@3ative

This comment has been minimized.

@mattnewham
Copy link

OK, so ran the shell script, all fine (I had removed the configurator plugin as I use git to manage my config files).

There is no pickle file in my config dir though. Only one for my Ring doorbell. AlexaPY was working fine until the other day when this change became apparent, so I've already set up the connection to Amazon, so confused as to why I dont have a .pickle file?

@petestanley112

This comment has been minimized.

@3ative
Copy link

3ative commented Aug 1, 2019

@petestanley112
You mentioned: "Took me a few trays to get it working."

@mattnewham
Did you get it working again?

@mattnewham
Copy link

@3ative yep - not sure how though. I just restarted my instance and its working again now. I guess I was still properly authenticated because I didnt have to do any authentication steps, just started working when I put the new alexapy in the config dir. Should it not really go into deps though? I dont really like it in the root of the config dir

@3ative
Copy link

3ative commented Aug 1, 2019

@mattnewham
Cool, glad to hear it.
Same here, I didn't need any re-authentication either - even after deleting the .pickle file.
I know what you mean, my OCD hates the new folders/files in the Root too. :)

@leonka93
Copy link

leonka93 commented Aug 2, 2019

Hi I followed your instructions, but continue to give me the same problem. in config the alexapy folder appeared, should I put it under custom_component? thanks i'm a noob.

@petestanley112
Copy link

petestanley112 commented Aug 2, 2019

@3ative
God knows how to send messsges@ some one.

But yeah took a few trays to get it working del picke reinstalled the plugin but this

pip3 install --target /config --no-dependencies git+https://gitlab.com/jfautley/alexapy

Got it working for me.. so thanks fogarty....

Tts is all that works for me. But that's all I want so yeah... oh and I had to re authent every time I del .pickle

@3ative

This comment has been minimized.

@petestanley112

This comment has been minimized.

@3ative

This comment has been minimized.

@3ative
Copy link

3ative commented Aug 2, 2019

@leonka93
Running the script put these two folders in my Config folder

Capture
-- I haven't and didn't need to move them.
Also, have you deleted the .pickle file and re-started HA?

@mattnewham
Copy link

mattnewham commented Aug 3, 2019 via email

@leonka93
Copy link

leonka93 commented Aug 3, 2019

@ 3ative@mattnewham yes I deleted the pickle file and restart HA. but after the reboot the pickle file continues to appear, and it does not find me nor the Alexa speakers associated with my account asks me to verify it

@leonka93
Copy link

leonka93 commented Aug 3, 2019

I installed hassio on Raspberry Pi 3+.The URL is amazon.it thanks to all

@3ative
Copy link

3ative commented Aug 3, 2019

@leonka93
That .pickle file [should] return - post restart.
Not sure about your other issues, I've not had any trouble with my setup, soz I can't help futher.

@alandtse

This comment has been minimized.

@leonka93
Copy link

leonka93 commented Aug 3, 2019

after a few reboots, finally work, the pickle file continues to appear but does not create problems for music playback. I do some tests for notifications etc. I'll update you. thanks

@keatontaylor
Copy link
Collaborator

keatontaylor commented Aug 3, 2019

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... 🤷‍♂

@jfautley Please make a PR on the gitlab page.

@alandtse
Copy link
Owner

alandtse commented Aug 3, 2019

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

@alandtse
Copy link
Owner

alandtse commented Aug 4, 2019

As an update, there's a new OTP page where you can then select voice or SMS which we need to handle particularly if you have multiple devices. Looking at a solution now.

@rjbrookes
Copy link

Hi I am new to all this and have very limited knowledge. Trying to get the Alexa devices shown on media players, when I open the configurator - execute shell command and copy the above mentioned pip3 line it fails to update... I get this:

Command executed: pip3 install --target /config --no-dependencies git+https://gitlab.com/jfautley/alexapy: 1
Collecting git+https://gitlab.com/jfautley/alexapy
Cloning https://gitlab.com/jfautley/alexapy to /tmp/pip-req-build-tt6qrcya
Running command git clone -q https://gitlab.com/jfautley/alexapy /tmp/pip-req-build-tt6qrcya
fatal: unable to access 'https://gitlab.com/jfautley/alexapy/': Could not resolve host: gitlab.com
ERROR: Command "git clone -q https://gitlab.com/jfautley/alexapy /tmp/pip-req-build-tt6qrcya" failed with error code 128 in None

any ideas... spent all day trying to get Alexa to work!

thanks in advanced.

@DrBeaker73
Copy link

DrBeaker73 commented Aug 5, 2019

I dont normally post, but I have to say a HUGE thank you to @jfautley for finding this fix and to @fogarty for making the fix easy. I've spread the word where I can!

I have spent hours troubleshooting this today and long into the evening, but now. Rolling.

@cubilon
Copy link

cubilon commented Aug 6, 2019

Thanks to @fogarty for the solution! Not clear to me how to fix it on Home Assistant on wheels@Buster.

pip3 install --target /config --no-dependencies git+https://gitlab.com/jfautley/alexapy

This is the command for HASSIO, but how to solve on buster?

@Sk4zz
Copy link

Sk4zz commented Aug 7, 2019

Also thanks to @fogarty for the fix. I'm running hassbian and installed the fix by changing to homeassistant's virtualenv:

source /srv/homeassistant/bin/activate

the path might be different, if you installed homeassistant manually on a buster system. And then installed the fix to my local homeassistant/ via

pip3 install --target /home/homeassistant/.homeassistant/ --no-dependencies git+https://gitlab.com/jfautley/alexapy

You might need to run this command as root if your user hasn't write access to the homeassistant's config dir.

After applying this patch, I have media_player instances for my echos available. Unfortunately, they don't work and my journal shows approximately every 10secs the following error messages.

Aug 06 21:47:37 hassbian hass[3408]: 2019-08-06 21:47:37 ERROR (MainThread) homeassistant.helpers.entity] Update for media_player.this_device fails
Aug 06 21:47:37 hassbian hass[3408]: Traceback (most recent call last):
Aug 06 21:47:37 hassbian hass[3408]:   File "/srv/homeassistant/lib/python3.7/site-packages/homeassistant/helpers/entity.py", line 221, in async_update_ha_state
Aug 06 21:47:37 hassbian hass[3408]:     await self.async_device_update()
Aug 06 21:47:37 hassbian hass[3408]:   File "/srv/homeassistant/lib/python3.7/site-packages/homeassistant/helpers/entity.py", line 380, in async_device_update
Aug 06 21:47:37 hassbian hass[3408]:     await self.hass.async_add_executor_job(self.update)
Aug 06 21:47:37 hassbian hass[3408]:   File "/usr/lib/python3.7/concurrent/futures/thread.py", line 57, in run
Aug 06 21:47:37 hassbian hass[3408]:     result = self.fn(*self.args, **self.kwargs)
Aug 06 21:47:37 hassbian hass[3408]:   File "/home/homeassistant/.homeassistant/custom_components/alexa_media/media_player.py", line 428, in update
Aug 06 21:47:37 hassbian hass[3408]:     no_throttle=True)
Aug 06 21:47:37 hassbian hass[3408]:   File "/srv/homeassistant/lib/python3.7/site-packages/homeassistant/util/__init__.py", line 224, in wrapper
Aug 06 21:47:37 hassbian hass[3408]:     result = method(*args, **kwargs)
Aug 06 21:47:37 hassbian hass[3408]:   File "/srv/homeassistant/lib/python3.7/site-packages/homeassistant/util/__init__.py", line 224, in wrapper
Aug 06 21:47:37 hassbian hass[3408]:     result = method(*args, **kwargs)
Aug 06 21:47:37 hassbian hass[3408]:   File "/home/homeassistant/.homeassistant/custom_components/alexa_media/media_player.py", line 256, in refresh
Aug 06 21:47:37 hassbian hass[3408]:     self._last_called = self._get_last_called()
Aug 06 21:47:37 hassbian hass[3408]:   File "/home/homeassistant/.homeassistant/custom_components/alexa_media/media_player.py", line 357, in _get_last_called
Aug 06 21:47:37 hassbian hass[3408]:     ['serialNumber']))
Aug 06 21:47:37 hassbian hass[3408]: TypeError: 'NoneType' object is not subscriptable

I would appreciate it, if you could point me in the right direction.

Edit: I just realized the update in the dev branch by @alandtse and I will try it out later today.

Edit 2: Nevermind. I changed the server from amazon.com to the local amazon.de and now it seems to work fine!

@alandtse alandtse unpinned this issue Aug 8, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
alexapy Issue relates to the API bug Something isn't working
Projects
None yet
Development

Successfully merging a pull request may close this issue.