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

MyUSPS Login fails #9

Closed
aneisch opened this issue Oct 30, 2017 · 81 comments
Closed

MyUSPS Login fails #9

aneisch opened this issue Oct 30, 2017 · 81 comments

Comments

@aneisch
Copy link

aneisch commented Oct 30, 2017

python
>>> session = myusps.get_session("XXXXXXX", "XXXXXXXXX")
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/opt/homeassistant/deps/lib/python3.5/site-packages/myusps/__init__.py", line 296, in get_session
    _login(session)
  File "/opt/homeassistant/deps/lib/python3.5/site-packages/myusps/__init__.py", line 169, in _login
    raise USPSError('authentication failed')
myusps.USPSError: authentication failed
python
print(data):
{'actionMessages': [], 'fieldErrors': {}, 'actionErrors': ['We’ve upgraded our Login Page.  You will need to clear your browser cache to take advantage of the features on the new page.......  We have encountered an error.  Please refresh the page and try again.']}
@Per4merKC
Copy link

I'm having this issue, too. Let me know if there is anything I can do to help.

@happyleavesaoc
Copy link
Owner

I've tried to solve this on multiple occasions, with no luck. If someone else wants to take a stab at it, please do.

@firstof9
Copy link

Maybe the hidden values need to be passed as well?

<input type="hidden" name="struts.token.name" value="token" />
<input type="hidden" name="token" value="38VCHB4UYVXSVQTTN59GYMNEXHBS0UAB" />

@happyleavesaoc
Copy link
Owner

Unfortunately that is not sufficient.

@firstof9
Copy link

When I go through step by step within the javascript this data is sent to the URL:

"struts.token.name=token&token=24DRONKEQX9HA3VR0UAM0LJ0FDQW9VAM&route=&username=&password=&newPassword=&retypeNewPassword="

not sure if this helps any.

@stjohnjohnson
Copy link

stjohnjohnson commented Dec 28, 2017

I think the problem lies within some sort of checksum system they've obfusticated here. Take a look at the login page HTML, there's a bundle function.

Here is the encoded and decoded forms (along with a translator): https://gist.github.com/stjohnjohnson/3e2d41c4473e6807d149435fd465b2d1

Note: this is going into the headers of the XHR call

@happyleavesaoc
Copy link
Owner

Yes, that was my conclusion. That value is passed in the http request header to login. I executed some of that code with nodejs, but didn't go too far down that route.

@happyleavesaoc
Copy link
Owner

I was previously able to pass a blank value, but whatever they changed now checks. The JavaScript is part of Google Tag Manager, apparently.

@stjohnjohnson
Copy link

You know, we could resolve this by using a headless browser that executes javascript. There are ones that work directly in Python: http://jeanphix.me/Ghost.py/

However, that would require HomeAssistant to include pyqt, and I'm not sure if they are up for that.

@tchellomello
Copy link

Adding myself on this to track this since I'm hitting as well.

@brianjking
Copy link

@happyleavesaoc @stjohnjohnson @tchellomello

I found some other projects that might help out in getting this working. Let me know if I can be of service. The first link seems to use Selenium to login to the Informed Delivery site and then grab the contents... source here. Full repo is below.

and possibly this... however, this requires using the USPS API and passing tracking codes in manually as far as I can tell.

Thanks!

@brianjking
Copy link

I tested out @kylefrost's today's mail code and it does login correctly. Perhaps this is a good approach to getting the same data?

@happyleavesaoc
Copy link
Owner

@brianjking I think Selenium actually executes the Javascript required to get that cookie set properly, which is why it works. We could do the same thing, I guess! It's kind of a heavyweight dependency though.

@raccettura
Copy link

I agree it's heavy, but to be honest, it's way more reliable than reverse engineering and less likely to break in the future. Since USPS scraping seems so fragile it might be the better approach looking at the bigger picture.

@brianjking
Copy link

@happyleavesaoc @raccettura Yeah, it definitely works to login as well as per my testing. Scraping has never been overly reliable and now entirely broken.

