[twitter-dev] Authentication failing for status POST in C#
So far as I can tell, I'm following the instructions accurately according to http://dev.twitter.com/pages/auth, but regardless of any minute variations, I keep getting a 401, authorization error on posting (i.e. the method pushMessage() fails). So far as I can tell, I'm not doing anything different, other than using POST, than I did during the initial authorization. public class Twitter { private string tokenSecret = ; private string createTimestamp() { var now = DateTime.UtcNow; var then = new DateTime(1970, 1, 1); var timespan = now - then; return ( + (long)timespan.TotalSeconds); } private string createNonce() { const string ALPHANUMERIC = qwertyuiopasdfghjklzxcvbnm + QWERTYUIOPASDFGHJKLZXCVBNM + 1234567890 ; var sb = new StringBuilder(); var random = new Random(); for (int L = 0; L 42; L++) { sb.Append( ALPHANUMERIC[random.Next(ALPHANUMERIC.Length)] ); } return sb.ToString(); } private string createSignature(string signatureBase) { string key = clientSecret + + tokenSecret; var keyBytes = Encoding.UTF8.GetBytes(key); var sigMethod = new HMACSHA1(keyBytes); byte[] data = Encoding.UTF8.GetBytes(signatureBase); var hash = sigMethod.ComputeHash(data); var sig = Convert.ToBase64String(hash); sig = Uri.EscapeDataString(sig); return sig; } private void myInit() { int found = 0; string[] words = sessionData.Split(''); for (uint L = 0; L words.Length; L++) { if (words[L].StartsWith(oauth_token=)) { ++found; accessToken = words[L]; } else if (words[L].StartsWith(oauth_token_secret=)) { ++found; tokenSecret = words[L].Substring(oauth_token_secret=.Length); } else if (words[L].StartsWith(user_id=)) { ++found; userID = words[L].Substring(user_id=.Length); } else if (words[L].StartsWith(screen_name=)) { ++found; screenName = words[L].Substring(screen_name=.Length); } } if (found != 4) { throw new Exception(Unknown response from server); } } public new void createSession(System.Web.UI.Page page) { if ( !string.IsNullOrEmpty(page.Request.QueryString[oauth_token]) ) { string timestamp = createTimestamp(); string nonce = createNonce(); var sigBase = GET + + Uri.EscapeDataString(https://api.twitter.com/oauth/ access_token) + + Uri.EscapeDataString(oauth_consumer_key= + clientID) + %26 + Uri.EscapeDataString(oauth_nonce= + nonce) + %26 + Uri.EscapeDataString(oauth_signature_method=HMAC-SHA1) + %26 + Uri.EscapeDataString(oauth_timestamp= + timestamp) + %26 + Uri.EscapeDataString(oauth_token= + page.Request.QueryString[oauth_token]) + %26 + Uri.EscapeDataString(oauth_version=1.0) ; string signature = createSignature(sigBase); string url = https://api.twitter.com/oauth/access_token;; WebClient client = new WebClient(); client.Headers[Authorization] = OAuth + oauth_consumer_key=\ + clientID + \, + oauth_nonce=\ + nonce + \, + oauth_signature=\ + signature + \, + oauth_signature_method=\HMAC-SHA1\, + oauth_timestamp=\ + timestamp + \, + oauth_token=\ + page.Request.QueryString[oauth_token] + \, + oauth_version=\1.0\ ; byte[] data
Re: [twitter-dev] Authentication failing for status POST in C#
I like code, but I like seeing the results even more :-) Can you provide us (the twitter-dev list) with these : * A base string * A full request and response Tom On 9/10/10 8:22 PM, hyronymous wrote: So far as I can tell, I'm following the instructions accurately according to http://dev.twitter.com/pages/auth, but regardless of any minute variations, I keep getting a 401, authorization error on posting (i.e. the method pushMessage() fails). So far as I can tell, I'm not doing anything different, other than using POST, than I did during the initial authorization. public class Twitter { private string tokenSecret = ; private string createTimestamp() { var now = DateTime.UtcNow; var then = new DateTime(1970, 1, 1); var timespan = now - then; return ( + (long)timespan.TotalSeconds); } private string createNonce() { const string ALPHANUMERIC = qwertyuiopasdfghjklzxcvbnm + QWERTYUIOPASDFGHJKLZXCVBNM + 1234567890 ; var sb = new StringBuilder(); var random = new Random(); for (int L = 0; L 42; L++) { sb.Append( ALPHANUMERIC[random.Next(ALPHANUMERIC.Length)] ); } return sb.ToString(); } private string createSignature(string signatureBase) { string key = clientSecret + + tokenSecret; var keyBytes = Encoding.UTF8.GetBytes(key); var sigMethod = new HMACSHA1(keyBytes); byte[] data = Encoding.UTF8.GetBytes(signatureBase); var hash = sigMethod.ComputeHash(data); var sig = Convert.ToBase64String(hash); sig = Uri.EscapeDataString(sig); return sig; } private void myInit() { int found = 0; string[] words = sessionData.Split(''); for (uint L = 0; L words.Length; L++) { if (words[L].StartsWith(oauth_token=)) { ++found; accessToken = words[L]; } else if (words[L].StartsWith(oauth_token_secret=)) { ++found; tokenSecret = words[L].Substring(oauth_token_secret=.Length); } else if (words[L].StartsWith(user_id=)) { ++found; userID = words[L].Substring(user_id=.Length); } else if (words[L].StartsWith(screen_name=)) { ++found; screenName = words[L].Substring(screen_name=.Length); } } if (found != 4) { throw new Exception(Unknown response from server); } } public new void createSession(System.Web.UI.Page page) { if ( !string.IsNullOrEmpty(page.Request.QueryString[oauth_token]) ) { string timestamp = createTimestamp(); string nonce = createNonce(); var sigBase = GET + + Uri.EscapeDataString(https://api.twitter.com/oauth/ access_token) + + Uri.EscapeDataString(oauth_consumer_key= + clientID) + %26 + Uri.EscapeDataString(oauth_nonce= + nonce) + %26 + Uri.EscapeDataString(oauth_signature_method=HMAC-SHA1) + %26 + Uri.EscapeDataString(oauth_timestamp= + timestamp) + %26 + Uri.EscapeDataString(oauth_token= + page.Request.QueryString[oauth_token]) + %26 + Uri.EscapeDataString(oauth_version=1.0) ; string signature = createSignature(sigBase); string url = https://api.twitter.com/oauth/access_token;; WebClient client = new WebClient(); client.Headers[Authorization] = OAuth + oauth_consumer_key=\ + clientID + \, + oauth_nonce=\ + nonce + \, + oauth_signature=\ + signature + \, + oauth_signature_method=\HMAC-SHA1\, + oauth_timestamp=\ + timestamp + \, + oauth_token=\ +