Exemplo n.º 1
0
        /// <summary>
        /// Logs in to the Logitech Harmony web service to get a UserAuthToken.
        /// </summary>
        /// <param name="username">myharmony.com username</param>
        /// <param name="password">myharmony.com password</param>
        /// <returns>Logitech UserAuthToken</returns>
        public HarmonyAuthToken GetUserAuthToken(string username, string password)
        {
            try
            {
                if (harmonyAuthToken == null)
                {
                    const string logitechAuthUrl = "https://svcs.myharmony.com/CompositeSecurityServices/Security.svc/json/GetUserAuthToken";

                    var httpWebRequest = (HttpWebRequest)WebRequest.Create(logitechAuthUrl);
                    httpWebRequest.ContentType = "text/json";
                    httpWebRequest.Method      = "POST";

                    Task <Stream> request = httpWebRequest.GetRequestStreamAsync();
                    request.Wait(10000);
                    using (StreamWriter sw = new StreamWriter(request.Result))
                    {
                        sw.Write(JsonConvert.SerializeObject(new { email = username, password = password }, Formatting.Indented));
                        sw.Flush();
                    }

                    Task <WebResponse> response = httpWebRequest.GetResponseAsync();
                    response.Wait(10000);

                    var responseStream = response.Result.GetResponseStream();
                    if (responseStream == null)
                    {
                        return(null);
                    }

                    using (var streamReader = new StreamReader(responseStream))
                    {
                        var result = streamReader.ReadToEnd();
                        harmonyAuthToken = JsonConvert.DeserializeObject <GetUserAuthTokenResultRootObject>(result).GetUserAuthTokenResult;
                    }
                }
            }
            catch (Exception)
            {
            }

            return(harmonyAuthToken);
        }
Exemplo n.º 2
0
        /// <summary>
        /// Logs in to the Logitech Harmony web service to get a UserAuthToken.
        /// </summary>
        /// <param name="username">myharmony.com username</param>
        /// <param name="password">myharmony.com password</param>
        /// <returns>Logitech UserAuthToken</returns>
        public HarmonyAuthToken GetUserAuthToken(string username, string password)
        {
            try
            {
                if (harmonyAuthToken == null)
                {
                    const string logitechAuthUrl = "https://svcs.myharmony.com/CompositeSecurityServices/Security.svc/json/GetUserAuthToken";

                    var httpWebRequest = (HttpWebRequest)WebRequest.Create(logitechAuthUrl);
                    httpWebRequest.ContentType = "text/json";
                    httpWebRequest.Method = "POST";

                    Task<Stream> request = httpWebRequest.GetRequestStreamAsync();
                    request.Wait(10000);
                    using (StreamWriter sw = new StreamWriter(request.Result))
                    {
                        sw.Write(JsonConvert.SerializeObject(new { email = username, password = password }, Formatting.Indented));
                        sw.Flush();
                    }

                    Task<WebResponse> response = httpWebRequest.GetResponseAsync();
                    response.Wait(10000);

                    var responseStream = response.Result.GetResponseStream();
                    if (responseStream == null)
                        return null;

                    using (var streamReader = new StreamReader(responseStream))
                    {
                        var result = streamReader.ReadToEnd();
                        harmonyAuthToken = JsonConvert.DeserializeObject<GetUserAuthTokenResultRootObject>(result).GetUserAuthTokenResult;
                    }
                }
            }
            catch (Exception)
            {
            }

            return harmonyAuthToken;
        }