The scraping option required a lot of development/debugging from @happyleavesaoc and others which has certainly been frustrating and time consuming I'm certain. It's also one of my favorite Home Assistant components so I'd love to have it back.

I think this is the best option for moving forward until USPS releases an API, which may never happen. If nothing else, can anyone maybe help me out in getting this setup as a custom component? It would be hugely appreciated.

<3 Thanks!

@jcconnell
Copy link

I'll work on making a custom component. It'll be my first, but I'll give it a go.

@philk
Copy link

philk commented Jan 25, 2018

Selenium is a pretty heavyweight component. How about attempting to execute the JS via Python to get whatever silly data they want for the login?

https://github.com/amol-/dukpy
https://github.com/sony/v8eval
https://github.com/sqreen/PyMiniRacer

@kylefrost
Copy link

Hey! Glad todays-mail might help with y'all's issue. It's a pretty quickly written, "hey this seems like a cool idea" thing, so excuse the sloppiness. Selenium was a pain to set up at first, and I agree it may be a heavier dependency than what y'all are looking for, but it does work (obviously). I can try to help out if anyone runs into issues getting it set up.

Side note: It looks like this is being used for a home-assistant component? I've debated setting up home-assistant on my server, what are y'all using it for? Haven't decided if it is worth the effort.

@aneisch
Copy link
Author

aneisch commented Jan 25, 2018

@kylefrost: Using it to glue together all of the different smart home components I have distributed in my house. Check out my config repo https://github.com/aneisch/home-assistant-config

@brianjking
Copy link

@kylefrost Pretty much same as @aneisch said. You can check out my repo here along with an old screen recording of some of my configuration https://github.com/brianjking/homeassistant-config. It's really easy to get started with Home Assistant especially on Raspberry Pi. Hass.io seems great, however, I use the customized Raspbian distribution called Hassbian. Feel free to catch me via an issue on my Home Assistant repo or on twitter if you have any questions.

@jcconnell Awesome, I plan on trying to look into making a custom component as well. If you figure it out before I do and get it working, let me know. I'll do the same.

@philk I'm open to anything that works. However, I harnessing @kylefrost's work using Selenium is probably a great option as we know it works and should be stable.

Again, let me know if there's anything I can do to help out in testing or any research. Even if this doesn't get implemented into the USPS component I really hope to get it setup and working as a custom component.

@brianjking
Copy link

brianjking commented Jan 29, 2018

Ok, now I'm seeing a post from the Home Assistant forums with someone that worked out a different way of getting this working.

https://community.home-assistant.io/t/a-working-usps-component-solved-for-now/41012

I haven't had a chance to try it out yet, however, I might look into it.

@stjohnjohnson
Copy link

@brianjking that setup appears to scrape your email instead of their website. I'd rather go down the website scraping.

@brianjking
Copy link

I think using @kylefrost's route of logging in with Selenium is the best approach with what we have today.

@happyleavesaoc
Copy link
Owner

I'd accept a PR to this repository that adds a Selenium (or otherwise) based approach but implements the same API.

@happyleavesaoc
Copy link
Owner

@philk I tried executing the javascript with a couple of those tools - no luck. I think it relies on a browser environment to properly execute. We could isolate the key generation code, but that's tough since the javascript is obfuscated.

@brianjking
Copy link

brianjking commented Feb 11, 2018

@happyleavesaoc -- @J-CMartin has an implementation of @kylefrost's working in his HASS install. Trying to get that info so we can do a PR or at least use a custom component. Re: brianjking/homeassistant-config#125

@happyleavesaoc
Copy link
Owner

I have successfully used selenium to login. I then pass the session cookies to requests and the rest of the code proceeds as normal, unchanged.

I will test this locally for a few days, and figure out a few webdriver configuration details. If that's all successful, I'll push it for everyone interested to test.

@jcconnell
Copy link

@brianjking I think that might be your issue. Change the python-myusps version number to 1.3.2 in myusps.py and remove the init.py you put there.

@firstof9
Copy link

