private static string MakeSignature(string method, string baseUri, Dictionary <string, string> headers)
        {
            var items = from k in headers.Keys
                        orderby k
                        select k + "%3D" + OAuthUtils.PercentEncode(headers[k]);

            return(method + "&" + OAuthUtils.PercentEncode(baseUri) + "&" +
                   string.Join("%26", items.ToArray()));
        }
        public static string AuthorizeRequest(string consumerKey, string consumerKeySecret, string oauthToken,
                                              string oauthTokenSecret, string method, Uri uri, string data)
        {
            var headers = new Dictionary <string, string>
            {
                { "oauth_consumer_key", consumerKey },
                { "oauth_nonce", MakeNonce() },
                { "oauth_signature_method", "HMAC-SHA1" },
                { "oauth_timestamp", MakeTimestamp() },
                { "oauth_version", "1.0" }
            };
            var signatureHeaders = new Dictionary <string, string>(headers);

            if (!string.IsNullOrEmpty(oauthToken))
            {
                signatureHeaders.Add("oauth_token", oauthToken);
            }

            // Add the data and URL query string to the copy of the headers for computing the signature
            if (!string.IsNullOrEmpty(data))
            {
                var parsed = HttpUtility.ParseQueryString(data);
                foreach (string k in parsed.Keys)
                {
                    signatureHeaders.Add(k, OAuthUtils.PercentEncode(parsed[k]));
                }
            }

            var nvc = HttpUtility.ParseQueryString(uri.Query);

            foreach (var key in nvc.Cast <string>().Where(key => key != null))
            {
                signatureHeaders.Add(key, OAuthUtils.PercentEncode(nvc[key]));
            }

            var signature           = MakeSignature(method, uri.GetLeftPart(UriPartial.Path), signatureHeaders);
            var compositeSigningKey = MakeSigningKey(consumerKeySecret, oauthTokenSecret);
            var oauthSignature      = MakeOAuthSignature(compositeSigningKey, signature);

            headers.Add("oauth_signature", OAuthUtils.PercentEncode(oauthSignature));

            return(HeadersToOAuth(headers));
        }
Esempio n. 3
0
        public static string PostToUrl(TwitterAuthProvider oAuthProvider, string url, string accessToken, string accessTokenSecret, Dictionary <string, string> args, string acceptType = MimeTypes.Json)
        {
            var uri    = new Uri(url);
            var webReq = (HttpWebRequest)WebRequest.Create(uri);

            webReq.Accept = acceptType;
            webReq.Method = HttpMethods.Post;

            string data = null;

            if (args != null)
            {
                var sb = new StringBuilder();
                foreach (var arg in args)
                {
                    if (sb.Length > 0)
                    {
                        sb.Append("&");
                    }
                    sb.AppendFormat("{0}={1}", arg.Key, OAuthUtils.PercentEncode(arg.Value));
                }
                data = sb.ToString();
            }

            webReq.Headers[HttpRequestHeader.Authorization] = OAuthAuthorizer.AuthorizeRequest(
                oAuthProvider, accessToken, accessTokenSecret, "POST", uri, data);

            if (data != null)
            {
                webReq.ContentType = MimeTypes.FormUrlEncoded;
                using (var writer = new StreamWriter(webReq.GetRequestStream()))
                    writer.Write(data);
            }

            using (var webRes = webReq.GetResponse())
            {
                return(webRes.ReadToEnd());
            }
        }
 private static string MakeSigningKey(string consumerSecret, string oauthTokenSecret)
 {
     return(OAuthUtils.PercentEncode(consumerSecret) + "&" +
            (oauthTokenSecret != null ? OAuthUtils.PercentEncode(oauthTokenSecret) : ""));
 }