ondrej1024 / carelink-python-client

Experimental Medtronic CareLink Client in Python
GNU Affero General Public License v3.0
39 stars 21 forks source link

Login fails after Carelink update #5

Closed sbc2280 closed 1 year ago

sbecze commented 1 year ago

Connection log:

[2023-06-28 10:31:02:DEBUG] HTTP server started at http://0.0.0.0:8081
[2023-06-28 10:31:02:DEBUG] Starting new HTTPS connection (1): carelink.minimed.eu:443
[2023-06-28 10:31:04:DEBUG] https://carelink.minimed.eu:443 "GET /patient/sso/login?country=hu&lang=en HTTP/1.1" 303 0
[2023-06-28 10:31:04:DEBUG] Starting new HTTPS connection (1): mdtlogin-ocl.medtronic.com:443
[2023-06-28 10:31:04:DEBUG] https://mdtlogin-ocl.medtronic.com:443 "GET /mmcl/auth/oauth/v2/authorize?client_id=fc310f93-e8c1-4719-ae9c-69d65884e235&response_type=code&redirect_uri=https://carelink.minimed.eu/patient/sso/auth&scope=openid%20profile%20roles&state=auth&response_mode=query&lang=en&country=hu&code_challenge=Kj-Fz86_aCxn7D1Qqc8VN5hip9bdFr5nz-QROvb8MIw&code_challenge_method=S256 HTTP/1.1" 302 20
[2023-06-28 10:31:04:DEBUG] https://mdtlogin-ocl.medtronic.com:443 "GET /mmcl/auth/oauth/v2/authorize/login?action=display&sessionID=3bc41ed2-fd60-4572-9871-31a9854dedbe&sessionData=eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.ew0KICAgICJzZXNzaW9uIjogew0KICAgICAgICAic2Vzc2lvbklEIjoiM2JjNDFlZDItZmQ2MC00NTcyLTk4NzEtMzFhOTg1NGRlZGJlIiwNCiAgICAgICAgImV4cCI6MTY4Nzk0NDY2NCwNCiAgICAgICAgImN1cnJlbnRfdXNlcm5hbWUiOiIiLA0KICAgICAgICAiY3VycmVudF91c2VyX2NvbnNlbnQiOiJub25lIiwNCiAgICAgICAgImN1cnJlbnRfdXNlcl9yb2xlIjoiIiwNCiAgICAgICAgImN1cnJlbnRfdXNlcl9hY3IiOiIwIiwNCiAgICAgICAgImN1cnJlbnRfdXNlcl9hdXRoVGltZSI6IjAiLA0KICAgICAgICAiY3VycmVudF91c2VyX2F0dHJpYnV0ZXMiOiIiLA0KICAgICAgICAic2FsdCI6IiIsDQogICAgICAgICJ0aGlyZF9wYXJ0eV9zc29fdG9rZW4iOiIiLA0KICAgICAgICAidGhpcmRfcGFydHlfc3NvX3Rva2VuX3R5cGUiOiIiDQogICAgfSwNCiAgICAicmVxdWVzdF9jb25zZW50Ijogew0KICAgICAgICAiY2xpZW50X25hbWUiOiJPQ0wrLStXZWIrYXBwKy0rT1VTIiwNCiAgICAgICAgInNjb3BlX3ZlcmlmaWVkIjoib3BlbmlkK3Byb2ZpbGUrcm9sZXMiDQogICAgfSwNCiAgICAicmVxdWVzdF9wYXJhbWV0ZXJzIjogew0KICAgICAgICAiZGlzcGxheSI6InBhZ2UiLA0KICAgICAgICAicHJvbXB0IjoibG9naW4rY29uc2VudCIsDQogICAgICAgICJpZF90b2tlbl9oaW50IjoiIiwNCiAgICAgICAgImxvZ2luX2hpbnQiOiIiLA0KICAgICAgICAiYWNyX3ZhbHVlcyI6IiIsDQogICAgICAgICJjbGllbnRfaWQiOiJmYzMxMGY5My1lOGMxLTQ3MTktYWU5Yy02OWQ2NTg4NGUyMzUiLA0KICAgICAgICAibm9uY2UiOiIiLA0KICAgICAgICAic2NvcGUiOiJvcGVuaWQrcHJvZmlsZStyb2xlcyIsDQogICAgICAgICJtYXhfYWdlIjogIiINCiAgICB9DQp9.zcSx0YIiFR8HcpywwRmeFwBecFEByO5qpzsYSONbNrE&locale=en&countrycode=hu HTTP/1.1" 200 1741
[2023-06-28 10:31:04:DEBUG] Starting new HTTPS connection (1): mdtlogin.medtronic.com:443
[2023-06-28 10:31:05:DEBUG] https://mdtlogin.medtronic.com:443 "POST /mmcl/auth/oauth/v2/authorize/login?country=hu&locale=en HTTP/1.1" 400 1806
Client login error! Response code: 400 Error message: local variable 'response' referenced before assignment
hhuitema commented 1 year ago

