0
votes

I am trying to execute a Search Query using the New Twitter REST API (Version 1.1), which uses the OAuth Authentication.

I have followed the Code found at the following link, but modified it to a GET Request instead of a POST. http://www.codeproject.com/Articles/247336/Twitter-OAuth-authentication-using-Net

The Code is here after reproduced.

Since I am developing locally, I have furthermore followed the steps found in the fisrt answer at this following link: Twitter oAuth callbackUrl - localhost development

Thus, I have created a Second App in Twitter, and used "127.0.0.1" as the Callback URL (I have meanwhile left the WebSite address setting to my Production one).

Nonetheless, I am still receiving a 401 Unauthorized Error.

Would anyone understand why I am not able to get authorized?

        var oauth_token = "MyAccessToken";
        var oauth_token_secret = "MyAccessTokenSecret";
        var oauth_consumer_key = "MyConsumerKey";
        var oauth_consumer_secret = "MyConsumerSecret";

        var oauth_version = "1.0";
        var oauth_signature_method = "HMAC-SHA1";
        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();
        var resource_url = "https://api.twitter.com/1.1/search/tweets.json?q=MyQuery";
        var status = "Updating status via REST API if this works";

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

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

        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)));
        }


        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)
                        );


        var postBody = "status=" + Uri.EscapeDataString(status);

        ServicePointManager.Expect100Continue = false;

        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();
1
The Query in the resource_url used is “Word1+Word2&rpp=100&geocode=LatCity,LngCity,50km&callback=?” Following the = sign. - JF0001
Update: I am receiving the same Error Message on the Production Server. Thus, there must be something wrong with my Code, or my Query. - JF0001

1 Answers

1
votes

EDIT

You need to split the querystring down into it's constituent parameters and include them in your basestring.

Note: the Twitter developer page does not list 'status' as a parameter of this call and the 'rpp' parameter has been replaced by 'count'. Try this:

var q = "Word1+Word2";
var count = "100";
var LatCity = "37.781157"; //example
var LngCity = "-122.398720"; //example
var geocode = string.Format("{0},{1},50km", LatCity, LngCity);
var callback = "?";

var baseFormat = "callback={0}&count={1}&geocode={2}&
    oauth_consumer_key={3}&oauth_nonce={4}&
    oauth_signature_method={5} &oauth_timestamp={6}&
    oauth_token={7}&oauth_version={8}&q={9}";

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

Also you only include the base url (without the querystring) in the basestring:

var base_url = "https://api.twitter.com/1.1/search/tweets.json";

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