Hi Joshua,

code works perfect. But, how can I add geocode and unti parameters to your MakeTwitterCall function. These parameters are listed in this url: https://dev.twitter.com/docs/api/1.1/get/search/tweets

Many of these are working but when I add these two parameters in my IEnumerable<KeyValuePair<string, string>> RequestParameters then twitter send empty results.

Can you help me?
Thanks.

Thanks for posting this valuable code Britney.

Any one who can answer this ? cause I’m having the same problem
thanks in advance

Guys plz I need ur help in this :

I am getting the same unauthorized like most people. After reading this, it is probably with my signature. I am trying to do a query using the geocode. here is my code:

var geocode = "42.791306,-86.033448,1mi";
        var baseFormat = "geocode={7}&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,
                                    oauth_consumer_key,
                                    oauth_nonce,
                                    oauth_signature_method,
                                    oauth_timestamp,
                                    oauth_token,
                                    oauth_version,
                                    Uri.EscapeDataString(q),
                                    Uri.EscapeDataString(geocode)
                                    );

var postBody = “q=” + Uri.EscapeDataString(q) + “&geocode=” + Uri.EscapeDataString(geocode);
resource_url += “?” + postBody;

That all looks fine to me I haven’t tried using Geocode at all I can try and get code working for you tomorrow. I can’t promise anything though.

public string searchTwitter(string searchTerm) { try { string URL = "https://api.twitter.com/1.1/search/tweets.json"; // oauth application keys var oauth_token = //"insert personal token "; var oauth_token_secret = //"insert personal secret "; var oauth_consumer_key = // = "insert company key"; var oauth_consumer_secret = // = "insert company secret";
        // 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();
        var geocode = "42.791306,-86.033448,1mi";
        // create oauth signature
        var baseFormat = "geocode={7}&oauth_consumer_key={0}&oauth_nonce={1}&oauth_signature_method={2}" +
                        "&oauth_timestamp={3}&oauth_token={4}&oauth_version={5}&q={6}&result_type=mixed";//&rpp=" + tweetCount + "&include_entities=true" + "&page=" + page +"&until=

        var baseString = string.Format(baseFormat,
                                    oauth_consumer_key,
                                    oauth_nonce,
                                    oauth_signature_method,
                                    oauth_timestamp,
                                    oauth_token,
                                    oauth_version,
            //Uri.EscapeDataString(tweetCount),
                                    Uri.EscapeDataString(searchTerm),
                                    Uri.EscapeDataString(geocode)
                                    );

        baseString = string.Concat("GET&", Uri.EscapeDataString(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)
                        );


        //myDiv.InnerHtml = baseString + "<br/><br/><br/>" + authHeader;
        //return;
        ServicePointManager.Expect100Continue = false;

        // make the request'
        URL = URL + "?geocode=" + geocode + "&q=" + Uri.EscapeDataString(searchTerm) + "&result_type=mixed";//
        //URL = Uri.EscapeDataString(URL);
        HttpWebRequest request = (HttpWebRequest)WebRequest.Create(URL);
        request.Headers.Add("Authorization", authHeader);
        request.Method = "GET";
        request.ContentType = "application/x-www-form-urlencoded";
        /*using (Stream stream = request.GetRequestStream())
        {
            byte[] content = ASCIIEncoding.ASCII.GetBytes(query);
            stream.Write(content, 0, content.Length);
            //myDiv.InnerHtml = content;
        }*/
        var response = (HttpWebResponse)request.GetResponse();
        //WebResponse response = request.GetResponse();
        var reader = new StreamReader(response.GetResponseStream());
        var objText = reader.ReadToEnd();
        //myDiv.InnerHtml = objText;
        return objText;
    }
    catch (Exception err)
    {
        return "err" + err;
    }
}
This worked for me you may have your tokens wrong.

hi Britney,
thanks for your code example, It has helped me a lot with my application but i still have a problem.
Im trying to use streaming API and send a POST in order to connect and read tweets to the public stream
https://stream.twitter.com/1.1/statuses/filter.json
I followed your example but i still receive Error 401 Unauthorized.

here my code:

        string URL = "https://stream.twitter.com/1.1/statuses/filter.json";
        string query = "track=twitter";

        // oauth application keys
        string oauth_token = "xxx";
        string oauth_token_secret = "xxx";
        string oauth_consumer_key = "xxx";
        string oauth_consumer_secret = "xxx";

        // oauth implementation details
        string oauth_version = "1.0";
        string 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();


        // create oauth signature
        string baseFormat = "oauth_consumer_key={0}&oauth_nonce={1}&oauth_signature_method={2}" +
                            "&oauth_timestamp={3}&oauth_token={4}&oauth_version={5}&q={6}";


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


        baseString = string.Concat("POST&", Uri.EscapeDataString(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
        HttpWebRequest request = (HttpWebRequest)WebRequest.Create(URL);
        request.Headers.Add("Authorization", authHeader);
        request.Method = "POST";
        request.ContentType = "application/x-www-form-urlencoded";

        //string postData = "track=twitter";
        ASCIIEncoding encoding = new ASCIIEncoding();
        byte[] byte1 = encoding.GetBytes(query);
        //Set the content length of the string being posted.
        request.ContentLength = byte1.Length;
        
        Stream newStream = request.GetRequestStream();
        newStream.Write(byte1, 0, byte1.Length);


        var response = (HttpWebResponse)request.GetResponse();
        var reader = new StreamReader(response.GetResponseStream());
        var objText = reader.ReadToEnd();

can you help me to find out what am I doing wrong?

thanks in advance
Stefano

Hi Britney,

I want to get the replies for this post using twitter api. how can i do it.

i tried many ways but not getting the replies in web response.

Help me out in this.

thanks,
Mujeeb.

I’m using this example to get the replies on a tweet but return json doesn’t include the replies at all.
https://dev.twitter.com/docs/api/1.1/get/statuses/home_timeline

Please help me on this. I’m using this url:
https://api.twitter.com/1.1/statuses/user_timeline.json?screen_name=mujeebfarooqi&count=1&exclude_replies=false

Also, if I want to get the replies of a particular tweet then how i’ll do it?
like sample url: https://twitter.com/MujeebFarooqi/status/459340704356261888

Thanks,
Mujeeb.

I’m using this example to get the replies on a tweet but return json doesn’t include the replies at all.
https://dev.twitter.com/docs/api/1.1/get/statuses/home_timeline

Please help me on this. I’m using this url:
https://api.twitter.com/1.1/statuses/user_timeline.json?screen_name=mujeebfarooqi&count=1&exclude_replies=false

Also, if I want to get the replies of a particular tweet then how i’ll do it?
like sample url: https://twitter.com/MujeebFarooqi/status/459340704356261888

Thanks,
Mujeeb.

thanks Britney