Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Captcha issue #29

Closed
gaardiolor opened this issue Aug 9, 2021 · 10 comments
Closed

Captcha issue #29

gaardiolor opened this issue Aug 9, 2021 · 10 comments

Comments

@gaardiolor
Copy link

gaardiolor commented Aug 9, 2021

Hello,

Looks like tesla changed something.

Traceback (most recent call last):
  File "/usr/local/scripts/teslacli/teslacli.py", line 40, in <module>
    main()
  File "/usr/local/scripts/teslacli/teslacli.py", line 31, in main
    tesla = fetch_tesla()
  File "/usr/local/scripts/teslacli/teslacli.py", line 16, in fetch_tesla
    tesla.fetch_token()
  File "/usr/local/scripts/teslacli/venv/lib/python3.9/site-packages/teslapy/__init__.py", line 210, in fetch_token
    raise ValueError('. '.join(msgs))
ValueError: Credentials rejected. Captcha is required. Captcha does not match

The first GET to oauth2/v3/authorize does not ask for a captcha, but after POST'ing username / password it rejects the login, and a new form is presented. Looks identical to the previous, but now a captcha is requested. teslapy doesn't handle this correctly.

Had to copy this in (__init__.py after line 198) to check for it:

        form = HTMLForm(response.text)
        # Retrieve captcha image if required
        if 'captcha' in form:
            response = oauth.get(self.sso_base + 'captcha')
            response.raise_for_status()  # Raise HTTPError, if one occurred
            form['captcha'] = self.captcha_solver(response.content)
            if not form['captcha']:
                raise ValueError('Missing captcha response')
            # Submit login credentials to get authorization code through redirect
            form.update({'identity': self.email, 'credential': self.password})
            response = oauth.post(self.sso_base + 'oauth2/v3/authorize',
                                  data=form, allow_redirects=False)
@dawiinci
Copy link

#24

tdorssers added a commit that referenced this issue Aug 12, 2021
@tdorssers
Copy link
Owner

Please give the fix in ebcef52 a try!

@marcone
Copy link
Contributor

marcone commented Aug 13, 2021

Is there a way to avoid triggering the captcha altogether? When I use cli.py it asks for a captcha, but when I sign in with the Tesla app it does not.
(edit: to be clear: this is with the latest code from github)

@omaliphant
Copy link

omaliphant commented Aug 13, 2021

@tdorssers i still seem to have an issue where the captcha doesnt save the svg file to my directory and never prompts me for the captcha response?

im using the:

`def solveCaptcha(svg):

    with open('captcha.svg', 'wb') as f:

        f.write(svg)

        f.close()

    return input('Captcha: ')`

with:

`def teslapyStartSession(creds):

    with teslapy.Tesla(creds[0], creds[2], lambda: 'XXXXXX') as tesla:

        tesla.captcha_solver = solveCaptcha

        tesla.fetch_token()

    return tesla`

is this correct?

@dawiinci
Copy link

Please give the fix in ebcef52 a try!

Unfortunately it still asks for a Captcha and opens the browser.

   Script Error                    tesla_connect.py: EOF when reading a line
   Script Error                    Exception Traceback (most recent call shown last):

     tesla_connect.py, line 18, at top level
     File "/Users/server/Library/Python/2.7/lib/python/site-packages/teslapy/__init__.py", line 205, in fetch_token
       form['captcha'] = self.captcha_solver(response.content)
     File "/Users/server/Library/Python/2.7/lib/python/site-packages/teslapy/__init__.py", line 319, in _solve_captcha
       return input('Captcha: ')
EOFError: EOF when reading a line

I just replaced the three files in teslapy folder.

@tdorssers
Copy link
Owner

I don't know why the auth server asks for a captcha, probably because it recognizes a foreign app logging in.

@tdorssers
Copy link
Owner

tdorssers commented Aug 15, 2021

@tdorssers i still seem to have an issue where the captcha doesnt save the svg file to my directory and never prompts me for the captcha response?

im using the:

`def solveCaptcha(svg):
    with open('captcha.svg', 'wb') as f:
        f.write(svg)
        f.close()
    return input('Captcha: ')`

with:

`def teslapyStartSession(creds):
    with teslapy.Tesla(creds[0], creds[2], lambda: 'XXXXXX') as tesla:
        tesla.captcha_solver = solveCaptcha
        tesla.fetch_token()
    return tesla`

is this correct?

Does this occur using version ebcef52? I haven't released a new version to pypi. Do you get any tracebacks?

@tdorssers
Copy link
Owner

Is there a way to avoid triggering the captcha altogether? When I use cli.py it asks for a captcha, but when I sign in with the Tesla app it does not.
(edit: to be clear: this is with the latest code from github)

I haven't found a way yet, maybe it has something to do with the useragent or some other properties that are recognized by the auth server.

@gaardiolor
Copy link
Author

@tdorssers ebcef52 works, no more tracebacks. Thanks!

@HildisviniOttar
Copy link

If you reverse proxy the native Tesla iOS app through Charles, API calls works with the Bearer tokens.
However if you logout and try to login again, it fails and shows an NSURLConnection error. Remove proxy and it works fine. They must be hard-coding a trusted certificate in-app that Tesla API requires for initial primary app auth.

Interestingly - I wonder if the native iOS app token can be used by teslapy for "permanent" auth token. 🤔

bhusang pushed a commit to bhusang/TeslaPy that referenced this issue Jan 17, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

6 participants