0

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();
Community
  • 1
  • 1
JF0001
  • 819
  • 7
  • 30
  • The Query in the resource_url used is “Word1+Word2&rpp=100&geocode=LatCity,LngCity,50km&callback=?” Following the = sign. – JF0001 Sep 07 '13 at 16:21
  • 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 Sep 08 '13 at 15:02

1 Answers1

1

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));
Jon Susiak
  • 4,948
  • 1
  • 30
  • 38
  • Hi Jonny, Thank you very much for your answer. I have modified my code according to your suggestions. However, I am still receiving a 401 Unauthorized Error from the Twitter Server. Would you have any other idea of what could be creating this issue? Thank you again. Regards, Joseph – JF0001 Sep 10 '13 at 02:46
  • After checking the developer page it appears that you need to split your querystring into it's individual parts and include them in your basestring. I have edited my answer. – Jon Susiak Sep 10 '13 at 08:46
  • Hi Jonny, Thank you again for your time and continued help, but I am still receiving the same error message after having implemented the suggested modifications. Unfortunately, it appears that there is no working example for Twitter Searches using the New API documented on the Web... Joseph – JF0001 Sep 11 '13 at 01:29
  • Ok sorry it's got me stumped. Perhaps you could try and track down the problem by simplifying the query to q=word1 initially (alter your basestring accordingly). If that works add in further bits of the querystring to find where the problem lies. Good luck. – Jon Susiak Sep 11 '13 at 07:37
  • Hi Jonny, Thank you again for your time and help regarding this issue. I have solved the Unauthorized issue by simplifying the query, as you had suggested, and by slightly modifying my code, following the example found at the following link: https://dev.twitter.com/discussions/15206. Joseph – JF0001 Sep 14 '13 at 15:11