C# Windows Form post status update on Twitter -
c# windows form post status update on twitter
i have tried solutions can find , pieced following code gives 401 authorisation error.
when set twitter application in twitter asked callback url, developing on local machine , need use windows forms tweets can data local database. want tweet own account data database insert tweets when add new items.
i have tried changing encoding utf8 (left previous string definitions commented out show where), after reading problem somewhere.
i not know if there wrong authorisation have required keys or if callback url, , if can use windows form client no website.
if paste curl dev.twitter site browser "bad authentication data" when using https://api.twitter.com/1.1/statuses/update.json (it defaults 1 , gives message saying has been deprecated). website have entered callback url mine, not know if there needs code there test function.
any appreciated have been trying resolve week now.
public static void anothertweet(string status) { //gs - oauth params //string status = status; string postbody = "status=" + uri.escapedatastring(status); string oauth_consumer_key = "xxx"; //string oauth_nonce = convert.tobase64string( //new asciiencoding().getbytes( // datetime.now.ticks.tostring())); string oauth_nonce = convert.tobase64string(system.text.encoding.utf8.getbytes(datetime.now.ticks.tostring())); string oauth_signature_method = "hmac-sha1"; string oauth_token = "xxx"; string oauth_version = "1.0"; timespan ts = datetime.utcnow - new datetime(1970, 1, 1, 0, 0, 0, 0); string oauth_timestamp = convert.toint64(ts.totalseconds).tostring(); //string oauth_version = "1.0"; //gs - when building signature string params //must in alphabetical order. can't bothered //with that, sorteddictionary it's thing sorteddictionary<string, string> sd = new sorteddictionary<string, string>(); sd.add("status", 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); //gs - build 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 + "&"); } //gs - remove trailing ambersand char, remember //it's been urlencoded have remove //last 3 chars - %26 basestring = basestring.substring(0, basestring.length - 3); //gs - build signing key string consumersecret = "xxx"; string oauth_token_secret = "xxx"; string signingkey = uri.escapedatastring(consumersecret) + "&" + uri.escapedatastring(oauth_token_secret); //gs - sign request hmacsha1 hasher = new hmacsha1( new asciiencoding().getbytes(signingkey)); //string signaturestring = convert.tobase64string( //hasher.computehash( // new asciiencoding().getbytes(basestring))); string signaturestring = convert.tobase64string( hasher.computehash( system.text.encoding.utf8.getbytes(basestring))); //gs - tell twitter don't 100 continue thing servicepointmanager.expect100continue = false; //gs - instantiate web request , populate //authorization header httpwebrequest hwr = (httpwebrequest)webrequest.create( @"https://api.twitter.com/1.1/statuses/update.json"); string authorizationheaderparams = string.empty; authorizationheaderparams += "oauth "; authorizationheaderparams += "oauth_nonce=" + "\"" + uri.escapedatastring(oauth_nonce) + "\","; authorizationheaderparams += "oauth_signature_method=" + "\"" + uri.escapedatastring(oauth_signature_method) + "\","; authorizationheaderparams += "oauth_timestamp=" + "\"" + uri.escapedatastring(oauth_timestamp) + "\","; authorizationheaderparams += "oauth_consumer_key=" + "\"" + uri.escapedatastring( oauth_consumer_key) + "\","; authorizationheaderparams += "oauth_token=" + "\"" + uri.escapedatastring(oauth_token) + "\","; authorizationheaderparams += "oauth_signature=" + "\"" + uri.escapedatastring(signaturestring) + "\","; //authorizationheaderparams += "oauth_version=" + "\"" + //uri.escapedatastring(oauth_version) + "\""; hwr.headers.add( "authorization", authorizationheaderparams); //gs - post off request hwr.method = "post"; hwr.contenttype = "application/x-www-form-urlencoded"; stream stream = hwr.getrequeststream(); //byte[] bodybytes = //new asciiencoding().getbytes(postbody); byte[] bodybytes = system.text.encoding.utf8.getbytes(postbody); stream.write(bodybytes, 0, bodybytes.length); stream.flush(); stream.close(); //gs - allow reasonable timeout in case //twitter's busy hwr.timeout = 3 * 60 * 1000; try { httpwebresponse rsp = hwr.getresponse() httpwebresponse; //gs - return here... } catch (webexception e) { messagebox.show(e.message); } }
if want post account, can create token on dev.twitter.com. can use credentials of new application directly within code.
like previous comment recommend either use oauth or twitter library.
Comments
Post a Comment