Confirmed working on my Pi setup w/ HASS.

@brianjking
Copy link

@firstof9 What installation method did you use on your Pi?

@jcconnell @happyleavesaoc I deleted the /srv/homeassistant/lib/python3.5/site-packages/myusps/__init__.py file and restarted Home Assistant and now I'm getting this error:

2018-02-16 14:30:40 INFO (MainThread) [homeassistant.setup] Setting up usps
2018-02-16 14:30:40 ERROR (MainThread) [homeassistant.setup] Error during setup of component usps
Traceback (most recent call last):
  File "/srv/homeassistant/lib/python3.5/site-packages/homeassistant/components/usps.py", line 50, in setup
    session = myusps.get_session(username, password,
AttributeError: module 'myusps' has no attribute 'get_session'

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/srv/homeassistant/lib/python3.5/site-packages/homeassistant/setup.py", line 148, in _async_setup_component
    component.setup, hass, processed_config)
  File "/usr/lib/python3.5/asyncio/futures.py", line 380, in __iter__
    yield self  # This tells Task to wait for completion.
  File "/usr/lib/python3.5/asyncio/tasks.py", line 304, in _wakeup
    future.result()
  File "/usr/lib/python3.5/asyncio/futures.py", line 293, in result
    raise self._exception
  File "/usr/lib/python3.5/concurrent/futures/thread.py", line 55, in run
    result = self.fn(*self.args, **self.kwargs)
  File "/srv/homeassistant/lib/python3.5/site-packages/homeassistant/components/usps.py", line 52, in setup
    except myusps.USPSError:
AttributeError: module 'myusps' has no attribute 'USPSError'
2018-02-16 14:30:40 ERROR (MainThread) [homeassistant.setup] Unable to setup dependencies of camera.usps. Setup failed for dependencies: usps
2018-02-16 14:30:40 ERROR (MainThread) [homeassistant.setup] Unable to prepare setup for platform camera.usps: Could not setup all dependencies.

@firstof9
Copy link

firstof9 commented Feb 16, 2018

@brianjking I did just as you did, but after restarting HA im now getting the same error about split :(

2018-02-16 07:46:27 ERROR (MainThread) [homeassistant.setup] Error during setup of component usps
Traceback (most recent call last):
  File "/srv/homeassistant/homeassistant_venv/lib/python3.5/site-packages/homeassistant/setup.py", line 148, in _async_setup_component
    component.setup, hass, processed_config)
  File "/usr/lib/python3.5/asyncio/futures.py", line 380, in __iter__
    yield self  # This tells Task to wait for completion.
  File "/usr/lib/python3.5/asyncio/tasks.py", line 304, in _wakeup
    future.result()
  File "/usr/lib/python3.5/asyncio/futures.py", line 293, in result
    raise self._exception
  File "/usr/lib/python3.5/concurrent/futures/thread.py", line 55, in run
    result = self.fn(*self.args, **self.kwargs)
  File "/srv/homeassistant/homeassistant_venv/lib/python3.5/site-packages/homeassistant/components/usps.py", line 46, in setup
    import myusps
  File "/srv/homeassistant/homeassistant_venv/lib/python3.5/site-packages/myusps/__init__.py", line 12, in <module>
    import requests_cache
  File "/srv/homeassistant/homeassistant_venv/lib/python3.5/site-packages/requests_cache/__init__.py", line 28, in <module>
    from .core import(
  File "/srv/homeassistant/homeassistant_venv/lib/python3.5/site-packages/requests_cache/core.py", line 21, in <module>
    ver = tuple(map(int, requests.__version__.split(".")))
AttributeError: module 'pip._vendor.requests.__version__' has no attribute 'split'

@happyleavesaoc
Copy link
Owner

@brianjking Your install is busted. I'd recommend entirely removing myusps. Just keep the version number at 1.3.2 in usps.py.

@firstof9 Try a re-install. That's an error in a dependency ... and should be unrelated to selenium/webdrivers/etc

If you want to test the module in isolation (without HASS), use this script from your hass virtual env:

import myusps
s = myusps.get_session('username', 'password', driver='phantomjs')
p = myusps.get_packages(s)
print(p)

It should print out all your packages in JSON.

@brianjking
Copy link

@happyleavesaoc

I deleted the files in the myusps directory. 1.3.2 is the required version in /srv/homeassistant/lib/python3.5/site-packages/homeassistant/components/usps.py.

2018-02-16 14:58:53 INFO (MainThread) [homeassistant.setup] Setting up usps
2018-02-16 14:58:53 ERROR (MainThread) [homeassistant.setup] Error during setup of component usps
Traceback (most recent call last):
  File "/srv/homeassistant/lib/python3.5/site-packages/homeassistant/setup.py", line 148, in _async_setup_component
    component.setup, hass, processed_config)
  File "/usr/lib/python3.5/asyncio/futures.py", line 380, in __iter__
    yield self  # This tells Task to wait for completion.
  File "/usr/lib/python3.5/asyncio/tasks.py", line 304, in _wakeup
    future.result()
  File "/usr/lib/python3.5/asyncio/futures.py", line 293, in result
    raise self._exception
  File "/usr/lib/python3.5/concurrent/futures/thread.py", line 55, in run
    result = self.fn(*self.args, **self.kwargs)
  File "/srv/homeassistant/lib/python3.5/site-packages/homeassistant/components/usps.py", line 46, in setup
    import myusps
