ondrej1024 / carelink-python-client

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

Doesn't work for US. Only seems to work for EU. #18

Closed meluvalli closed 1 month ago

meluvalli commented 6 months ago

Hello. I am trying to use carelink_carepartner_api_login.py. It doesn't seem to work for the US. It defaults to EU as the county and because it goes straight to the login URL, you can't change the county.

Any suggestions? After Firefox launched, I tried manually going to the correct URL for US, but it after I login, it doesn't do anything... The python script that is..

meluvalli commented 6 months ago

Hmm. Ok. Found the issue. It appears on line 120 you have:

is_us_region=False

This hard codes it to say it's not US. I change it to True and then it goes to the US site correctly. Now for my next issue... Once I login, it just goes to a blank page and then nothing happens... Python doesn't continue and just sits there. Should I create a new issue for this?

ondrej1024 commented 6 months ago

I can fix the hardcoded region selection (it was used for testing and then I forgot about it). Unfortunately I don't have a US Carelink account, so I don't know how to debug this. But any contribution is welcome.

meluvalli commented 6 months ago

Hmm. Ok. Well, after you login, what should happen exactly? I know it suppose to generate the logindata.json... But is FireFox suppose to go to a blank page? Is it suppose to close FireFox automatically after it's done?

I was using the v1 successfully for a while now... However, now, when I use V1, it can't refresh the auth code. It give 403 error. I'm assuming something changed on Medtronic's side :(. So, I have to update the auth_token manually every 30 minutes for it to work, and obviously, that's not realistically doable :(

ondrej1024 commented 6 months ago

As for the login script, you may need to close Firefox if it's already running or nothing happens.

meluvalli commented 6 months ago

Well, I don't know much about programming in Python, but the line it's getting stuck on is 108:

for request in driver.requests:

It loops the sleep command, but I am assuming request is null or empty, so it never gets further on the do_captcha... It never get's the the line "if request.response: " (Line 109). It just loops between line 108 and 120. I am not sure why this would be. I'm running FireFox 124.0 (64-bit).

I'm guessing webdriver isn't working correctly in it.

My Test Code I tried was this:

def do_captcha(url, redirect_url):
    driver = webdriver.Firefox()
    driver.get(url)

    while True:
        print("Waiting to do For Statement...")
        for request in driver.requests:  
            print("Waiting for request.response")
            if request.response:  
                print(request.response.status_code)
                if request.response.status_code == 302:
                    if "location" in request.response.headers:
                        location = request.response.headers["location"]
                        if redirect_url in location:
                            code = re.search(r"code=(.*)&", location).group(1)
                            state = re.search(r"state=(.*)", location).group(1)
                            driver.quit()
                            return (code, state)
        sleep(0.1)

The output just looped over and over again "Waiting to do For Statement...". Which tells me it never gets the request in driver.requests.

And again, it does open FireFox for me correctly... So, I know webdriver is working in firefox.

ondrej1024 commented 6 months ago

There has been an issue reported with the login script reported in issue #15. This is probably not related but maybe it gives you some clue.

ondrej1024 commented 6 months ago

This is the Firefox window which opens on my PC when running carelink_carepartner_api_login.py with is_us_region=True. I am running this on Ubuntu Linux.

image

meluvalli commented 6 months ago

That's what mine looks like, but after I sign in, I get a blank white page, and nothing happens.
I read the info from #15, however, I don't understand how I would update the geckodriver on Windows. I downloaded the geckodriver.exe, but I don't have another geckodriver.exe file to replace to upgrade it... :(

meluvalli commented 6 months ago

Ok, I used https://stackoverflow.com/questions/41190989/how-do-i-install-geckodriver and downloaded the geckodriver. Then placed the exe in my Python directory. In My case it is "C:\Program Files\Python312". I am still having the same problem. I verified Selenium is installed in Python as well. It's version 4.19.0.

ondrej1024 commented 6 months ago

I guess we need some feedback of other Carelink user with a US account to find out if this is a general issue or just your setup. As said before, I cannot verify any of this, not having a US Carelink account, nor running Windows on my PC.

meluvalli commented 6 months ago

I finally got it!

So, here is what I did...

  1. Edited the carelink_carepartner_api_login.py file. Added the following lines on line 52: This allows for auto install of GeckoDriver.
    
    from selenium.webdriver.firefox.service import Service as FirefoxService
    from webdriver_manager.firefox import GeckoDriverManager
   Changed is_us_region=False to is_us_region=True.  (This was needed as I'm in the US not EU)

2. Installed Python 3.12.2 x64
3. Installed FireFox 124.0 x64
4. I rebooted my machine at this point.  Not sure if it's required or not.
5. Ran the following commands to install all pre-requisites for the carelink_carepartner_api_login.py

pip.exe install requests pip.exe install curlify pip.exe install pyOpenSSL pip.exe install selenium-wire

The below lines are new for allowing auto-install of GeckoDriver.

pip.exe install setuptools pip.exe install packaging pip.exe install webdriver-manager

6. Ran the script

python.exe carelink_carepartner_api_login.py


FireFox opened to Medtronic, I was able to login, then the screen closed automatically and the logindata.json file was generated!

Hope these DETAILED instructions help others if they have issues!

Side note:  This is on Windows 11 Build 26085.1
Below are the versions of packages installed:

requests==2.31.0 curlify==2.2.1 pyOpenSSL==24.1.0 selenium-wire==5.1.0 setuptools==69.2.0 packaging==24.0 webdriver-manager==4.0.1


Script installed GeckoDriver v0.34.0
ondrej1024 commented 6 months ago

Good to hear you worked it out.

I will add an option to the login script to select US region. But it seems that one can run into quite some trouble installing the correct script dependencies. I don't know how this could be improved.

ondrej1024 commented 1 month ago

This issue is fixed with PR #23