Q42.HueApi
Q42.HueApi copied to clipboard
Simple test exception help & other questions
Hey Michiel I'm trying to migrate to the newer HueApi content and I'm running into an issue.
using HueApi.Models;
using HueApi.Models.Requests;
using HueApi.ColorConverters.Original.Extensions;
...
public async void SimpleTest()
{
var bridge = (await new HueApi.BridgeLocator.LocalNetworkScanBridgeLocator().LocateBridgesAsync(TimeSpan.FromSeconds(15))).ToList()[0]; // Find (first) Hue Bridge
var registrationResult = HueApi.LocalHueApi.RegisterAsync(bridge.IpAddress, "MyApp", "MyDevice", true).Result; // Attempt to register application with first Hue Bridge found
var ip = registrationResult.Ip;
var key = registrationResult.StreamingClientKey;
if (ip == null || key == null) // Check if application registration was NOT successful (key data is NOT valid)
return;
Console.WriteLine($"Bridge IP Address = {ip}");
Console.WriteLine($"Application Key = {key}");
var localHueClient = new HueApi.LocalHueApi(ip, key); // Connect application as client to Hue Bridge
// Ideally the connection gets verified here
// Get light information
var lights = await localHueClient.GetLightsAsync(); // Get data for all lights from Hue Bridge
var id = lights.Data.Last().Id; // Pick a light from all retrieved lights
// Turn one light red
var req = new UpdateLight()
.TurnOn()
.SetColor(new HueApi.ColorConverters.RGBColor("FF0000"));
var result = await localHueClient.UpdateLightAsync(id, req);
await Task.Delay(TimeSpan.FromSeconds(5));
}
My bridge is found without issue. After pressing the 'Link' button on the bridge, a key (which is 32 alphanumeric characters) is generated. Any idea what I'm doing wrong?
Here are the error message details:
System.UnauthorizedAccessException
HResult=0x80070005
Message=Attempted to perform an unauthorized operation.
Source=HueApi
StackTrace:
at HueApi.BaseHueApi.<ProcessResponseAsync>d__142`1.MoveNext()
at HueApi.BaseHueApi.<HueGetRequestAsync>d__138`1.MoveNext()
at HueApiInterface.HueLightControl.<SimpleTest>d__20.MoveNext()