DavidMStraub / homeconnect

MIT License
31 stars 16 forks source link

Error on Auth #1

Closed iotlabo closed 4 years ago

iotlabo commented 5 years ago

I have the following error, but the states are equals.

Traceback (most recent call last): File "test.py", line 6, in hc.get_token("https://example.com/?code=XXXXXX&state=YYYYYY&grant_type=authorization_code") File "C:\WinPython\WPy-3662\python-3.6.6.amd64\lib\site-packages\homeconnect\api.py", line 95, in get_token client_secret=self.client_secret) File "C:\WinPython\WPy-3662\python-3.6.6.amd64\lib\site-packages\requests_oauthlib\oauth2_session.py", line 208, in fetch_token state=self._state) File "C:\WinPython\WPy-3662\python-3.6.6.amd64\lib\site-packages\oauthlib\oauth2\rfc6749\clients\web_application.py", line 203, in parse_request_uri_response response = parse_authorization_code_response(uri, state=state) File "C:\WinPython\WPy-3662\python-3.6.6.amd64\lib\site-packages\oauthlib\oauth2\rfc6749\parameters.py", line 271, in parse_authorization_code_response raise MismatchingStateError() oauthlib.oauth2.rfc6749.errors.MismatchingStateError: (mismatching_state) CSRF Warning! State not equal in request and response.

DavidMStraub commented 4 years ago

Sorry, I somehow overlooked this issue.

This looks like you simply pasted the wrong response URI.

santoshkrishnanr commented 4 years ago

I am getting a response from print(hc.get_authurl()) as https://api.home-connect.com//security/oauth/authorize?response_type=code&client_id=XXXXXXXXXXXXXXXXXXx&redirect_uri=https%3A%2F%2Fapi.home-connect.com%2Fsecurity%2Foauth%2Ftoken&state=XXXXXXXXXXXXXXXXXXXx

may i know what should be the redirecet uri given in homeconnect portal and in program. when not using homeassiastant.

DavidMStraub commented 4 years ago

In that case you can just use https://localhost/something. This doesn't have to exist and will give you a 404 in the browser, but that's enough to copy the URL you're being redirect to. Note that it has to start with https://.

DavidMStraub commented 4 years ago

I assume this can be closed.

qpkorr commented 3 years ago

Hi - total oauth newbie here - but I hit the same issue above, and just resolved it in a way I figured worth asking about. Your example code is:

from homeconnect import HomeConnect
hc = HomeConnect(my_clientid, my_clientsecret, my_redirecturi)
# open this URL in your web browser
print(hc.get_authurl())
# paste the resulting URL below as `auth_result` to get a token
hc.get_token(auth_result)
# list the existing appliances
hc.get_appliances()

So I saved this as a script, ran it, and it gave me the get_authurl. I put that in my browser, logged in, and got redirected (with redirect URI of https://localhost/something) which gave a 404 as you've said. Then I edited the script again, pasted in auth_result=, saved the script, and ran it again. But the next time it runs, it generates a get_authurl with a different "state" value, which - as per the original error above - doesn't match the value in the auth_result I pasted in from last execution of the script.

I suppose my mistake was that you intended this to be run interactively in python, not as a script? If you changed the code to include, above the call to get_token, a line like this: auth_result = input("Enter auth_result URL: ") then your code would work correctly as a script - and both myself, and perhaps the originator of this thread, might have got things working quicker? Either way - thanks very much for your code!

qpkorr commented 3 years ago

Haha - I just found your examples/simulator_events.py file, with a very similar input line to mine above... personally I'd still add it in the code above for dummies like me, but your call of course!