Using C# to create the Twitter authorization header for search -
i'm writing write c# method generate authentication header twitter. i'm trying search twitter through api: https://api.twitter.com/1.1/search/tweets.json.
here's url call:
https://api.twitter.com/1.1/search/tweets.json?q=%23countryman+or+%23johncooperworks+or+%40mini%26since_id%3d24012619984051000%26max_id%3d250126199840518145%26result_type%3dmixed%26count%3d4
here's method:
private string gettwitterauthheader() { const string oauthconsumerkey = ""; const string oauthconsumersecret = ""; const string oauthtoken = ""; const string oauthtokensecret = ""; const string oauthversion = "1.0"; const string oauthsignaturemethod = "hmac-sha1"; var oauthnonce = convert.tobase64string(new asciiencoding().getbytes(datetime.now.ticks.tostring(cultureinfo.invariantculture))); var timespan = datetime.utcnow - new datetime(1970, 1, 1, 0, 0, 0, 0, datetimekind.utc); var oauthtimestamp = convert.toint64(timespan.totalseconds).tostring(cultureinfo.invariantculture); const string resourceurl = "https://api.twitter.com/1.1/search/tweets.json"; const string baseformat = "oauth_consumer_key={0}&oauth_nonce={1}&oauth_signature_method={2}" + "&oauth_timestamp={3}&oauth_token={4}&oauth_version={5}"; var basestring = string.format(baseformat, oauthconsumerkey, oauthnonce, oauthsignaturemethod, oauthtimestamp, oauthtoken, oauthversion ); basestring = string.concat("get&", uri.escapedatastring(resourceurl), "&", uri.escapedatastring(basestring)); var compositekey = string.concat(uri.escapedatastring(oauthconsumersecret), "&", uri.escapedatastring(oauthtokensecret)); string oauthsignature; using (var hasher = new hmacsha1(encoding.ascii.getbytes(compositekey))) { oauthsignature = convert.tobase64string( hasher.computehash(encoding.ascii.getbytes(basestring))); } const string headerformat = "oauth oauth_consumer_key=\"{0}\", " + "oauth_nonce=\"{1}\", " + "oauth_signature=\"{2}\", " + "oauth_signature_method=\"{3}\", " + "oauth_timestamp=\"{4}\", " + "oauth_token=\"{5}\", " + "oauth_version=\"{6}\""; var authheader = string.format(headerformat, uri.escapedatastring(oauthconsumerkey), uri.escapedatastring(oauthnonce), uri.escapedatastring(oauthsignature), uri.escapedatastring(oauthsignaturemethod), uri.escapedatastring(oauthtimestamp), uri.escapedatastring(oauthtoken), uri.escapedatastring(oauthversion) ); return authheader; }
the error is:
{ "errors": [ { "message": "bad authentication data", "code": 215 } ] }
any pointers?
do need account actual search query in generating auth header? (e.g. values append search api)?
i'm finding hard debug.
thanks!
edit:
based on feedback, here's update:
var resourceurl = "https://api.twitter.com/1.1/search/tweets.json"; const string baseformat = "oauth_consumer_key={0}&oauth_nonce={1}&oauth_signature_method={2}" + "&oauth_timestamp={3}&oauth_token={4}&oauth_version={5}&q={6}"; var basestring = string.format(baseformat, oauthconsumerkey, oauthnonce, oauthsignaturemethod, oauthtimestamp, oauthtoken, oauthversion, query ); basestring = string.concat("get&", uri.escapedatastring(resourceurl), "&", uri.escapedatastring(basestring));
having read https://dev.twitter.com/oauth/overview/creating-signatures closer, seems right. still, same error.
wrap "query" in uri.escapedatastring()
.
your exact code above works me if that. said, should escaping all of parameter keys , values, , i'm bit stumped why nonce working without escaping, since it's base64 encoded. perhaps lucky in testing , never hit nonce slash in it.
Comments
Post a Comment