public async void Report() { try { ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls11 | SecurityProtocolType.Tls12; JavaScriptSerializer json = new JavaScriptSerializer(); HttpClient client = new HttpClient(); HttpRequestMessage req = new HttpRequestMessage(HttpMethod.Post, ReportUrl) { Content = new StringContent(json.Serialize(_gatherData()), Encoding.UTF8, "application/json") }; HttpResponseMessage res = await client.SendAsync(req); _plugin.WriteLog(ELogType.Trace, $"Analytics report: {res.StatusCode}"); req.Dispose(); res.Dispose(); client.Dispose(); } catch (Exception ex) { string errMsg = ex.Message; Exception inner = ex; while ((inner = inner.InnerException) != null) { errMsg += $" [{inner.Message}]"; } _plugin.WriteLog(ELogType.Trace, $"Analytics report: {errMsg}"); } }
public async Task <string> LoginWithPassword(string email, string password) { EmailPasswordLoginBody body = new EmailPasswordLoginBody { user = new EmailPasswordLoginBody.EmailPasswordUser { email = email, password = password, application = new EmailPasswordLoginBody.EmailPasswordUser.ApplicationCredentials { app_id = AppId, app_secret = AppSecret } } }; HttpRequestMessage req = new HttpRequestMessage(HttpMethod.Post, "https://user-field.aylanetworks.com/users/sign_in.json"); req.Headers.Add("Authorization", "none"); req.Content = new StringContent(_jsonSerializer.Serialize(body), Encoding.UTF8, "application/json"); try { _hs.WriteLog(ELogType.Debug, "Sending password login request"); HttpResponseMessage res = await _httpClient.SendAsync(req); HttpStatusCode statusCode = res.StatusCode; string responseString = await res.Content.ReadAsStringAsync(); dynamic response = _jsonSerializer.DeserializeObject(responseString); if (!res.IsSuccessStatusCode) { string errMsg = response["error"] ?? "Unspecified error"; req.Dispose(); res.Dispose(); return($"{errMsg} ({statusCode})"); } req.Dispose(); res.Dispose(); AccessToken = response["access_token"]; RefreshToken = response["refresh_token"]; int?expiresIn = response["expires_in"]; if (AccessToken == null || RefreshToken == null || expiresIn == null) { return($"Missing logon data ({statusCode})"); } TokenExpirationTime = DateTime.Now.AddSeconds((double)expiresIn); return(""); } catch (Exception ex) { string errMsg = ex.Message; Exception inner = ex; while ((inner = inner.InnerException) != null) { errMsg = $"{errMsg} [{inner.Message}]"; } req.Dispose(); return(errMsg); } }