pyicloud icon indicating copy to clipboard operation
pyicloud copied to clipboard

Missing X-APPLE-WEBAUTH_HSA-LOGIN cookie

Open bickyb opened this issue 5 years ago • 9 comments

I can login just fine once after using the following code:

from pyicloud import PyiCloudService
api=PyiCloudService('[email protected]','mypassword')
devices=api.trusted_devices

However, when I run this code the second time after my session times out, I get a: pyicloud.exceptions.PyiCloudAPIResponseError: Missing X-APPLE-WEBAUTH_HSA-LOGIN cookie exception.

I tried this on a mac and PC and get the same cookie issue.

Any ideas on how to solve this?

bickyb avatar Jan 28 '20 01:01 bickyb

Hi @bickyb !

You have to pass a third parameter to store a cookie.

This one will permits you to authenticate without login you on your second attempt.

Tell me it is solves the problem.

Quentame avatar Feb 05 '20 19:02 Quentame

Hi, can you show what third parameter and a sample snippet of code of how to pass it?

bickyb avatar Feb 05 '20 21:02 bickyb

@bickyb you are in an open source project, lots of the time, the documentation is not complete or not updated : look at the source to understand how it works !

Sent with GitHawk

Quentame avatar Feb 05 '20 22:02 Quentame

The third parameter is cookie_directory, you should pass a string to a directory

Sent with GitHawk

Quentame avatar Feb 05 '20 22:02 Quentame

You can see a working exemple here https://github.com/home-assistant/home-assistant/tree/dev/homeassistant/components/icloud

Sent with GitHawk

Quentame avatar Feb 05 '20 22:02 Quentame

So I tried adding a third parameter:

api=PyiCloudService('[email protected]','mypassword','/Users/bickybhogal/Desktop/cookies') devices=api.trusted_devices

This creates a file in the cookies directory, but unfortunately, when I run this code the second time after my session times out, I still get a: pyicloud.exceptions.PyiCloudAPIResponseError: Missing X-APPLE-WEBAUTH_HSA-LOGIN cookie exception.

I looked in the file that was created in the cookies directory and it seems to have created the following cookies: X-APPLE-WEBAUTH-HSA-TRUST X_APPLE_WEB_KB

But it does not seem to have created a X-APPLE-WEBAUTH_HSA-LOGIN cookie.

Any thoughts?

bickyb avatar Feb 06 '20 04:02 bickyb

I meet the same situation and find that X-APPLE-WEBAUTH_HSA-LOGIN cookie is only planted within a clean login.

magus0219 avatar Apr 09 '20 14:04 magus0219

Same issue here.

just4fun20 avatar May 28 '20 14:05 just4fun20

So I did a bit of looking into this. My account has 2FA enabled.

The sample code fails:

from pyicloud import PyiCloudService
api=PyiCloudService('[email protected]','mypassword')
devices=api.trusted_devices

I get the pyicloud.exceptions.PyiCloudAPIResponseException: Missing X-APPLE-WEBAUTH-HSA-LOGIN cookie as everyone else. So I had a look at cmdline.py and tried their code:

#!/Users/andreas/pyicloud/venv/bin/python3

from pyicloud import PyiCloudService, utils
from builtins import input
from pyicloud.exceptions import PyiCloudFailedLoginException
from pprint import pprint

username = "[email protected]"
password = "supersecret"

try:
    api = PyiCloudService(username, password)

    if api.requires_2sa:
        print("\nTwo-step authentication required.", "\nYour trusted devices are:")

        devices = api.trusted_devices
        for i, device in enumerate(devices):
            print(
                "    %s: %s"
                % (i, device.get("deviceName", "SMS to %s" % device.get("phoneNumber")))
            )

            print("\nWhich device would you like to use?")
            device = int(input("(number) --> "))
            device = devices[device]
            if not api.send_verification_code(device):
                print("Failed to send verification code")
                sys.exit(1)

            print("\nPlease enter validation code")
            code = input("(string) --> ")
            if not api.validate_verification_code(device, code):
                print("Failed to verify verification code")
                sys.exit(1)

            print("")
            break
except PyiCloudFailedLoginException:
    # If they have a stored password; we just used it and
    # it did not work; let's delete it if there is one.
    if utils.password_exists_in_keyring(username):
        utils.delete_password_in_keyring(username)

        message = "Bad username or password for {username}".format(username=username)
        password = None

        failure_count += 1
        if failure_count >= 3:
            raise RuntimeError(message)

        print(message, file=sys.stderr)


print(api.trusted_devices)

Same error. Spend some time debugging this and it turns out the error is not in the login form, the trusted_devices command returns the error... Print api.devices and you're getting the right output.

ixs avatar Jul 31 '20 11:07 ixs