I am using c# code to use api, it works fine when I don't add comma(,) but with comma it return unauthorized, Is there any issue with encoding of comma?
url
header parameter
basestringParameters.Add("track", "twitter"); basestringParameters.Add("locations", "-122.75,36.8,-121.75,37.8");
my base String
GET&https%3A%2F%2Fstream.twitter.com%2F1.1%2Fstatuses%2Ffilter.json&locations%3D-122.75%2C36.8%2C-121.75%2C37.8%26oauth_consumer_key%3DxtOm0eNd8pm7hv643YqjZKhjH%26oauth_nonce%3DNjM1NjgxMzQ2Nzk5MjAwOTc0%26oauth_signature_method%3DHMAC-SHA1%26oauth_timestamp%3D1432519880%26oauth_token%3D603356335-N3LRAKyYobdbNB4PeMaQkjNsiHvEEcX9DKHiC93Q%26oauth_version%3D1.0%26track%3Dtwitter
authorization header
OAuthoauth_nonce="NjM1NjgxMzQ2Nzk5MjAwOTc0",oauth_signature_method="HMAC-SHA1",oauth_timestamp="1432519880",oauth_consumer_key="xtOm0eNd8pm7hv643YqjZKhjH",oauth_token="603356335-N3LRAKyYobdbNB4PeMaQkjNsiHvEEcX9DKHiC93Q",oauth_signature="vYeOXGxuFRMiP%2BThEtFwHVG8Jvo%3D",oauth_version="1.0"
Complete code
string oauthconsumerkey = "xxxxxx";
string oauthconsumersecret = "xxxxx";
string oauthtoken = "xxxxx";
string oauthtokensecret = "xxxxx";
public void Search()
{
string url = "https://stream.twitter.com/1.1/statuses/filter.json?track=twitter";
string oauthsignaturemethod = "HMAC-SHA1";
string oauthversion = "1.0";
string oauthnonce = Convert.ToBase64String(
new ASCIIEncoding().GetBytes(DateTime.Now.Ticks.ToString()));
TimeSpan timeSpan = DateTime.UtcNow - new DateTime(1970, 1, 1, 0, 0, 0, 0, DateTimeKind.Utc);
string oauthtimestamp = Convert.ToInt64(timeSpan.TotalSeconds).ToString();
SortedDictionary<string, string> basestringParameters = new SortedDictionary<string, string>();
basestringParameters.Add("track", "twitter");
//basestringParameters.Add("locations", "-122.75,36.8,-121.75,37.8");
basestringParameters.Add("oauth_version", oauthversion);
basestringParameters.Add("oauth_consumer_key", oauthconsumerkey);
basestringParameters.Add("oauth_nonce", oauthnonce);
basestringParameters.Add("oauth_signature_method", oauthsignaturemethod);
basestringParameters.Add("oauth_timestamp", oauthtimestamp);
basestringParameters.Add("oauth_token", oauthtoken);
//Build the signature string
StringBuilder baseString = new StringBuilder();
baseString.Append("GET" + "&");
baseString.Append(EncodeCharacters(Uri.EscapeDataString(url.Split('?')[0]) + "&"));
foreach (KeyValuePair<string, string> entry in basestringParameters)
{
baseString.Append(EncodeCharacters(Uri.EscapeDataString(entry.Key + "=" + entry.Value + "&")));
}
//Remove the trailing ambersand char last 3 chars - %26
string finalBaseString = baseString.ToString().Substring(0, baseString.Length - 3);
//Build the signing key
string signingKey = EncodeCharacters(Uri.EscapeDataString(oauthconsumersecret)) + "&" +
EncodeCharacters(Uri.EscapeDataString(oauthtokensecret));
//Sign the request
HMACSHA1 hasher = new HMACSHA1(new ASCIIEncoding().GetBytes(signingKey));
string oauthsignature = Convert.ToBase64String(
hasher.ComputeHash(new ASCIIEncoding().GetBytes(finalBaseString)));
//Tell Twitter we don't do the 100 continue thing
ServicePointManager.Expect100Continue = false;
//authorization header
HttpWebRequest webRequest = (HttpWebRequest)WebRequest.Create(EncodeCharacters(url));
StringBuilder authorizationHeaderParams = new StringBuilder();
authorizationHeaderParams.Append("OAuth ");
authorizationHeaderParams.Append("oauth_nonce=" + "\"" + Uri.EscapeDataString(oauthnonce) + "\",");
authorizationHeaderParams.Append("oauth_signature_method=" + "\"" + Uri.EscapeDataString(oauthsignaturemethod) + "\",");
authorizationHeaderParams.Append("oauth_timestamp=" + "\"" + Uri.EscapeDataString(oauthtimestamp) + "\",");
authorizationHeaderParams.Append("oauth_consumer_key=" + "\"" + Uri.EscapeDataString(oauthconsumerkey) + "\",");
if (!string.IsNullOrEmpty(oauthtoken))
authorizationHeaderParams.Append("oauth_token=" + "\"" + Uri.EscapeDataString(oauthtoken) + "\",");
authorizationHeaderParams.Append("oauth_signature=" + "\"" + Uri.EscapeDataString(oauthsignature) + "\",");
authorizationHeaderParams.Append("oauth_version=" + "\"" + Uri.EscapeDataString(oauthversion) + "\"");
webRequest.Headers.Add("Authorization", authorizationHeaderParams.ToString());
webRequest.Method = "GET";
webRequest.ContentType = "application/x-www-form-urlencoded";
//Allow us a reasonable timeout in case Twitter's busy
webRequest.Timeout = 3 * 60 * 1000;
try
{
//Proxy settings
//webRequest.Proxy = new WebProxy("enter proxy details/address");
HttpWebResponse webResponse = webRequest.GetResponse() as HttpWebResponse;
Stream dataStream = webResponse.GetResponseStream();
// Open the stream using a StreamReader for easy access.
StreamReader reader = new StreamReader(dataStream);
// Read the content.
string responseFromServer = "";
String s;
Console.WriteLine("output start");
while ((s = reader.ReadLine()) != null)
{
Debug.Write(s);
responseFromServer += s;
}
Console.WriteLine("output over");
ViewBag.Result += responseFromServer + "<br/>";
}
catch (Exception ex)
{
ViewBag.Result = ex.ToString();
}
}
private string EncodeCharacters(string data)
{
//as per OAuth Core 1.0 Characters in the unreserved character set MUST NOT be encoded
//unreserved = ALPHA, DIGIT, '-', '.', '_', '~'
if (data.Contains("!"))
data = data.Replace("!", "%21");
if (data.Contains("'"))
data = data.Replace("'", "%27");
if (data.Contains("("))
data = data.Replace("(", "%28");
if (data.Contains(")"))
data = data.Replace(")", "%29");
if (data.Contains("*"))
data = data.Replace("*", "%2A");
if (data.Contains(","))
data = data.Replace(",", "%2C");
return data;
}
Thanks!
twython
and more simply with the test feature that twitter provides at their webpage. You must have something wrong in the way you build your authorization. Please share some code. (I know it looks weird that your auth works when you don't include locations but rest assured this is not the problem. Just post code so that somebody can help) – lrnzcig