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

requests.exceptions.HTTPError: 401 Client Error: Unauthorized for url #235

Open
kerryshireman opened this issue Nov 25, 2024 · 12 comments
Open

Comments

@kerryshireman
Copy link

Hi all, I just started receiving an error today using this lib. I was on the 0.2.14 release prior to today, so I updated to the latest 0.2.21 release. I see that MFA is now included in the example code, so I incorporated the latest init_api function. Username & password challenge seems to work with the login method, as I am challenged for an MFA one-time code. After entering the code, I then get an exception:

requests.exceptions.HTTPError: 401 Client Error: Unauthorized for url: https://sso.garmin.com/sso/signin?id=gauth-widget&embedWidget=true&gauthHost=https://sso.garmin.com/sso/embed&service=https://sso.garmin.com/sso/embed&source=https://sso.garmin.com/sso/embed&redirectAfterAccountLoginUrl=https://sso.garmin.com/sso/embed&redirectAfterAccountCreationUrl=https://sso.garmin.com/sso/embed

I didn't see any related active issues. I've made no other changes to the Python setup using garminconnect for about 10 months. I've confirmed the same credentials work fine on the Garmin Connect website. Is anyone else having the same issue of late?

@kerryshireman
Copy link
Author

I dropped down to simply using the example.py file. Each time I login, the code setting the display_name triggers this error

  File "/Users/{path-to-venv}/lib/python3.11/site-packages/garminconnect/__init__.py", line 230, in login
    self.display_name = self.garth.profile["displayName"]
                        ^^^^^^^^^^^^^^^^^^
  File "/Users/{path-to-venv}/lib/python3.11/site-packages/garth/http.py", line 104, in profile
    assert isinstance(self._profile, dict)
AssertionError

@pblocz
Copy link

pblocz commented Nov 25, 2024

I am having the same error for the AssertionError, seems someone has reported it as an API change by Garmin in the garth library matin/garth#73

@kerryshireman
Copy link
Author

Good find, @pblocz, it's so fun when Garmin changes things. I'll leave this issue open for the moment, but assuming a garth fix is forthcoming, the pyproject.toml here should pick it up.

@RobertWojtowicz
Copy link

I dropped down to simply using the example.py file. Each time I login, the code setting the display_name triggers this error

  File "/Users/{path-to-venv}/lib/python3.11/site-packages/garminconnect/__init__.py", line 230, in login
    self.display_name = self.garth.profile["displayName"]
                        ^^^^^^^^^^^^^^^^^^
  File "/Users/{path-to-venv}/lib/python3.11/site-packages/garth/http.py", line 104, in profile
    assert isinstance(self._profile, dict)
AssertionError

Unfortunately I have the same

BR,
Robert

@yorkshirelad99
Copy link

confirm the same issue, tried on 2 machines and having the same problem

@enydog
Copy link

enydog commented Nov 26, 2024

The issue seems to be with garth, as a simple example triggers the same error:
return self.profile["userName"]

Test :
import garth
from getpass import getpass

email = input("Enter email address: ")
password = getpass("Enter password: ")

If there's MFA, you'll be prompted during the login

garth.login(email, password)
sleep = garth.connectapi(
f"/wellness-service/wellness/dailySleepData/{garth.client.username}",
params={"date": "2023-07-05", "nonSleepBufferMinutes": 60},
)
list(sleep.keys())

@psdupvi
Copy link
Contributor

psdupvi commented Nov 26, 2024

This issue can be fixed (thanks to the good work of the people over at garth) by going into the Garth package file you have as part of your environment and changing the following line in garth/http.py

Line 15-17

From:

USER_AGENT = {
    "User-Agent": (
        "Mozilla/5.0 (iPhone; CPU iPhone OS 16_5 like Mac OS X) "
        "AppleWebKit/605.1.15 (KHTML, like Gecko) Mobile/15E148"
    ),
}

to

USER_AGENT = {
    "User-Agent": ("GCM-iOS-5.7.2.1"),
}

There's a PR on that repo to update the user agent from our great maintainer @cyberjunky , but while we wait for that to get merged and everything to update, this will work

@kerryshireman
Copy link
Author

It appears a fix is imminent.

@cyberjunky already merged a USER_AGENT fix in their fork: cyberjunky/garth#1
My local tests updating the user agent restored API functionality.
Said fix is being merged into garth proper: matin/garth#74

Assuming a garth release is forthcoming, reinstalling python-garminconnect deps should pull in the fix.

Thanks all for such a fast response! No one likes it when their fitness automations are brokwn.

@LilDinker
Copy link

Thanks guys!

@enydog
Copy link

enydog commented Nov 26, 2024

thanks.

@pblocz
Copy link

pblocz commented Nov 26, 2024

I just tried it, but while matin/garth#74 is being merged and a new release of garth is created, this patch should do it:

import garth.http
garth.http.USER_AGENT = {"User-Agent": "GCM-iOS-5.7.2.1",}

@cyberjunky
Copy link
Owner

Temp fixed in version 0.2.22!

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

8 participants