michielpost / Q42.HueApi

C# helper library to talk to the Philips Hue bridge
MIT License
411 stars 114 forks source link

Authenticate in API V2 #285

Closed gkapellmann closed 1 year ago

gkapellmann commented 1 year ago

Hey, great package!

This is more a question than an issue I guess: Is there a roadmap or an estimate of when will the Authentication might arrive for the API v2?

I know that atm we can do it with the original:

Registration of your App and retreiving a key can be done using the original Q42.HueApi. There's no support yet in the new Clip V2 API.

Just wondering to orginize myself, maybe even try to implement it.

michielpost commented 1 year ago

Hi!

This library follows the Hue API. There is no new v2 API for authenticating with the bridge, so that's why this is not implemented yet. If Hue develops a v2 authentication API, it will be included.

But I do understand that this makes standalone usage of the new v2 HueApi impossible, you will also need a dependency on the old Q42.HueApi just for the authentication part.

So what I can do is also implement authentication in the new HueApi, but that will call the old APIs. Would that help?

michielpost commented 1 year ago

I included a LocalHueApi.RegisterAsync static method in version 0.9.1 (now on NuGet). Let me know if this works for you.

gkapellmann commented 1 year ago

Ah ok! Thank you for your answer.

Yes, that will help, I will test in the next few days and let you know how it went.

Thanks!

gkapellmann commented 1 year ago

Yeap, works like a charm now.

One question, what is the "regResult.StreamingClientKey"?

And a request, could "await LocalHueApi.RegisterAsync" implement a cancellation event? I am thinking for a timeout in case the button doesnt get pressed, to be able to catch the timeout, for example:

using (var cancellationTokenSource = new CancellationTokenSource(5000)) { var regResult = await LocalHueApi.RegisterAsync(ipAddress, newUser, ID, cancellationTokenSource.Token); }

michielpost commented 1 year ago

The StreamingClientKey will be filled with the streaming / entertainment key when you send generateClientKey = true with the RegisterAsync request. It's only needed when you want to use the streaming API.

I also added the cancellationToken. But the user should first press the button, and then do a RegisterAsync request. If the button is not pressed, it will immediately return with a LinkButtonNotPressedException

gkapellmann commented 1 year ago

OK, thank you!

I'll test and let you know how it goes.

gkapellmann commented 1 year ago

Ok, this works like a charm, Thanks!