cyberjunky / python-garminconnect

Python 3 API wrapper for Garmin Connect to get activity statistics
MIT License
964 stars 149 forks source link

Error with logout() in release 0.2.4 #152

Closed bobgott closed 1 year ago

bobgott commented 1 year ago

I noticed that when I upgraded to release 0.2.4 the method logout() was failing in my code. I then added that logic to example.py and received the same error (I'm not sure when logout is appropriate). Below is the subsequent trace

            elif i == "#":
                print('Logging Out!')
                api.logout()

Logging Out! ... trace

WARNING:urllib3.connectionpool:Retrying (Retry(total=2, connect=None, read=None, redirect=None, status=None)) after connection broken by 'NameResolutionError("<urllib3.connection.HTTPSConnection object at 0x00000274D11432D0>: Failed to resolve 'connectapi.garmin.comauth' ([Errno 11001] getaddrinfo failed)")': /logout/?url=
WARNING:urllib3.connectionpool:Retrying (Retry(total=1, connect=None, read=None, redirect=None, status=None)) after connection broken by 'NameResolutionError("<urllib3.connection.HTTPSConnection object at 0x00000274D1150210>: Failed to resolve 'connectapi.garmin.comauth' ([Errno 11001] getaddrinfo failed)")': /logout/?url=
WARNING:urllib3.connectionpool:Retrying (Retry(total=0, connect=None, read=None, redirect=None, status=None)) after connection broken by 'NameResolutionError("<urllib3.connection.HTTPSConnection object at 0x00000274D1153E50>: Failed to resolve 'connectapi.garmin.comauth' ([Errno 11001] getaddrinfo failed)")': /logout/?url=
Traceback (most recent call last):
  File "C:\Pythons\virtenvs\glogger11.4\Lib\site-packages\urllib3\connection.py", line 200, in _new_conn
    sock = connection.create_connection(
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Pythons\virtenvs\glogger11.4\Lib\site-packages\urllib3\util\connection.py", line 60, in create_connection
    for res in socket.getaddrinfo(host, port, family, socket.SOCK_STREAM):
               ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Pythons\Python3.11\Lib\socket.py", line 962, in getaddrinfo
    for res in _socket.getaddrinfo(host, port, family, type, proto, flags):
               ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
socket.gaierror: [Errno 11001] getaddrinfo failed

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
  File "C:\Pythons\virtenvs\glogger11.4\Lib\site-packages\urllib3\connectionpool.py", line 790, in urlopen
    response = self._make_request(
               ^^^^^^^^^^^^^^^^^^^
  File "C:\Pythons\virtenvs\glogger11.4\Lib\site-packages\urllib3\connectionpool.py", line 491, in _make_request
    raise new_e
  File "C:\Pythons\virtenvs\glogger11.4\Lib\site-packages\urllib3\connectionpool.py", line 467, in _make_request
    self._validate_conn(conn)
  File "C:\Pythons\virtenvs\glogger11.4\Lib\site-packages\urllib3\connectionpool.py", line 1092, in _validate_conn
    conn.connect()
  File "C:\Pythons\virtenvs\glogger11.4\Lib\site-packages\urllib3\connection.py", line 604, in connect
    self.sock = sock = self._new_conn()
                       ^^^^^^^^^^^^^^^^
  File "C:\Pythons\virtenvs\glogger11.4\Lib\site-packages\urllib3\connection.py", line 207, in _new_conn
    raise NameResolutionError(self.host, self, e) from e
urllib3.exceptions.NameResolutionError: <urllib3.connection.HTTPSConnection object at 0x00000274D1158790>: Failed to resolve 'connectapi.garmin.comauth' ([Errno 11001] getaddrinfo failed)

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
  File "C:\Pythons\virtenvs\glogger11.4\Lib\site-packages\requests\adapters.py", line 486, in send
    resp = conn.urlopen(
           ^^^^^^^^^^^^^
  File "C:\Pythons\virtenvs\glogger11.4\Lib\site-packages\urllib3\connectionpool.py", line 874, in urlopen
    return self.urlopen(
           ^^^^^^^^^^^^^
  File "C:\Pythons\virtenvs\glogger11.4\Lib\site-packages\urllib3\connectionpool.py", line 874, in urlopen
    return self.urlopen(
           ^^^^^^^^^^^^^
  File "C:\Pythons\virtenvs\glogger11.4\Lib\site-packages\urllib3\connectionpool.py", line 874, in urlopen
    return self.urlopen(
           ^^^^^^^^^^^^^
  File "C:\Pythons\virtenvs\glogger11.4\Lib\site-packages\urllib3\connectionpool.py", line 844, in urlopen
    retries = retries.increment(
              ^^^^^^^^^^^^^^^^^^
  File "C:\Pythons\virtenvs\glogger11.4\Lib\site-packages\urllib3\util\retry.py", line 515, in increment
    raise MaxRetryError(_pool, url, reason) from reason  # type: ignore[arg-type]
    ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
urllib3.exceptions.MaxRetryError: HTTPSConnectionPool(host='connectapi.garmin.comauth', port=443): Max retries exceeded with url: /logout/?url= (Caused by NameResolutionError("<urllib3.connection.HTTPSConnection object at 0x00000274D1158790>: Failed to resolve 'connectapi.garmin.comauth' ([Errno 11001] getaddrinfo failed)"))

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "C:\Users\suppo\Documents\Technology\Programming\Python\Projects\GarminLogger\src\special\garmin_api_examples.py", line 663, in <module>
    switch(api, option)
  File "C:\Users\suppo\Documents\Technology\Programming\Python\Projects\GarminLogger\src\special\garmin_api_examples.py", line 633, in switch
    api.logout()
  File "C:\Pythons\virtenvs\glogger11.4\Lib\site-packages\garminconnect\__init__.py", line 924, in logout
    self.connectapi(self.garmin_connect_logout)
  File "C:\Pythons\virtenvs\glogger11.4\Lib\site-packages\garminconnect\__init__.py", line 151, in connectapi
    return self.garth.connectapi(path, **kwargs)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Pythons\virtenvs\glogger11.4\Lib\site-packages\garth\http.py", line 148, in connectapi
    resp = self.get("connectapi", path, api=True, **kwargs)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Pythons\virtenvs\glogger11.4\Lib\site-packages\garth\http.py", line 133, in get
    return self.request("GET", *args, **kwargs)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Pythons\virtenvs\glogger11.4\Lib\site-packages\garth\http.py", line 116, in request
    self.last_resp = self.sess.request(
                     ^^^^^^^^^^^^^^^^^^
  File "C:\Pythons\virtenvs\glogger11.4\Lib\site-packages\requests\sessions.py", line 589, in request
    resp = self.send(prep, **send_kwargs)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Pythons\virtenvs\glogger11.4\Lib\site-packages\requests\sessions.py", line 703, in send
    r = adapter.send(request, **kwargs)
        ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Pythons\virtenvs\glogger11.4\Lib\site-packages\requests\adapters.py", line 519, in send
    raise ConnectionError(e, request=request)
requests.exceptions.ConnectionError: HTTPSConnectionPool(host='connectapi.garmin.comauth', port=443): Max retries exceeded with url: /logout/?url= (Caused by NameResolutionError("<urllib3.connection.HTTPSConnection object at 0x00000274D1158790>: Failed to resolve 'connectapi.garmin.comauth' ([Errno 11001] getaddrinfo failed)"))
cyberjunky commented 1 year ago

There is no logout url anymore in revised code, so in the example.py I just delete the stored tokens to simulate logout, forgot to change the api call (make it bogus/deprecated until we have or need similar solution)

@matin do you know if there is a logout endpoint in mobile api?

matin commented 1 year ago

There is. I'll add it later today.

bobgott commented 1 year ago

(@cyberjunky) so in the example.py I just delete the stored tokens to simulate logout

Thank you for the explanation. Seems like progress is being made

matin commented 1 year ago

The mobile app only revokes the OAuth2 token. The OAuth1 token remains active for a year since login and can still be used to obtain a new, valid OAuth2 token!

In other words, not even the app revokes the OAuth1 token to fully log out of the session.

The only method I can find to invalidate the OAuth1 token is to change your password in the Account Details page and choose "Sign me out of all Garmin websites and apps".

@cyberjunky the approach of simply deleting the tokens appears to be the right one.

cyberjunky commented 1 year ago

I marked api.logout as deprecated, example in example.py can be used to delete whole .garminconnect directory, or just delete one or two OAuth files.