2
votes

I have been having a problem trying to get a successful response from twitter when trying the following url request: https://api.twitter.com/1.1/statuses/user_timeline.json?screen_name=username&count=1

I have found a brilliant example on codeproject.com that enables me to get a users tweets but I wish to be able to append some properties to the url string to filter the amount of tweets I'm receiving. I use to be able to do this in version 1.0 of the twitter API without OAuth. When I try the following code below I get a unauthorized exception thrown by the WebRequest object.

var oauth_token = "twitterAppData";
var oauth_token_secret = "twitterAppData";
var oauth_consumer_key = "twitterAppData";
var oauth_consumer_secret = "twitterAppData";

// oauth implementation details
var oauth_version = "1.0";
var oauth_signature_method = "HMAC-SHA1";

// unique request details
var oauth_nonce = Convert.ToBase64String(
    new ASCIIEncoding().GetBytes(DateTime.Now.Ticks.ToString()));
var timeSpan = DateTime.UtcNow
    - new DateTime(1970, 1, 1, 0, 0, 0, 0, DateTimeKind.Utc);
var oauth_timestamp = Convert.ToInt64(timeSpan.TotalSeconds).ToString();

// message api details
var status = "Updating status via REST API if this works";
var resource_url = "https://api.twitter.com/1.1/statuses/user_timeline.json";
var screen_name = "user1234";
var count = "1";
// create oauth signature
var baseFormat = "oauth_consumer_key={0}&oauth_nonce={1}&oauth_signature_method={2}" +
                "&oauth_timestamp={3}&oauth_token={4}&oauth_version={5}&screen_name={6}&count={7}";

var baseString = string.Format(baseFormat,
                            oauth_consumer_key,
                            oauth_nonce,
                            oauth_signature_method,
                            oauth_timestamp,
                            oauth_token,
                            oauth_version,
                             Uri.EscapeDataString(screen_name),
                             Uri.EscapeDataString(count)
                            );

baseString = string.Concat("GET&", Uri.EscapeDataString(resource_url), "&", Uri.EscapeDataString(baseString));

var compositeKey = string.Concat(Uri.EscapeDataString(oauth_consumer_secret),
                        "&", Uri.EscapeDataString(oauth_token_secret));

string oauth_signature;
using (HMACSHA1 hasher = new HMACSHA1(ASCIIEncoding.ASCII.GetBytes(compositeKey)))
{
    oauth_signature = Convert.ToBase64String(
        hasher.ComputeHash(ASCIIEncoding.ASCII.GetBytes(baseString)));
}

// create the request header
var headerFormat = "OAuth oauth_nonce=\"{0}\", oauth_signature_method=\"{1}\", " +
                   "oauth_timestamp=\"{2}\", oauth_consumer_key=\"{3}\", " +
                   "oauth_token=\"{4}\", oauth_signature=\"{5}\", " +
                   "oauth_version=\"{6}\"";

var authHeader = string.Format(headerFormat,
                        Uri.EscapeDataString(oauth_nonce),
                        Uri.EscapeDataString(oauth_signature_method),
                        Uri.EscapeDataString(oauth_timestamp),
                        Uri.EscapeDataString(oauth_consumer_key),
                        Uri.EscapeDataString(oauth_token),
                        Uri.EscapeDataString(oauth_signature),
                        Uri.EscapeDataString(oauth_version)
                );


// make the request

ServicePointManager.Expect100Continue = false;

var postBody = "screen_name=" + Uri.EscapeDataString(screen_name);//
var postbody2 = "count=" + Uri.EscapeDataString(count);
resource_url += "?" + postBody + "&" + postbody2;
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(resource_url);
request.Headers.Add("Authorization", authHeader);
request.Method = "GET";
request.ContentType = "application/x-www-form-urlencoded";


WebResponse response = request.GetResponse();
string responseData = new StreamReader(response.GetResponseStream()).ReadToEnd();

The codeblock above works if I take out all parts that referees to &count=1

Thank you in advance for any help or light you can shed on this for me.

Stuart Turbefield

1

1 Answers

4
votes

Looks like the parameters needs to be in alphabetical order in the OAUTH signature string. I tried switching the COUNT to the begining and it works :

var baseFormat = "count={7}&oauth_consumer_key={0}&oauth_nonce={1}&oauth_signature_method={2}" + "&oauth_timestamp={3}&oauth_token={4}&oauth_version={5}&screen_name={6}";

var baseString = string.Format(baseFormat, oauth_consumer_key, oauth_nonce, oauth_signature_method, oauth_timestamp, oauth_token, oauth_version, Uri.EscapeDataString(screen_name), Uri.EscapeDataString(count) );

Good luck, and thanks to you, I was looking for example with the 1.1 api.

K.