yo-han / Home-Assistant-Carelink

Unofficial Home Assistant Carelink Component
MIT License
24 stars 12 forks source link

Integration stopped working & cannot login anymore: 'auth_tmp_token' error #46

Closed GertVanderstukken closed 1 year ago

GertVanderstukken commented 1 year ago

The integration suddenly stopped working somewhere yesterday. I have to say that I also had some issues on the app on my mobile for some time before that, so I figured it to be a general problem @ Medtronic.

Then the app on my mobile started working again, however the HA integration was not.

I tried to remove and re-add the integration, but when I try to complete the credential window, I get "bad username/password" (but I'm "sure" it's OK).

Anybody else with this problem?

This is in the logs: ` 2023-08-19 00:32:21.165 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 468, in get_recent_data if await self.__get_authorization_token() is not None: ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "/config/custom_components/carelink/api.py", line 430, 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'
2023-08-19 00:32:21.173 WARNING (MainThread) [homeassistant.config_entries] Config entry 'Carelink' for carelink integration not ready yet: 'auth_tmp_token'; Retrying in background
`
GertVanderstukken commented 1 year ago

Just to know: Is anybody else experiencing the same issue? Or is it just me :( ?

yo-han commented 1 year ago

Hi @GertVanderstukken. I've never seen this issue before. What version of Home Assistant are you using and what version of this integration? Are they both up to date?

GertVanderstukken commented 1 year ago

Hey, strange...

I'm on the latest version of everything:

Home Assistant 2023.8.3 Supervisor 2023.08.1 Operating System 10.5 Frontend-versie: 20230802.1 - latest

HACS: 1.32.1 Carelink Integration: 2023.7.0

GertVanderstukken commented 1 year ago

OK, I did some more testing on a Home-Assistant instance I had powered off for some time, that is based on docker.

When I start it with a container running Home-Assistant version 2023.5.3, it runs OK. When I start it using a 2023.5.4 container, it fails.

image image

Stopping and restarting 2023.5.3 => OK again.

image image

What version of Home Assistant are you running ?

GertVanderstukken commented 1 year ago

I just installed a complete new Home Assistant server, installed HACS & Carelink integration => not working. Very strange. Nobody else with this problem?

yo-han commented 1 year ago

I'm running the integration in the latest available version now. Maybe I can test some other versions tonight, I know there are some changes done to Home Assistant this year that had an impact on the integration but they are never in minor releases (e.g. 2023.8.2) but only in the major ones (e.g. 2023.8.0).

There you got seems to occur when no session is setup with metronic after login. Not sure why this only seems to happen to you.

bvdkerkhof commented 1 year ago

I can confirm i have the same issue, also the temp token issue.

Running the HA Core 2023.8.3 version on Python 3.11 on Raspbian. If you need more info let me know i'm happy to do some debug logging

yo-han commented 1 year ago

Jup, got it now too after a reboot. Not sure where this comes from but now I have a starting point at least.

GertVanderstukken commented 1 year ago

At least I'm not alone anymore. Welcome to the club :)

pops3449 commented 1 year ago

Same issue image Home Assistant 2023.8.3 Supervisor 2023.08.1 Operating System 10.4 Frontend 20230802.1 - latest

yo-han commented 1 year ago

I did a quick test with the original client lib I used (https://github.com/ondrej1024/carelink-python-client) and it has the same problem. I guess something changed in the login process again.

It can also have something to do with the python version 3.11. Is there anyone still running on python 3.10 and running a working installation of the latest version of this integration?

niemczuk commented 1 year ago

User-Agent data was modified in the xDRIP application and it started logging in correctly: https://github.com/NightscoutFoundation/xDrip/commit/22954b7233e6070b0a758f21cf06e234eae2c0e7

GertVanderstukken commented 1 year ago

OK, I found a way to make it work. in api.py, I changed line 94: self.__common_headers = {} into:

self.__common_headers = {
    "Accept-Language":"en;q=0.9, *;q=0.8",
    "Connection":"keep-alive",
    "sec-ch-ua":"\"Google Chrome\";v=\"115\", \" Not;A Brand\";v=\"99\", \"Chromium\";v=\"115\"",
    "User-Agent":"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/115.0.0.0 Safari/537.36",
    "Accept":"text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9"
}

rebooted and all started working.

I based myself on "carelink_client.py" - line 78 (https://github.com/ondrej1024/carelink-python-client). Also in there, they mention: # 09/05/2023 - Fix connection issues by removing common http headers But.... I'm NOT a developer, so I'm not sure if this is the right thing ...

niemczuk commented 1 year ago

I corrected the code you wrote in the api.py file and everything works fine now - good job.

yo-han commented 1 year ago

Hmmm. These headers are removed in pr #39 because it made the login not work. Not completely sure if this is the final solution.

sedy89 commented 1 year ago

hi @yo-han , as @niemczuk mentioned, a very similar change was taken into xdrip to fix the communication after the latest carelink server update https://github.com/NightscoutFoundation/xDrip/pull/3029. They did not just add back the common headers, but they updated the chrome user agent to the latest version number. Seems to be required after the latest medtronic server update.

yo-han commented 1 year ago

Yes I noticed. But what I don't understand is why a version of a browser would be a factor for Medtronic to not let someone login. This is the second time these headers are the source of issues where users cannot login anymore. For now we can restore them, but I would like to know what the real reason is they need to be updated.

Why would Chrome/112 be an issue? It's a release from may this year. A version like Chrome/87 would make more sense.

yo-han commented 1 year ago

I made a beta release with a possible fix. If anyone wants to test it please activate beta releases for this integration in Home Assistant and pull new updates.

https://github.com/yo-han/Home-Assistant-Carelink/releases/tag/2023.8.0-b1

hhuitema commented 1 year ago

Works for my install.

Ps my theory is that Medtronic was implementing updates on the Auth services. But the last Weeks there were quite a few outages, so maybe they opted to go back to something more reliable..

bvdkerkhof commented 1 year ago

I made a beta release with a possible fix. If anyone wants to test it please activate beta releases for this integration in Home Assistant and pull new updates.

https://github.com/yo-han/Home-Assistant-Carelink/releases/tag/2023.8.0-b1

The beta also solves the issue for me

yo-han commented 1 year ago

Allright. A new release is available for everyone now. It worked for me I several version too so this must be it for now.

GertVanderstukken commented 1 year ago

Yes, the new release is working for me. Thanks!