Twitter user search by name and location


#1

I am trying twitter search api using name and location. However, adding the location parameter to the Http web request url is giving me a 401 unauthorized exception.

My web request Url that works: “https://api.twitter.com/1.1/users/search.json?q=John%20Doe

The one that doesn’t: "https://api.twitter.com/1.1/users/search.json?q=John%20Doe&near=seattle&within=20&unit=mi"
OR this one doesn’t work either: “https://api.twitter.com/1.1/users/search.json?q=John%20Doe&geocode=-18.9750,-72.8258,1000mi

Any help will be appreciated.

Thanks,
Kushal.


#2

Any help here will be much appreciated.


#3

Hi Kushal,

Thanks for reaching out, it would appear that you have an oAuth issue. In order for us to best help with this request would you mind using twurl?

Here is an example:
twurl -H api.twitter.com “/1.1/users/search.json?q=John%20Doe&geocode=-18.9750,-72.8258,1000mi”

Alternatively if you can provide more details on the oAuth implementation that would be great.

Thanks,
Gareth


#4

Yes, twurl command works great. Here is our code, any pointers on what we are missing on? This is in C#.

            string resource_url = "https://api.twitter.com/1.1/users/search.json";
            var oauth_token = "1387779014-YOOSpe8F65JU9ptRGFfQDzBF3g5HjgkeoajkmyL";
            var oauth_token_secret = "xBWvFzzLSbmgpjyaReMWQU5KNXBup3s2g58tN9X3K0GYX";
            var oauth_consumer_key = "8aiV1jhmXU2IvdRGzKmsQ";
            var oauth_consumer_secret = "removing that from here";

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

            // 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}&q={6}";

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

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

            ServicePointManager.Expect100Continue = false;

            // make the request
            var postBody = "q=" + Uri.EscapeDataString(name);
            resource_url += "?" + postBody;

            HttpWebRequest request = (HttpWebRequest)WebRequest.Create(resource_url);
            request.Headers.Add("Authorization", authHeader);
            request.Method = "GET";
            request.ContentType = "application/x-www-form-urlencoded";
            var response = (HttpWebResponse)request.GetResponse();
            var reader = new StreamReader(response.GetResponseStream());
            var objText = reader.ReadToEnd();

#5

Gareth - Any help here would be greatly appreciated. Here is the zip file with the C# project.

Thanks,
Kushal.


#6

Any update on this? What are we missing? Few places it talks about url encoding, but that didn’t help either.
https://dev.twitter.com/discussions/11720


#7

Try commenting out this line:

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

#8

This hasn’t helped. We have now tried all these Urls:

https://api.twitter.com/1.1/users/search.json?q=kushal%20shah&geocode=-18.9750,-72.8258,1000mi
https://api.twitter.com/1.1/users/search.json?q=kushal shah&geocode=-18.9750,-72.8258,1000mi

https://api.twitter.com/1.1/users/search.json?q=kushal%20shah&location=seattle
https://api.twitter.com/1.1/users/search.json?q=kushal shah&location=seattle

https://api.twitter.com/1.1/users/search.json?q=kushal%20shah&near=seattle
https://api.twitter.com/1.1/users/search.json?q=kushal shah&near=seattle

Any help will be much appreciated.

Thanks,
Kushal.