DavidMStraub / homeconnect

MIT License
31 stars 16 forks source link

Web-based auth workflow neccessary after python app restart? #8

Closed psilo909 closed 4 months ago

psilo909 commented 3 years ago

Hi,

just a short question.

I am currently integrating the library into our home automation framework https://www.smarthomeng.de/.

After passing the authentication workflow, I get the token and the token gets cached in the json file.

When I now restart our framework, I am even explicitly doing a token_load(). The token values are correctly retreived. Unfortunately when requesting e.g. the appliances, I am getting a 401 now:

homeconnect.api.HomeConnectError: {'key': '401', 'description': 'Authentication is possible but has failed or not yet been provided.'}

Is there the neccessity to walk through the web based authentication workflow each time when I restart?

From other oauth2 based workflows I integrated (e.g. withings-api) I know, that it is no problem to restart, as long as the token refresh interval is not hit.

Thanks for your reply, I will update the post in case I get new insights.

René

DavidMStraub commented 3 years ago

Have you seen the token_dump and token_load methods? (I'm not using them myself as I'm using Home Assistant which is handling the token storage/refresh internally.)

psilo909 commented 3 years ago

Yes,, as written I am doing a token = self.plugin.get_hc().token_load() (get_hc() returns your HomeConnect object).

The Dump is done automatically once I execture the get_token URL https://github.com/DavidMStraub/homeconnect/blob/master/homeconnect/api.py#L178

token_load() also looks correcly executed after I restart my python project, as I have all token information afterwards.

But the https://github.com/DavidMStraub/homeconnect/blob/master/homeconnect/api.py#L126 is still getting the HTTP error code (coming from https://github.com/DavidMStraub/homeconnect/blob/master/homeconnect/api.py#L87) until i completly run through the web-workflow again. Refresh also only makes sense once the token is expired. From what i see in the timestamp, the token is valid until tomorrow.

DavidMStraub commented 3 years ago

I see. That's strange, that should work :thinking:

psilo909 commented 3 years ago

i will try some more debugging tomorrow. in the worst case i add your source to my project to directly ty some things in your code.

I also prefer to store the token information in our internal "item structure" and not in a file. so perhaps it makes sense for me in any way.

psilo909 commented 3 years ago

I did some further analysis and wonder, where the token in https://github.com/DavidMStraub/homeconnect/blob/ae2780976dfdc0d20c908772c54f650103d18a21/homeconnect/api.py#L48 comes from, once i reinitialize the HomeConnect superclass after a restart. normally i would assume that the tokes must be loaded, but i dont see any place for that. I also cant do that manually before the instantiation, as the token_load() method can only be called once the whole HomeConnect object is already assembled. And after that i cant reinitiate the session. Not sure if I am following the right path, but I think I am getting the unauthorized because something is missing in the session.

Is there any way to see if this session exists? I tried to write hc._oauth out via logger, but see nothing as output.

DavidMStraub commented 3 years ago

Can you please have a look at this old version of the code? https://github.com/DavidMStraub/homeconnect/blob/a8577f104f8fdd330b417df5d2087eac2d7ebad6/homeconnect/api.py

After this commit, I refactored it to match with Home Assistant's internal OAuth system, but it might well be that I screwed up the way it works when manually storing the token.

Perhaps that old version makes more sense for what you are doing.

psilo909 commented 3 years ago

thx. I will check it out later!

psilo909 commented 3 years ago

Hi,

it looks like your old code is working. I will integrate it directly in my plugin and use my own way of saving the token. Hope thats fine for you!

Thx for the hint! Will do more tests now.

René

Mirarkitty commented 2 years ago

I ran into the same issue. Using token_load after starting the hc object gives

homeconnect.api.HomeConnectError: {'key': '401', 'description': 'Authentication is possible but has failed or not yet been provided.'}

Not sure what to do about it other than using the old code, too.

from homeconnect import HomeConnect hc=HomeConnect(...); hc.token_load() {'access_token': [...stuff...] hc.get_appliances() Traceback (most recent call last): File "", line 1, in File "/home/mirar/.local/lib/python3.6/site-packages/homeconnect/api.py", line 136, in get_appliances data = self.get(ENDPOINT_APPLIANCES) File "/home/mirar/.local/lib/python3.6/site-packages/homeconnect/api.py", line 91, in get raise HomeConnectError(res["error"]) homeconnect.api.HomeConnectError: {'key': '401', 'description': 'Authentication is possible but has failed or not yet been provided.'}

DavidMStraub commented 2 years ago

Indeed, don't know why you closed this @psilo909 as apparently it's not fixed.

JoshuaDodds commented 2 years ago

@DavidMStraub Do you have any time to look into fixing this? I am also attempting to use your module for my own integration and running into the same issues mentioned above.

DavidMStraub commented 2 years ago

Honestly I don't think I'll have time in the near future. But I would be happy to review/merge contributions.

Mirarkitty commented 2 years ago

I added self.token_load() to init, and

    self._oauth = OAuth2Session(
        client_id=self.client_id,
        redirect_uri=self.redirect_uri,
        auto_refresh_kwargs={"client_id": self.client_id, "client_secret": self.client_secret},
        token=token,
        token_updater=self.token_updater,
    )

to token_load (before return). Not perfect but better. Problem seems to be that the information is not transferred to the oauth and the oauth is started before token is loaded.

Also had to change default retry timeouts from 1000 to 30000 or I would run out of requests every day (not sure why), but that's unrelated (probably).

GijsTimmers commented 1 year ago

@Mirarkitty could you perhaps upload your full code? I am running into this problem as well and haven't been able to fix it yet.

Mirarkitty commented 1 year ago

Here's my api.py (python 3.8, not updated since at least january). api.py Sorry about the late reply.

DavidMStraub commented 4 months ago

Solved by #29.

Mirarkitty commented 3 months ago

Thanks!