Skip to content

Instantly share code, notes, and snippets.

@aawaheed
Last active September 15, 2019 12:14
Show Gist options
  • Save aawaheed/41f248365b1b401f3d7db52969d38f0e to your computer and use it in GitHub Desktop.
Save aawaheed/41f248365b1b401f3d7db52969d38f0e to your computer and use it in GitHub Desktop.
To send tweet using pure HTTP post with OAuth in C#
//To send tweet using OAuth without any library
private void SendTweet(string message)
{
try
{
string _CK = "ConsumerKey"; //YOUR CONSUMER KEY
string _CS = "ConsumerSecret" //YOUR CONSUMER SECRET
string _AT = "AccessToken" //Access Token
string _ATS = "AccessTokenSecret " //Access Token Secret
string authHeader = GenerateAuthorizationHeader(message);
string postBody = "status=" + Uri.EscapeDataString(message);
HttpWebRequest authRequest = (HttpWebRequest)WebRequest.Create(oAuthUrl);
authRequest.Headers.Add("Authorization", authHeader);
authRequest.Method = "POST";
authRequest.UserAgent = "OAuth gem v0.4.4";
authRequest.Host = "api.twitter.com";
authRequest.ContentType = "application/x-www-form-urlencoded;charset=UTF-8";
authRequest.ServicePoint.Expect100Continue = false;
authRequest.AutomaticDecompression = DecompressionMethods.GZip | DecompressionMethods.Deflate;
ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12;
using (Stream stream = authRequest.GetRequestStream())
{
byte[] content = Encoding.UTF8.GetBytes(postBody);
stream.Write(content, 0, content.Length);
}
WebResponse authResponse = authRequest.GetResponse();
authResponse.Close();
}
catch (System.Exception ea)
{
//handle exception
}
}
private string GenerateAuthorizationHeader(string status)
{
string signatureMethod = "HMAC-SHA1";
string version = "1.0";
string nonce = GenerateNonce();
double timestamp = ConvertToUnixTimestamp(DateTime.Now);
string dst = string.Empty;
dst = string.Empty;
dst += "OAuth ";
dst += string.Format("oauth_consumer_key=\"{0}\", ", Uri.EscapeDataString(_CK));
dst += string.Format("oauth_nonce=\"{0}\", ", Uri.EscapeDataString(nonce));
dst += string.Format("oauth_signature=\"{0}\", ", Uri.EscapeDataString(GenerateOauthSignature(status, nonce, timestamp.ToString())));
dst += string.Format("oauth_signature_method=\"{0}\", ", Uri.EscapeDataString(signatureMethod));
dst += string.Format("oauth_timestamp=\"{0}\", ", timestamp);
dst += string.Format("oauth_token=\"{0}\", ", Uri.EscapeDataString(_AT));
dst += string.Format("oauth_version=\"{0}\"", Uri.EscapeDataString(version));
return dst;
}
private string GenerateOauthSignature(string status, string nonce, string timestamp)
{
string signatureMethod = "HMAC-SHA1";
string version = "1.0";
string result = string.Empty;
string dst = string.Empty;
dst += string.Format("oauth_consumer_key={0}&", Uri.EscapeDataString(_CK));
dst += string.Format("oauth_nonce={0}&", Uri.EscapeDataString(nonce));
dst += string.Format("oauth_signature_method={0}&", Uri.EscapeDataString(signatureMethod));
dst += string.Format("oauth_timestamp={0}&", timestamp);
dst += string.Format("oauth_token={0}&", Uri.EscapeDataString(_AT));
dst += string.Format("oauth_version={0}&", Uri.EscapeDataString(version));
dst += string.Format("status={0}", Uri.EscapeDataString(status));
string signingKey = string.Empty;
signingKey = string.Format("{0}&{1}", Uri.EscapeDataString(_CS), Uri.EscapeDataString(_ATS));
result += "POST&";
result += Uri.EscapeDataString(oAuthUrl);
result += "&";
result += Uri.EscapeDataString(dst);
HMACSHA1 hmac = new HMACSHA1();
hmac.Key = Encoding.UTF8.GetBytes(signingKey);
byte[] databuff = System.Text.Encoding.UTF8.GetBytes(result);
byte[] hashbytes = hmac.ComputeHash(databuff);
return Convert.ToBase64String(hashbytes);
}
private string GenerateNonce()
{
string nonce = string.Empty;
var rand = new Random();
int next = 0;
for (var i = 0; i < 32; i++)
{
next = rand.Next(65, 90);
char c = Convert.ToChar(next);
nonce += c;
}
return nonce;
}
public static double ConvertToUnixTimestamp(DateTime date)
{
DateTime origin = new DateTime(1970, 1, 1, 0, 0, 0, 0);
TimeSpan diff = date.ToUniversalTime() - origin;
return Math.Floor(diff.TotalSeconds);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment