michielpost / Q42.HueApi

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

How to use the remote API? #104

Closed niels9001 closed 7 years ago

niels9001 commented 7 years ago

Sorry for this :(, to me it's not entirely clear when to use what: https://github.com/Q42/Q42.HueApi/blob/master/RemoteApi.md

Is my thinking correct?

IRemoteAuthenticationClient authClient = new RemoteAuthenticationClient(clientId, clientSecret, appId); authClient.BuildAuthorizeUri("sample", "consoleapp"); var webAuthenticationResult = await WebAuthenticationBroker.AuthenticateAsync(WebAuthenticationOptions.None, authorizeUri, callbackUri); var accessToken = await authClient.GetToken(result.Code);



Afterwards, if you want to check if the user is on wifi. If not, you can connect like this:

`IRemoteHueClient client = new RemoteHueClient(authClient.GetValidToken);`

Do we need to store the token to replace authClient.GetValidToken after the authenticating?

Oh - and RegisterBridgeAsync, is this also for apps that have already connected locally to the Bridge?
michielpost commented 7 years ago

Yes, you can store the accessToken in your app and use it to connect to the bridge later. I think the token is not needed anymore.

I'm not sure about the RegisterBridgeAsync, maybe if you use the same app id it is not needed. Test it and let me know the results.

niels9001 commented 7 years ago

Thanks, I'll look into the Register stuff to see if necessary.

So, as I understand now you follow the code here when setting it up for the first time. (https://github.com/Q42/Q42.HueApi/blob/master/RemoteApi.md) for the first connection / registration.

But the 2nd time the app launches, how do we then use the RemoteClient? How do we use the authClient.GetValidToken then?

michielpost commented 7 years ago

Good point. The access token and refresh token are only valid for a limited amount of time. So I guess you'll have to go through the whole authentication process again. There must be an easier way, maybe you can ask Philips how you should handle a user that gave permission and connects for a second time. Or play around with the sample to see what works best.

I'm not sure, I just created a simple sample app that works but have no further info.

niels9001 commented 7 years ago

I checked with Philips, and I got some good feedback that validates your point. On the access/refresh tokens:

"The authentication code that you get at the begin is only valid for a few minutes. With this, you can get the access tokens. The access tokens are valid for 7 days and the refreshtokens for 100 days. With the refreshtokens you can get a new set of accesstokens and refreshtokens. So what you need to do is to see if the accesstokens are still valid. You could do this with a call, find out that the accesstoken has been expired and then take action. If the refreshtoken has been expired the user would need to log in."

On registration:

Registration is only necessary if the app hasn't registered locally yet. If this has already been done you don't need to do it again, only if you'd want to keep them explicitely seperate.

michielpost commented 7 years ago

Ok thanks. I'll create an extra method so you an initialize the RemoteAuthenticationClient with a access and refresh token.

michielpost commented 7 years ago

You can now initialize the remote authentication client with existing tokens. Example in comments here: https://github.com/Q42/Q42.HueApi/blob/master/src/Q42.HueApi.RemoteApi.Sample/MainPage.xaml.cs#L48-L51

Should be enough to create an app that uses the remote api.