internal static Dictionary <string, string> Get(string url, List <KeyValuePair <string, string> > queryParams)
        {
            Dictionary <string, string> result;

            try
            {
                StringBuilder stringBuilder = new StringBuilder();
                if (queryParams.Count > 0)
                {
                    stringBuilder.AppendFormat("?{0}={1}", queryParams[0].Key, HttpUtility.UrlEncode(queryParams[0].Value));
                }
                for (int i = 1; i < queryParams.Count; i++)
                {
                    stringBuilder.AppendFormat("&{0}={1}", queryParams[i].Key, HttpUtility.UrlEncode(queryParams[i].Value));
                }
                string         str            = stringBuilder.ToString();
                HttpWebRequest httpWebRequest = (HttpWebRequest)WebRequest.Create(url + str);
                httpWebRequest.Method      = "GET";
                httpWebRequest.Timeout     = 10000;
                httpWebRequest.ContentType = "application/x-www-form-urlencoded";
                using (WebResponse response = httpWebRequest.GetResponse())
                {
                    using (StreamReader streamReader = new StreamReader(response.GetResponseStream()))
                    {
                        string text = streamReader.ReadToEnd();
                        result = JsonConvert.DeserializeObject <Dictionary <string, string> >(text);
                    }
                }
            }
            catch (WebException ex)
            {
                if (ex.Response != null)
                {
                    using (StreamReader streamReader2 = new StreamReader(ex.Response.GetResponseStream()))
                    {
                        string text2 = streamReader2.ReadToEnd();
                        result = JsonConvert.DeserializeObject <Dictionary <string, string> >(text2);
                        return(result);
                    }
                }
                result = HttpRequestHelper.HttpRequestErrorByMessage(ex.Message);
            }
            catch (Exception ex2)
            {
                result = HttpRequestHelper.HttpRequestErrorByMessage(ex2.Message);
            }
            return(result);
        }
Ejemplo n.º 2
0
        public Dictionary <string, string> GetAccessToken(string code)
        {
            List <KeyValuePair <string, string> > list = new List <KeyValuePair <string, string> >();

            list.Add(new KeyValuePair <string, string>("client_id", Config.ClientName));
            list.Add(new KeyValuePair <string, string>("client_secret", Config.Credentials));
            list.Add(new KeyValuePair <string, string>("redirect_uri", Config.CallbackUrl));
            list.Add(new KeyValuePair <string, string>("grant_type", "authorization_code"));
            list.Add(new KeyValuePair <string, string>("code", code));
            Dictionary <string, string> dictionary = HttpRequestHelper.Post(Config.TokenPath, list);

            if (dictionary != null && dictionary.ContainsKey("error"))
            {
                this.errorInfo   = dictionary;
                this.accessToken = null;
            }
            else
            {
                this.errorInfo   = null;
                this.accessToken = dictionary;
            }
            return(this.accessToken);
        }