Giving some feedback as an interested party.. (the home-assistant integration has resolved this problem)

Medtronic change the auth server url url = "https://mdtlogin.medtronic.com" + "/mmcl/auth/oauth/v2/authorize/login"

Should become url = "https://mdtlogin-ocl.medtronic.com" + "/mmcl/auth/oauth/v2/authorize/login"

ondrej1024 commented 1 year ago

There has been some maintenance downtime of the Carelink service yesterday. So I was actually afraid that something would change and break things. Did anyone try the new login URL in the carelink-python-client ?

hhuitema commented 1 year ago

I cannot confirm, because it is not working in my case. (unclear what the issue is) but others have mentioned a succesfull fix.

https://github.com/yo-han/Home-Assistant-Carelink/issues/36

Akilan1999 commented 1 year ago

Giving some feedback as an interested party.. (the home-assistant integration has resolved this problem)

Medtronic change the auth server url url = "https://mdtlogin.medtronic.com" + "/mmcl/auth/oauth/v2/authorize/login"

Should become url = "https://mdtlogin-ocl.medtronic.com" + "/mmcl/auth/oauth/v2/authorize/login"

This worked for me thanks !

tloczekt commented 1 year ago

@hhuitema, @ondrej1024 be careful, new url works only for EU Carelink server, US host is still "https://mdtlogin.medtronic.com/"

More flexible and universal can be similar like : url = loginSessionResponse.url.scheme + "://" + loginSessionResponse.url.host + "".join(loginSessionResponse.url().encodedPath().substring(1))

it's not tested, it's only suggestion

tloczekt commented 1 year ago

I'm sorry I mixed languages: in python should be rather: from urllib.parse import urlparse

url = loginSessionResponse.url.scheme + "://" + loginSessionResponse.url.host + "".join(urlparse(loginSessionResponse.url).path[1:])

ondrej1024 commented 1 year ago

@tloczekt , thanks for your suggestion. In fact, I don't think we should have a hard coded login server if the information is provided by the API. But it is not clear to me where you take this information from. Is it in the response to the initial connection request?

tloczekt commented 1 year ago

I mean 128 line in Carelink_client.py

You have url="......". You have one host for all server, but EU has now = "https://mdtlogin-ocl.medtronic.com" but US has old one = "https://mdtlogin.medtronic.com" .

I recommend to use loginSessionResponse.url.host instead of text "https://......."

SmokelessLlama commented 1 year ago

I just updated in HACS and am now getting "Unexpected error fetching carelink data: 'auth_tmp_token'"

In the US, do I need to change that url?

tloczekt commented 1 year ago

I just updated in HACS and am now getting "Unexpected error fetching carelink data: 'auth_tmp_token'"

In the US, do I need to change that url?

US has still old one = "https://mdtlogin.medtronic.com" as I know.

But it seems your issue is different. It seems sth wrong with token

SmokelessLlama commented 1 year ago

Is there anything I can do to resolve this error or do I just need to wait it out for an update? more detail:

2023-06-29 12:00:27.474 ERROR (MainThread) [custom_components.carelink] Unexpected error fetching carelink data: 'auth_tmp_token' Traceback (most recent call last): File "/usr/src/homeassistant/homeassistant/helpers/update_coordinator.py", line 283, in _async_refresh self.data = await self._async_update_data() ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "/config/custom_components/carelink/init.py", line 149, in _async_update_data recent_data = await self.client.get_recent_data() ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "/config/custom_components/carelink/api.py", line 467, in get_recent_data if await self.__get_authorization_token() is not None: ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "/config/custom_components/carelink/api.py", line 429, in __get_authorization_token auth_token = self.async_client.cookies[CARELINK_AUTH_TOKEN_COOKIE_NAME]


File "/usr/local/lib/python3.11/site-packages/httpx/_models.py", line 1148, in __getitem__
raise KeyError(name)
KeyError: 'auth_tmp_token'
ondrej1024 commented 1 year ago

Here is a fix that removes the hard coded login server name: https://github.com/ondrej1024/carelink-python-client/commit/4e01d510968edbdc6fd21ba0dc542fbee6c30bc2

Please anyone try and confirm that it works for you (both EU and US).

tloczekt commented 1 year ago

It works for my daughter (EU) and friend in US.

ondrej1024 commented 1 year ago

It works for my daughter (EU) and friend in US.

Great, thanks for the feedback