ImportError: No module named 'myusps'
2018-02-16 14:58:53 ERROR (MainThread) [homeassistant.setup] Unable to setup dependencies of camera.usps. Setup failed for dependencies: usps
2018-02-16 14:58:53 ERROR (MainThread) [homeassistant.setup] Unable to prepare setup for platform camera.usps: Could not setup all dependencies.

@firstof9
Copy link

@happyleavesaoc Ah there we go, pip3 install --upgrade myusps==1.3.2 downgraded some other component "requests(something)" and seems to have resolved the issue.

@firstof9
Copy link

@brianjking try pip3 install --reinstall myusps==1.3.2 see if it helps
I don't use hassbian so im not sure if you need to do it from a venv or not.

@brianjking
Copy link

@firstof9 Hassbian does use a venv so inside the active venv I ran pip3 install --upgrade --force-reinstall myusps==1.3.2 and restarted Home Assistant and it's working! Thank you @happyleavesaoc and everyone else!

@n0dyjeff
Copy link

n0dyjeff commented Feb 16, 2018 via email

@mariniertje
Copy link

mariniertje commented Feb 16, 2018 via email

@firstof9
Copy link

firstof9 commented Feb 16, 2018

@n0dyjeff

wget https://github.com/fg2it/phantomjs-on-raspberry/releases/download/v2.1.1-wheezy-jessie-armv6/phantomjs_2.1.1_armhf.deb
sudo dpkg -i phantomjs_2.1.1_armhf.deb

activate your venv and run:

pip3 install --upgrade --force-reinstall myusps==1.3.2

That should do it. @brianjking or someone more fimilar with hasbbian would have to fill in how to do the venv bit if you are not sure how to.

@n0dyjeff
Copy link

n0dyjeff commented Feb 16, 2018 via email

@firstof9
Copy link

firstof9 commented Feb 16, 2018

@n0dyjeff Shoot i forgot you have to modify homeassistant/components/usps.py and change the 1.3.0 to 1.3.2 as well, otherwise I think it'll revert to 1.3.0.

@brianjking
Copy link

@n0dyjeff Yeah, be sure that you update usps.py to reflect 1.3.2 Line referenced here. Most installation methods utilize a venv so be sure you activate that venv accordingly if forcing a reinstall of myusps via pip.

@n0dyjeff
Copy link

@firstof9 @brianjking

O--KAY!!! That did it. Edited usps.py as mentioned, then did force-reinstall, and I'm logging into usps for the first time in months. Awesome!

@mariniertje
Copy link

mariniertje commented Feb 17, 2018

Hey guys,

