Working through the API, and I can't get UpdateDeviceMetaData to work. It returns success, but does not change the name or location.
Thanks for the help on this. The API is very nice to use.
Code: Select all
using System;
using System.Net;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
class MainClass
{
const string zcommandUrl = "https://192.168.2.160";
public static int Main(string[] args)
{
// this is to allow the default certificate on the ZCommand module to pass. otherwise we'll keep getting exceptions.
ServicePointManager.ServerCertificateValidationCallback
= ((sender, cert, chain, errors) => cert.Subject.ToLower().Contains("zcommand"));
WebClient ZCommandWebClient = GetAuthenticatedWebClient();
var form = JsonConvert.SerializeObject(new { name = "Office Tester", location = "In the Office" });
var response = ZCommandWebClient.UploadString(string.Format("{0}/api/updatedevicemetadata/4", zcommandUrl), form);
Console.WriteLine("Response: {0}", response);
Console.ReadLine();
return 0;
}
private static WebClient GetAuthenticatedWebClient()
{
WebClient webClient = new WebClient();
webClient.Headers.Set(HttpRequestHeader.UserAgent, "zcommand");
var form = JsonConvert.SerializeObject(new { Username = "admin", Password = "admin" });
var response = webClient.UploadString(zcommandUrl + "/api/tokenauth", form);
var obj = JObject.Parse(response);
webClient = createAuthorizedWebClient(obj["responseObject"].ToString());
return webClient;
}
private static WebClient createAuthorizedWebClient(string token)
{
WebClient webClient = new WebClient();
webClient.Headers.Set(HttpRequestHeader.UserAgent, "zcommand");
webClient.CachePolicy = new System.Net.Cache.RequestCachePolicy(System.Net.Cache.RequestCacheLevel.NoCacheNoStore);
webClient.Headers.Add(HttpRequestHeader.Authorization, "Token " + token);
return webClient;
}
}