Created
May 8, 2013 00:05
-
-
Save smockle/5537203 to your computer and use it in GitHub Desktop.
Tweets uses HTTPWebRequest to authenticate and post to Twitter.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* Submit to Twitter. */ | |
public void Twitter(Post post) { | |
// Get the OAuth params. | |
string status = post.title + " http://blog.smockle.com/" + post.id; | |
string postBody = "status=" + Uri.EscapeDataString(status); | |
string oauth_consumer_key = Properties.Settings.Default.Twitter_Consumer_Key; | |
string oauth_nonce = Convert.ToBase64String(new ASCIIEncoding().GetBytes(DateTime.Now.Ticks.ToString())); | |
string oauth_signature_method = "HMAC-SHA1"; | |
string oauth_token = Properties.Settings.Default.Twitter_Token; | |
TimeSpan ts = DateTime.UtcNow - new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc); | |
string oauth_timestamp = Convert.ToInt64(ts.TotalSeconds).ToString(); | |
string oauth_version = "1.0"; | |
// When building the signature string the params must be in alphabetical order. | |
SortedDictionary<string, string> sd = new SortedDictionary<string, string>(); | |
sd.Add("status", Uri.EscapeDataString(status)); | |
sd.Add("oauth_version", oauth_version); | |
sd.Add("oauth_consumer_key", oauth_consumer_key); | |
sd.Add("oauth_nonce", oauth_nonce); | |
sd.Add("oauth_signature_method", oauth_signature_method); | |
sd.Add("oauth_timestamp", oauth_timestamp); | |
sd.Add("oauth_token", oauth_token); | |
// Build the signature string. | |
string baseString = String.Empty; | |
baseString += "POST" + "&"; | |
baseString += Uri.EscapeDataString("https://api.twitter.com/1.1/statuses/update.json") + "&"; | |
foreach (KeyValuePair<string, string> entry in sd) { | |
baseString += Uri.EscapeDataString(entry.Key + "=" + entry.Value + "&"); | |
} | |
// Remove the trailing ampersand char, remember it's been urlEncoded so you have to remove the last 3 chars - %26. | |
baseString = baseString.Substring(0, baseString.Length - 3); | |
// Build the signing key. | |
string consumerSecret = Properties.Settings.Default.Twitter_Consumer_Secret; | |
string oauth_token_secret = Properties.Settings.Default.Twitter_Token_Secret; | |
string signingKey = Uri.EscapeDataString(consumerSecret) + "&" + Uri.EscapeDataString(oauth_token_secret); | |
// Sign the request. | |
HMACSHA1 hasher = new HMACSHA1(new ASCIIEncoding().GetBytes(signingKey)); | |
string signatureString = Convert.ToBase64String(hasher.ComputeHash(new ASCIIEncoding().GetBytes(baseString))); | |
// Tell Twitter we don't do the 100 continue thing. | |
ServicePointManager.Expect100Continue = false; | |
// Instantiate a web request and populate the authorization header. | |
HttpWebRequest hwr = (HttpWebRequest)WebRequest.Create(@"https://api.twitter.com/1.1/statuses/update.json"); | |
hwr.KeepAlive = false; | |
string authorizationHeaderParams = String.Empty; | |
authorizationHeaderParams += "OAuth "; | |
authorizationHeaderParams += "oauth_consumer_key=" + "\"" + Uri.EscapeDataString(oauth_consumer_key) + "\","; | |
authorizationHeaderParams += "oauth_nonce=" + "\"" + Uri.EscapeDataString(oauth_nonce) + "\","; | |
authorizationHeaderParams += "oauth_signature=" + "\"" + Uri.EscapeDataString(signatureString) + "\","; | |
authorizationHeaderParams += "oauth_signature_method=" + "\"" + Uri.EscapeDataString(oauth_signature_method) + "\","; | |
authorizationHeaderParams += "oauth_timestamp=" + "\"" + Uri.EscapeDataString(oauth_timestamp) + "\","; | |
authorizationHeaderParams += "oauth_token=" + "\"" + Uri.EscapeDataString(oauth_token) + "\","; | |
authorizationHeaderParams += "oauth_version=" + "\"" + Uri.EscapeDataString(oauth_version) + "\""; | |
hwr.Headers.Add("Authorization", authorizationHeaderParams); | |
// POST off the request. | |
hwr.Method = "POST"; | |
hwr.ContentType = "application/x-www-form-urlencoded"; | |
Stream stream = hwr.GetRequestStream(); | |
byte[] bodyBytes = new ASCIIEncoding().GetBytes(postBody); | |
stream.Write(bodyBytes, 0, bodyBytes.Length); | |
stream.Flush(); | |
stream.Close(); | |
// Allow us a reasonable timeout in case Twitter is busy. | |
hwr.Timeout = 3 * 60 * 1000; | |
try { | |
HttpWebResponse rsp = hwr.GetResponse() as HttpWebResponse; | |
} | |
catch (WebException e) { | |
// Do some clever error handling here. | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment