tdorssers / TeslaPy

A Python module to use the Tesla Motors Owner API
MIT License
371 stars 80 forks source link

ValueError: Credentials rejected. Recaptcha is required #32

Closed totiherms closed 3 years ago

totiherms commented 3 years ago

Hi,

recently I'm getting this error :

Traceback (most recent call last): File "/home/gettoken.py", line 29, in main(sys.argv[1],sys.argv[2]) File "/home/gettoken.py", line 24, in main s = tesla.fetch_token() File "/opt/rh/rh-python36/root/usr/lib64/python3.6/site-packages/teslapy/init.py", line 227, in fetch_token raise ValueError('. '.join(msgs)) ValueError: Credentials rejected. Recaptcha is required

It seems that this error is raised before asking for the captcha solver? I'm using this code to solve captchas online via 2captcha, tit worked perfectlly until yesterday, and now seems that solve_captcha it's not executing

def solve_captcha(svg):
    #print("solver")
    with open('captcha.svg', 'wb') as f:
        f.write(svg)
    solver = CaptchaSolver('2captcha', api_key='xxxx')
    cairosvg.svg2png(url='captcha.svg', write_to='captcha.png')
    raw_data = open('captcha.png', 'rb').read()
    solv = solver.solve_captcha(raw_data)
    #print(solv)
    return solv

def main(user, pwd):
     with teslapy.Tesla(user, pwd) as tesla:
          tesla.captcha_solver = solve_captcha   
          s = tesla.fetch_token()
          print(tesla.token)
          return tesla.token

thanks!

MarlinMr commented 3 years ago

Same. But I am doing major rework on my scripts, so might be that for me.

jamesdeck commented 3 years ago

Something definitely changed. Not working for me as well.

alandtse commented 3 years ago

To the extent it helps TeslaPy resolve this, I wanted to share my observations. It looks like recaptcha has been added today which requires JavaScript to be processed to continue. I have a python library that proxied all the requests to a user that ran into this issue today too.

If you disable javascript, which is how python API access works, it fails with the message reported above. If you enable JavaScript, the scripts checks to confirm your browser is at auth.tesla.com and fails since it's not the right location. So my conclusion is that you need to process JavaScript and also override any checks on the browser location.

tdorssers commented 3 years ago

Captcha and recaptcha are very different. Tesla has upgraded to recaptcha, which is much harder to bypass. Any help much appreciated.

totiherms commented 3 years ago

I'm using 2captcha to solve captchas automatically, and accepts recaptcha v2 and v3, maybe this can be a temporary solution?

https://github.com/2captcha/2captcha-python#recaptcha-v3

result = solver.recaptcha(sitekey='6Le-wvkSVVABCPBMRTvw0Q4Muexq1bi0DJwx_mJ-',
                            url='https://mysite.com/page/with/recaptcha',
                            version='v3',
                            param1=..., ...)
AGSPhoenix commented 3 years ago

What information would be needed from 3rd party token manager apps to build a valid cache.json file? Could we get a redacted example file for those of us who have tokens already?

giachello commented 3 years ago

Please consider signing this petition to Tesla to open up their API https://chng.it/kfjvv9sr6k

tdorssers commented 3 years ago

What information would be needed from 3rd party token manager apps to build a valid cache.json file? Could we get a redacted example file for those of us who have tokens already?

This is the format:

{
    "elon@tesla.com": {
        "url": "https://auth.tesla.com/",
        "sso": {
            "access_token": "***",
            "refresh_token": "***",
            "id_token": "***",
            "expires_in": 28800,
            "state": "***",
            "token_type": "Bearer",
            "expires_at": 1631332627.6740067
        },
        "ownerapi": {
            "access_token": "***",
            "token_type": "bearer",
            "expires_in": 3888000,
            "refresh_token": "***",
            "created_at": 1631303828
        }
    }
}
tdorssers commented 3 years ago

I have removed headless login from the module. A web browser is now required to authenticate and manual copy-pasting of the redirected URL is needed. It's not the nicest solution, but it works (no ReCaptcha anymore) and you only need to do it once. Using Selenium this process can be automated, so it is not too bad. Please give f9a28cc a try.

AGSPhoenix commented 3 years ago

Tried it with menu.py. Entered my email address and got taken to the sign in page. Signed in and got a 404 error.

With no advance warning that this would happen, I assumed something had broken. Figured it out when I went back to the console, but some users will not know what the 'redirect URL' is. Should probably add a brief warning about what will happen next and what to do.

That aside, seems to work great.

tdorssers commented 3 years ago

@AGSPhoenix thanks for the feedback. f6514a6 includes a brief warning and improved docs.

tdorssers commented 3 years ago

Packaged, released and made available on PyPI

totiherms commented 3 years ago

@tdorssers works perfect for me as well, thanks a lot!