· 9 years ago · Sep 05, 2016, 09:20 AM
1public string signedSignature(string status, string postBody, string oauth_consumer_key, string oauth_nonce, string oauth_signature_method,
2 string oauth_token, string callbackURL, string oauth_timestamp, string oauth_version)
3 {
4
5 //GS - When building the signature string the params
6 //must be in alphabetical order. I can't be bothered
7 //with that, get SortedDictionary to do it's thing
8 SortedDictionary<string, string> sd =
9 new SortedDictionary<string, string>();
10
11 //sd.Add("status", status);
12 sd.Add("include_entities", "true");
13 sd.Add("oauth_consumer_key", oauth_consumer_key);
14 sd.Add("oauth_nonce", oauth_nonce);
15 sd.Add("oauth_signature_method", oauth_signature_method);
16 sd.Add("oauth_timestamp", oauth_timestamp);
17 sd.Add("oauth_token", oauth_token);
18 sd.Add("oauth_version", oauth_version);
19
20 //GS - Build the signature string
21 string baseString = String.Empty;
22 baseString += "POST" + "&";
23 baseString += Uri.EscapeDataString(
24 "https://api.twitter.com/oauth/request_token")
25 + "&";
26
27 foreach (KeyValuePair<string, string> entry in sd)
28 {
29 baseString += Uri.EscapeDataString(entry.Key +
30 "=" + entry.Value + "&");
31 }
32
33 //GS - Remove the trailing ambersand char, remember
34 //it's been urlEncoded so you have to remove the
35 //last 3 chars - %26
36 baseString =
37 baseString.Substring(0, baseString.Length - 3);
38
39 //GS - Build the signing key
40 string consumerSecret =
41 "<consumer Secret>";
42
43 string oauth_token_secret =
44 "<token secret>";
45
46 string signingKey =
47 Uri.EscapeDataString(consumerSecret) + "&" +
48 Uri.EscapeDataString(oauth_token_secret);
49
50 //GS - Sign the request
51 HMACSHA1 hasher = new HMACSHA1(
52 new ASCIIEncoding().GetBytes(signingKey));
53
54 string signatureString = Convert.ToBase64String(
55 hasher.ComputeHash(
56 new ASCIIEncoding().GetBytes(baseString)));
57
58 return signatureString;
59 }
60
61public ActionResult AccessToken()
62 {
63 //GS - Get the oAuth params
64 string status = "Hello Ladies + Gentlemen, a signed OAuth request!";
65 string postBody = "status=" +
66 Uri.EscapeDataString(status);
67
68 string oauth_consumer_key = "bidjtABOkF0b3mvw1UaHWDf7x";
69 string oauth_nonce = Convert.ToBase64String(
70 new ASCIIEncoding().GetBytes(
71 DateTime.Now.Ticks.ToString()));
72
73 string oauth_signature_method = "HMAC-SHA1";
74 string oauth_token =
75 "84473240-brz5BNw9r2WfbufzJ2WjaLysCBHmJjhjJxMVGz8Od";
76
77 string callbackURL = Uri.EscapeDataString("http://localhost:37808/");
78
79 TimeSpan ts = DateTime.UtcNow -
80 new DateTime(1970, 1, 1, 0, 0, 0, 0);
81
82 string oauth_timestamp =
83 Convert.ToInt64(ts.TotalSeconds).ToString();
84
85 string oauth_version = "1.0";
86
87 string sSig = signedSignature(status, postBody, oauth_consumer_key, oauth_nonce, oauth_signature_method, oauth_token,
88 callbackURL, oauth_timestamp, oauth_version);
89
90 //GS - Tell Twitter we don't do the 100 continue thing
91 ServicePointManager.Expect100Continue = false;
92
93 //GS - Instantiate a web request and populate the
94 //authorization header
95 HttpWebRequest hwr =
96 (HttpWebRequest)WebRequest.Create(
97 @"https://api.twitter.com/oauth/request_token");
98
99 string authorizationHeaderParams = String.Empty;
100 authorizationHeaderParams += "OAuth ";
101 authorizationHeaderParams += "oauth_nonce=" + """ +
102 Uri.EscapeDataString(oauth_nonce) + "",";
103
104 authorizationHeaderParams +=
105 "oauth_signature_method=" + """ +
106 Uri.EscapeDataString(oauth_signature_method) +
107 "",";
108 authorizationHeaderParams += "oauth_callback=" + """ +
109 callbackURL + "",";
110
111 authorizationHeaderParams += "oauth_timestamp=" + """ +
112 Uri.EscapeDataString(oauth_timestamp) + "",";
113
114 authorizationHeaderParams += "oauth_consumer_key="
115 + """ + Uri.EscapeDataString(
116 oauth_consumer_key) + "",";
117
118 authorizationHeaderParams += "oauth_signature=" + """
119 + Uri.EscapeDataString(sSig) + "",";
120
121 authorizationHeaderParams += "oauth_version=" + """ +
122 Uri.EscapeDataString(oauth_version) + """;
123
124 hwr.Headers.Add(
125 "Authorization", authorizationHeaderParams);
126
127 //GS - POST off the request
128 hwr.Method = "POST";
129 hwr.ContentType = "application/x-www-form-urlencoded";
130 Stream stream = hwr.GetRequestStream();
131 byte[] bodyBytes =
132 new ASCIIEncoding().GetBytes(postBody);
133
134 stream.Write(bodyBytes, 0, bodyBytes.Length);
135 stream.Flush();
136 stream.Close();
137
138 //GS - Allow us a reasonable timeout in case
139 //Twitter's busy
140 hwr.Timeout = 3 * 60 * 1000;
141
142 try
143 {
144 HttpWebResponse rsp = hwr.GetResponse()
145 as HttpWebResponse;
146 rsp.StatusCode.ToString();
147 //GS - Do something with the return here...
148 }
149 catch (WebException e)
150 {
151 //GS - Do some clever error handling here...
152 }
153
154 return View();
155 }