c# - Twitter OAuth request_token stopped working -
out of no where, twitter api calls, specificly first step in 3-legged auth, stopped working. i've compared timestamps, keys , oauth signature generator tool, , match (execpt oauth_nonce
thats point guess). here code. suggestions or small observations appreciated.
protected void requesttoken() { string oauthcallback = request.url.host + "/twittercallback.aspx"; string oauthconsumerkey = "xxx-consumerkey"; string oauthconsumersecret = "xxx-consumersecret"; string oauthtokensecret = string.empty; string oauthtoken = string.empty; string oauthsignaturemethod = "hmac-sha1"; string oauthversion = "1.0"; string oauthnonce = convert.tobase64string(new asciiencoding().getbytes(datetime.now.ticks.tostring())); timespan timespan = datetime.utcnow - new datetime(1970, 1, 1, 0, 0, 0, 0, datetimekind.utc); string oauthtimestamp = convert.toint64(timespan.totalseconds).tostring(); string url = "https://api.twitter.com/oauth/request_token?oauth_callback=" + oauthcallback; sorteddictionary<string, string> basestringparameters = new sorteddictionary<string, string>(); basestringparameters.add("oauth_version", oauthversion); basestringparameters.add("oauth_consumer_key", oauthconsumerkey); basestringparameters.add("oauth_nonce", oauthnonce); basestringparameters.add("oauth_signature_method", oauthsignaturemethod); basestringparameters.add("oauth_timestamp", oauthtimestamp); basestringparameters.add("oauth_callback", uri.escapedatastring(oauthcallback)); //build signature string string basestring = string.empty; basestring += "post" + "&"; basestring += uri.escapedatastring(url.split('?')[0]) + "&"; foreach (keyvaluepair<string, string> entry in basestringparameters) { basestring += uri.escapedatastring(entry.key + "=" + entry.value + "&"); } //remove trailing ambersand char last 3 chars - %26 //basestring = basestring.substring(0, basestring.length - 3); //build signing key string signingkey = uri.escapedatastring(oauthconsumersecret) + "&" + uri.escapedatastring(oauthtokensecret); //sign request hmacsha1 hasher = new hmacsha1(new asciiencoding().getbytes(signingkey)); string oauthsignature = convert.tobase64string( hasher.computehash(new asciiencoding().getbytes(basestring))); //tell twitter don't 100 continue thing servicepointmanager.expect100continue = false; httpwebrequest webrequest = (httpwebrequest)webrequest.create(@url); string authorizationheaderparams = string.empty; authorizationheaderparams += "oauth "; authorizationheaderparams += "oauth_nonce=" + "\"" + uri.escapedatastring(oauthnonce) + "\","; authorizationheaderparams += "oauth_signature_method=" + "\"" + uri.escapedatastring(oauthsignaturemethod) + "\","; authorizationheaderparams += "oauth_timestamp=" + "\"" + uri.escapedatastring(oauthtimestamp) + "\","; authorizationheaderparams += "oauth_consumer_key=" + "\"" + uri.escapedatastring(oauthconsumerkey) + "\","; authorizationheaderparams += "oauth_signature=" + "\"" + uri.escapedatastring(oauthsignature) + "\","; authorizationheaderparams += "oauth_version=" + "\"" + uri.escapedatastring(oauthversion) + "\""; webrequest.headers.add("authorization", authorizationheaderparams); webrequest.method = "post"; webrequest.contenttype = "application/x-www-form-urlencoded"; //allow reasonable timeout in case twitter's busy webrequest.timeout = 3 * 60 * 1000; try { httpwebresponse webresponse = webrequest.getresponse() httpwebresponse; stream datastream = webresponse.getresponsestream(); // open stream using streamreader easy access. streamreader reader = new streamreader(datastream); // read content. string responsefromserver = reader.readtoend(); var uri = new uri("https://test.dk?" + responsefromserver); var token = httputility.parsequerystring(uri.query).get("oauth_token"); ; var tokensecret = httputility.parsequerystring(uri.query).get("oauth_token_secret"); response.write(responsefromserver); response.redirect("https://api.twitter.com/oauth/authorize?force_login=true&oauth_token=" + token); } catch (exception ex) { response.write(ex.getbaseexception()); } }
the error happens when http request webrequest.getresponse()
it returns 401 unauthorized
apperently have include oauth version number in url now, or else fall oldest version (or maybe newest, can't remember).
providing /oath/1.0/
or /1.0/oauth/
or ever solved issue recall (it's been while).
Comments
Post a Comment