Exemplo n.º 1
0
        private Task <string> ExecuteRequestInternal()
        {
            return(Task.Run(() =>
            {
                HttpWebRequest Request;
                String SigBaseStringParams = "";

                foreach (var item in QueryParameters)
                {
                    if (SigBaseStringParams != "")
                    {
                        SigBaseStringParams += "&";
                    }
                    SigBaseStringParams += item.Key + "=" + OAuthEncoding.Encode(item.Value);
                }

                Request =
                    (HttpWebRequest)
                    WebRequest.Create(Url +
                                      (String.IsNullOrEmpty(SigBaseStringParams)
                                                               ? ""
                                                               : "?" + SigBaseStringParams));
                Request.Method = Method.ToUpper();


                if (Method != "GET")
                {
                    if (!String.IsNullOrEmpty(ContentType))
                    {
                        Request.ContentType = ContentType;
                    }

                    if (Data != null)
                    {
                        using (var stream = Request.GetRequestStreamAsync().Result)
                        {
                            var bytes = Encoding.UTF8.GetBytes(Data);
                            stream.Write(bytes, 0, bytes.Length);
                        }
                    }
                }

                String authHeader = "OAuth";
                var orderedParameters = AuthParameters.OrderBy(d => d.Key);
                foreach (var item in orderedParameters)
                {
                    authHeader += (item.Key != orderedParameters.First().Key ? ", " : " ") +
                                  item.Key + "=\"" + OAuthEncoding.Encode(item.Value) + "\"";
                }

                Request.Headers["Authorization"] = authHeader;

                HttpWebResponse Response = (HttpWebResponse)Request.GetResponseAsync().Result;
                StreamReader ResponseDataStream = new StreamReader(Response.GetResponseStream());

                return ResponseDataStream.ReadToEnd();
            }));
        }
Exemplo n.º 2
0
        public OAuthRequest WithFormEncodedData(object data)
        {
            StringBuilder builder = new StringBuilder();

            if (data != null)
            {
#if WINMD
                foreach (var item in data.GetType().GetTypeInfo().DeclaredProperties)
                {
                    if (builder.Length > 0)
                    {
                        builder.Append("&");
                    }
                    var key = item.Name;
                    builder.Append(key);
                    object value = item.GetValue(data);

                    if (value != null)
                    {
                        var stringvalue = value.ToString();
                        FormParameters.Add(key, stringvalue);
                        builder.Append("=");
                        builder.Append(OAuthEncoding.Encode(stringvalue));
                    }
                }
#else
                foreach (var item in data.GetType().GetProperties())
                {
                    if (builder.Length > 0)
                    {
                        builder.Append("&");
                    }
                    var key = item.Name;
                    builder.Append(key);
                    object value = item.GetValue(data, null);

                    if (value != null)
                    {
                        var stringvalue = value.ToString();
                        FormParameters.Add(key, stringvalue);
                        builder.Append("=");
                        builder.Append(OAuthEncoding.Encode(stringvalue));
                    }
                }
#endif
            }
            Data = builder.ToString();

            return(SetContentTypeTo(@"application/x-www-form-urlencoded; charset: UTF-8"));
        }
Exemplo n.º 3
0
        public OAuthRequest Sign(string tokenSecret)
        {
            String SigBaseStringParams = "";
            var    orderedParameters   = AllParameters.OrderBy(d => d.Key);

            foreach (var item in orderedParameters)
            {
                if (SigBaseStringParams != "")
                {
                    SigBaseStringParams += "&";
                }
                SigBaseStringParams += item.Key + "=" + item.Value;
            }

            String SigBaseString = Method.ToUpper() + "&";

            SigBaseString += OAuthEncoding.Encode(Url.ToString()) + "&" + OAuthEncoding.Encode(SigBaseStringParams);

            String Signature;

#if WINMD
            IBuffer KeyMaterial = CryptographicBuffer.ConvertStringToBinary(Context.ConsumerToken.Secret + "&" + tokenSecret, BinaryStringEncoding.Utf8);
            MacAlgorithmProvider HmacSha1Provider = MacAlgorithmProvider.OpenAlgorithm("HMAC_SHA1");
            CryptographicKey     MacKey           = HmacSha1Provider.CreateKey(KeyMaterial);
            IBuffer DataToBeSigned  = CryptographicBuffer.ConvertStringToBinary(SigBaseString, BinaryStringEncoding.Utf8);
            IBuffer SignatureBuffer = CryptographicEngine.Sign(MacKey, DataToBeSigned);
            Signature = CryptographicBuffer.EncodeToBase64String(SignatureBuffer);
#else
            HMACSHA1     myhmacsha1 = new HMACSHA1(Encoding.UTF8.GetBytes(Context.ConsumerToken.Secret + "&" + tokenSecret));
            byte[]       byteArray  = Encoding.UTF8.GetBytes(SigBaseString);
            MemoryStream stream     = new MemoryStream(byteArray);
            byte[]       hashValue  = myhmacsha1.ComputeHash(stream);
            Signature = System.Convert.ToBase64String(hashValue);
#endif
            AuthParameters.Add(OAuthParameters.SIGNATURE, Signature);

            return(this);
        }