I managed to install phantomjs by following these steps:
wget https://github.com/fg2it/phantomjs-on-raspberry/releases/download/v2.1.1-wheezy-jessie-armv6/phantomjs_2.1.1_armhf.deb sudo dpkg -i phantomjs_2.1.1_armhf.deb

Then I did a pip3 install --upgrade --force-reinstall myusps==1.3.2 in my venv.

I updated the requirements version to 1.3.2 in /srv/homeassistant/lib/python3.5/site-packages/homeassistant/components/usps.py.

When I use the following testscript, and run it with python3, I do get results.
/srv/homeassistant/lib/python3.5/site-packages/homeassistant/components/usps.py.

However, within HomeAssistant the sensors still throw the following error:

`ERROR (MainThread) [homeassistant.setup] Error during setup of component usps
Traceback (most recent call last):
File "/srv/homeassistant/lib/python3.5/site-packages/homeassistant/setup.py", line 148, in _async_setup_component
component.setup, hass, processed_config)
File "/usr/lib/python3.5/asyncio/futures.py", line 380, in iter
yield self # This tells Task to wait for completion.
File "/usr/lib/python3.5/asyncio/tasks.py", line 304, in _wakeup
future.result()
File "/usr/lib/python3.5/asyncio/futures.py", line 293, in result
raise self._exception
File "/usr/lib/python3.5/concurrent/futures/thread.py", line 55, in run
result = self.fn(*self.args, **self.kwargs)
File "/srv/homeassistant/lib/python3.5/site-packages/homeassistant/components/usps.py", line 41, in setup
conf = config[DOMAIN]
KeyError: 'usps'

2018-02-16 23:15:17 INFO (MainThread) [homeassistant.core] Bus:Handling <Event system_log_event[L]: exception=Traceback (most recent call last):
File "/srv/homeassistant/lib/python3.5/site-packages/homeassistant/setup.py", line 148, in _async_setup_component
component.setup, hass, processed_config)
File "/usr/lib/python3.5/asyncio/futures.py", line 380, in iter
yield self # This tells Task to wait for completion.
File "/usr/lib/python3.5/asyncio/tasks.py", line 304, in _wakeup
future.result()
File "/usr/lib/python3.5/asyncio/futures.py", line 293, in result
raise self._exception
File "/usr/lib/python3.5/concurrent/futures/thread.py", line 55, in run
result = self.fn(*self.args, **self.kwargs)
File "/srv/homeassistant/lib/python3.5/site-packages/homeassistant/components/usps.py", line 41, in setup
conf = config[DOMAIN]
KeyError: 'usps'

, source=components/usps.py, message=Error during setup of component usps, level=ERROR, timestamp=1518851717.1152697`

Any ideas what's causing this and how to fix it?

@firstof9
Copy link

firstof9 commented Feb 17, 2018

That script isn't a test script it is part of Home Assistant.

The errors in HA look like you didn't add the usps config bits into configuration.yaml

@mariniertje
Copy link

Thanks @firstof9 .

I wasn't aware it's suddenly critical to have usps configured in configuration.yaml
Before I had all 3 delivery services (UPS, USPS, FEDEX) configured in the sensor folder as sensors, since I like to have a minimal/clean (as possible) configuration.yaml

Moving it back to configuration.yaml indeed solved the isuse. Everything works perfect again.

@n0dyjeff
Copy link

I installed an update to phantomjs with the latest Ubuntu updates. It broke USPS. Reverting back to 2.1.1 fixed it. FYI

@brianjking
Copy link

@n0dyjeff Hmm, so running sudo apt update && sudo apt upgrade -y overwrote the PhantomJS installation? I think same thing happened to me. Next issue I guess is how to ensure PhantomJS isn't constantly getting overwritten with Ubuntu updates for RaspberryPi users.

CC: @happyleavesaoc

@happyleavesaoc
Copy link
Owner

That's up to you as the system admin. I personally recommend not installing phantomjs via apt at all, so it doesn't know to upgrade it in the first place. Download the binary, and symlink it into your PATH.

