michielpost / Q42.HueApi

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

LocalHueClient.RegisterAsync does not return applicationKey #222

Closed Spaceva closed 4 years ago

Spaceva commented 4 years ago

Hello, when I do this, and I pressed the button beforehand, I have null value returned from RegisterAsync. The entry in the whitelist is made though.

this.hueClient = new LocalHueClient(hueSettingsOptions.Value.IP);
var appKey = await this.hueClient.RegisterAsync("MyApp", Environment.MachineName);
michielpost commented 4 years ago

Q42.HueApi does an API call to the Hue bridge and reads the json result. If the Hue bridge does not return an API key, there's not much I can do. Maybe check the Philips Hue API forum of this is expected behaviour?

Spaceva commented 4 years ago

When I test on my local bridge with the provided UI (http://yourIP/debug/clip.html), I do get something like that :

[
    {
        "success": {
            "username": "wkxUCNpNeh-nZzjnIAlQuZnMbn-o5OOJszjpljEp"
        }
    }
]
michielpost commented 4 years ago

Yes, this is a correct response and Q42.HueApi can also parse this result and return the key. I don't think there's an actual issue with the library, parsing the api key is in the library since the beginning and never had any problems with it.

Can you reproduce this multiple times?

What you're describing is the normal workflow. A user always first presses the button and then Q42.HueApi should do the request to get an API key.

Spaceva commented 4 years ago

Which method from the library should I use then to get a new appkey ? I thought it was this one, sorry if I was mistaken. Yes it reproduces every time I try.

michielpost commented 4 years ago

Yes, your code is correct. Maybe the computername contains weird characters that cause this to fail?

Can you run the sample included in this repo? Q42.HueApi.UniversalWindows.Sample Then first use the Locate the bridge and then go to Register app, press the button on your bridge and then the Register button in the app, it should return an api key. Just tested this locally and that works.

Spaceva commented 4 years ago

I'm sorry I should have mentionned, I'm doing a console app. I would have to install the W10 SDK to do the test you ask me, it's 10 GB. Is there another way to test ? I tried to use the locator, I got the IP of the bridge, like the one I provided, but still got null result on RegisterAsync method :(

michielpost commented 4 years ago

You can debug the source code or use fiddler to see the raw response you're getting from the Hue Bridge.

Spaceva commented 4 years ago

So I debug using the source code and I found this line 117 of LocalHueClient-Api.cs

var username = result["success"]?["username"]?.Value<string>();
var streamingClientKey = result["success"]?["clientkey"]?.Value<string>();

if (username != null && streamingClientKey != null)
{
  return new RegisterEntertainmentResult()
  {
    Ip = ip,
    Username = username,
    StreamingClientKey = streamingClientKey
  };
}

That if is always false because streamingClientKey is null. I only get a "username", and not a "clientkey" on the JSON I get from the bridge.

The method RegisterAsync use false as value in the generateClientKey argument, because we don't ask for a Stream ApiKey (this is logic so far). But then we can't get a username, that is used afterwards for the requests.

public async Task<string?> RegisterAsync(string applicationName, string deviceName)
    {
      var result = await RegisterAsync(applicationName, deviceName, false);
      return result?.Username;
    }

I hope it helps you.

michielpost commented 4 years ago

Thanks! Fixed it in version 3.15.4 now on NuGet

Spaceva commented 4 years ago

You're welcome !