· 6 years ago · Mar 11, 2019, 12:04 PM
1public void MssSignUploadRequest(string ssConsumerKey, string ssConsumerSecret, string ssOAuthToken, string ssOAuthTokenSecret) {
2 // TODO: Write implementation for action
3 // This code is based on
4 // http://garyshortblog.wordpress.com/2011/02/11/a-twitter-oauth-example-in-c/
5
6 RestRequest context = RestRequest.GetCurrent();
7
8 // Get the current http request, we will be adding the authorization header to this object
9 HttpWebRequest request = context.GetHttpWebRequest();
10
11 // Get the request body, that is also used to compute the signature
12 //string requestBody = context.GetRequestBodyAsText();
13
14 string oauth_signature_method = "HMAC-SHA1";
15
16
17 string oauth_nonce = Convert.ToBase64String(new ASCIIEncoding().GetBytes(DateTime.UtcNow.Ticks.ToString()))
18 .Replace("=", "").Replace("+", "");
19
20 TimeSpan ts = DateTime.UtcNow - new DateTime(1970, 1, 1, 0, 0, 0, 0);
21
22 string oauth_timestamp = Convert.ToInt64(ts.TotalSeconds).ToString();
23
24 string oauth_version = "1.0";
25
26 //When building the signature string the params
27 //must be in alphabetical order.
28
29 SortedDictionary<string, string> sd = new SortedDictionary<string, string>();
30
31 // add all parameters specified in the body
32 /*
33 var parsedQueryString = HttpUtility.ParseQueryString(requestBody);
34 foreach (var name in parsedQueryString.AllKeys)
35 {
36 sd.Add(name, PercentEncode(parsedQueryString[name]));
37 }
38
39 // add all parameters specified in the querystring
40 parsedQueryString = HttpUtility.ParseQueryString(request.RequestUri.Query);
41 foreach (var name in parsedQueryString.AllKeys)
42 {
43 sd.Add(name, PercentEncode(parsedQueryString[name]));
44 }*/
45
46 string auth_callback = null;
47
48 sd.Add("oauth_version", oauth_version);
49 if (request.Headers["oauth_callback"] != null)
50 {
51 auth_callback = request.Headers["oauth_callback"];
52 sd.Add("oauth_callback", PercentEncode(auth_callback));
53 request.Headers.Remove("oauth_callback");
54 }
55 sd.Add("oauth_consumer_key", ssConsumerKey);
56 sd.Add("oauth_nonce", oauth_nonce);
57 sd.Add("oauth_signature_method", oauth_signature_method);
58 sd.Add("oauth_timestamp", oauth_timestamp);
59 if (!sd.ContainsKey("oauth_token"))
60 {
61 sd.Add("oauth_token", ssOAuthToken);
62 }
63
64
65 /*
66 * StringBuilder baseString = new StringBuilder(request.Method + "&" + PercentEncode(NormalizeUrl(request.RequestUri)) + "&");
67 foreach (var keyValuePair in sd)
68 {
69 // these & need to be UrlEncoded, but the previous ones do not!
70 baseString.Append(PercentEncode(string.Format("{0}={1}&", keyValuePair.Key, keyValuePair.Value)));
71 }
72 string signatureBaseString = baseString.ToString().Substring(0, baseString.Length - 3);
73 */
74 // Build the signing key
75
76 // Build the signing key
77 string signingKey =
78 PercentEncode(ssConsumerSecret) + "&" +
79 PercentEncode(ssOAuthTokenSecret);
80
81 string signatureString = ComputeSignature(signingKey, "");
82
83
84
85
86 /**************************************/
87 // Build the authorization header
88 string authorizationHeaderParams = "OAuth ";
89 authorizationHeaderParams += "oauth_nonce=" + "\"" + PercentEncode(oauth_nonce) + "\", ";
90
91 if (auth_callback != null)
92 {
93 authorizationHeaderParams += "oauth_callback=" + "\"" + PercentEncode(auth_callback) + "\", ";
94 }
95
96 if (sd.ContainsKey("oauth_verifier"))
97 {
98 authorizationHeaderParams += "oauth_verifier=" + "\"" + PercentEncode(sd["oauth_verifier"]) + "\", ";
99 }
100
101 authorizationHeaderParams += "oauth_signature_method=" + "\"" + PercentEncode(oauth_signature_method) + "\", ";
102
103 authorizationHeaderParams += "oauth_timestamp=" + "\"" + PercentEncode(oauth_timestamp) + "\", ";
104
105 authorizationHeaderParams += "oauth_consumer_key=" + "\"" + PercentEncode(ssConsumerKey) + "\", ";
106
107 authorizationHeaderParams += "oauth_token=" + "\"" + PercentEncode(ssOAuthToken) + "\", ";
108
109 authorizationHeaderParams += "oauth_signature=" + "\"" + PercentEncode(signatureString) + "\", ";
110
111 authorizationHeaderParams += "oauth_version=" + "\"" + PercentEncode(oauth_version) + "\"";
112
113 request.Headers.Remove("Authorization");
114 request.Headers.Add("Authorization", authorizationHeaderParams);
115 } // MssSignUploadRequest
116
117
118private static string ComputeSignature(string signingKey, string text)
119 {
120 // Sign the request
121 HMACSHA1 hasher = new HMACSHA1(new ASCIIEncoding().GetBytes(signingKey));
122 return Convert.ToBase64String(hasher.ComputeHash(new ASCIIEncoding().GetBytes(text)));
123 }