@brianjking
Copy link

@happyleavesaoc It looks like PhantomJS was already installed on it's own as I just ran a sudo hassbian-config upgrade hassbian which really just runs sudo apt update && sudo apt upgrade -y.

I had never installed PhantomJS via sudo apt install phantomjs. The install method I used was the one you instructed which is:

wget https://github.com/fg2it/phantomjs-on-raspberry/releases/download/v2.1.1-wheezy-jessie-armv6/phantomjs_2.1.1_armhf.deb
sudo dpkg -i phantomjs_2.1.1_armhf.deb

However, upon running sudo apt update && sudo apt upgrade -y today on my Pi3 I saw this:

Reading package lists... Done
Building dependency tree
Reading state information... Done
Calculating upgrade... Done
The following packages will be upgraded:
  phantomjs
1 upgraded, 0 newly installed, 0 to remove and 0 not upgraded.
Need to get 277 kB of archives.
After this operation, 58.3 MB disk space will be freed.
Get:1 http://mirrors.lug.mtu.edu/raspbian/raspbian stretch/main armhf phantomjs armhf 2.1.1+dfsg-2 [277 kB]
Fetched 277 kB in 1s (224 kB/s)
apt-listchanges: Reading changelogs...
(Reading database ... 68449 files and directories currently installed.)
Preparing to unpack .../phantomjs_2.1.1+dfsg-2_armhf.deb ...
Unpacking phantomjs (2.1.1+dfsg-2) over (2.1.1) ...
Setting up phantomjs (2.1.1+dfsg-2) ...
Processing triggers for man-db (2.7.6.1-2) ...

Upgrade complete.

CC: @firstof9 & @n0dyjeff -- Either of you have the same findings? Any idea how to disable the install of PhantomJS?

Thanks!

@jcconnell
Copy link

@brianjking Did you only install PhantomJS via pkg? Have you tried any of these techniques? https://tecadmin.net/exclude-packages-from-apt-upgrade/

@brianjking
Copy link

brianjking commented Feb 26, 2018

@jcconnell Yeah, I did try those methods. I also only ever installed the phantomjs package from the GitHub repo @happyleavesaoc instructed to use. I never installed it via apt-get install phantomjs, however, it still seems to get overwritten.

@firstof9 @n0dyjeff -- either of you have any ideas?

@Coolie1101
Copy link

Hey,
After reading through everything here, I did the following but still get a connection error.

wget https://github.com/fg2it/phantomjs-on-raspberry/releases/download/v2.1.1-wheezy-jessie-armv6/phantomjs_2.1.1_armhf.deb

sudo dpkg -i phantomjs_2.1.1_armhf.deb

pip3 install --upgrade --force-reinstall myusps==1.3.2 in my venv.

Error in log:

2018-02-27 03:22:02 ERROR (Thread-15) [homeassistant.components.usps] Could not connect to My USPS
Traceback (most recent call last):
  File "/srv/homeassistant/lib/python3.5/site-packages/homeassistant/components/usps.py", line 55, in setup
    driver=driver)
  File "/srv/homeassistant/lib/python3.5/site-packages/myusps/__init__.py", line 300, in get_session
    _login(session)
  File "/srv/homeassistant/lib/python3.5/site-packages/myusps/__init__.py", line 170, in _login
    driver = _get_driver(session.auth.driver)
  File "/srv/homeassistant/lib/python3.5/site-packages/myusps/__init__.py", line 151, in _get_driver
    raise USPSError('{} not supported'.format(driver_type))
myusps.USPSError: None not supported
2018-02-27 03:22:02 ERROR (MainThread) [homeassistant.setup] Setup failed for usps: Component failed to initialize.

@happyleavesaoc
Copy link
Owner

Hi all - please only use this issue tracker for issues specifically relating to python-myusps.

  • For Home Assistant integration, use the HASS issue tracker.
  • For dependency install issues (phantomjs etc), use an appropriate forum.

Thanks!

Repository owner locked as off-topic and limited conversation to collaborators Feb 27, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests