· 7 years ago · May 18, 2018, 08:56 PM
1public async Task<bool> Push(TwitterMessage twitterMessage)
2 {
3 const string updateApi = "http://api.twitter.com/1/statuses/update.json";
4 const string oauthConsumerKey = "<consumerKey>";
5 const string consumerSecret = "<consumerSecret>";
6 const string oauthSignatureMethod = "HMAC-SHA1";
7 const string oauthTokenSecret = "<tokenSecret>";
8 var signingKey = string.Format("{0}&{1}", consumerSecret.Escaped(), oauthTokenSecret.Escaped());
9
10 var postBody = "status=" + Uri.EscapeDataString(twitterMessage.MessageContent);
11 var oauthNonce = Convert.ToBase64String(new ASCIIEncoding().GetBytes(DateTime.Now.Ticks.ToString()));
12 var oauthToken = "<authenticatedUserToken>";
13 var timeSpan = DateTime.UtcNow - new DateTime(1970, 1, 1, 0, 0, 0, 0);
14 var oauthTimestamp = Convert.ToInt64(timeSpan.TotalSeconds).ToString();
15
16 var message = string.Format("POST {0}?{1} HTTP/1.1", updateApi, postBody.Escaped());
17 var hasher = new HMACSHA1(new ASCIIEncoding().GetBytes(signingKey));
18 var signatureString = Convert.ToBase64String(hasher.ComputeHash(new ASCIIEncoding().GetBytes(message)));
19
20 ServicePointManager.Expect100Continue = false;
21
22 var request = (HttpWebRequest)WebRequest.Create(updateApi);
23 request.KeepAlive = false;
24
25 var authorisationBuilder = new StringBuilder();
26 authorisationBuilder.Append("OAuth ");
27 authorisationBuilder.AppendFormat("oauth_consumer_key="{0}",", oauthConsumerKey.Escaped());
28 authorisationBuilder.AppendFormat("oauth_signature_method="{0}",", oauthSignatureMethod.Escaped());
29 authorisationBuilder.AppendFormat("oauth_timestamp="{0}",", oauthTimestamp.Escaped());
30 authorisationBuilder.AppendFormat("oauth_nonce="{0}",", oauthNonce.Escaped());
31 authorisationBuilder.AppendFormat("oauth_token="{0}",", oauthToken.Escaped());
32 authorisationBuilder.AppendFormat("oauth_signature="{0}"", signatureString.Escaped());
33 var authorisation = authorisationBuilder.ToString();
34 request.Headers.Add("Authorization", authorisation);
35
36 request.Method = "POST";
37 request.ContentType = "application/x-www-form-urlencoded";
38 using (var stream = await request.GetRequestStreamAsync())
39 {
40 var bodyBytes = new ASCIIEncoding().GetBytes(postBody);
41 stream.Write(bodyBytes, 0, bodyBytes.Length);
42 }
43
44 //Allow us a reasonable timeout in case Twitter's busy
45 request.Timeout = 3 * 60 * 1000;
46
47 try
48 {
49 var response = await request.GetResponseAsync() as HttpWebResponse;
50 return true;
51 }
52 catch (WebException)
53 {
54 return false;
55 }
56 }
57
58 public static string Escaped(this string input)
59 {
60 return Uri.EscapeDataString(input);
61 }
62
63protected void btnTweet_Click(object sender, EventArgs e)
64{
65 string oauthAccessToken = Session["twtoken"].ToString();
66 string oauthAccessTokenSecret = Session["twsecret"].ToString();
67
68 OAuthHelper oauthhelper = new OAuthHelper();
69 oauthhelper.TweetOnBehalfOf(oauthAccessToken, oauthAccessTokenSecret, txtTweet.Text);
70
71 if (string.IsNullOrEmpty(oauthhelper.oauth_error))
72 Response.Write("Twit Posted Successfully");
73 else
74 Response.Write(oauthhelper.oauth_error);
75}
76
77public async Task<bool> Push(TwitterAccount account)
78{
79 var twitterService = new TwitterService(consumerKey, consumerSecret);
80 twitterService.AuthenticateWith(account.AccessToken, account.AccessTokenSecret);
81 var options = new SendTweetOptions {Status = string.Format("{0} {1}", account.Message.MessageContent, account.Message.ShortLink)};
82 var status = twitterService.SendTweet(options);
83 return status != null;
84}