public TwitterTrending fetchTrending(string city_name) { // Get WOEID string woeid = getWoeid(city_name); string url = "https://api.twitter.com/1.1/trends/place.json?id=" + woeid; // Create OAuth parameters for twitter API string oauth_nonce = Convert.ToBase64String( new ASCIIEncoding().GetBytes(DateTime.Now.Ticks.ToString())); TimeSpan time_span = DateTime.UtcNow - new DateTime(1970, 1, 1, 0, 0, 0, 0, DateTimeKind.Utc); string oauthtimestamp = Convert.ToInt64(time_span.TotalSeconds).ToString(); SortedDictionary<string, string> basestringParameters = new SortedDictionary<string, string>(); basestringParameters.Add("id", woeid); basestringParameters.Add("oauth_version", oauth_version); basestringParameters.Add("oauth_consumer_key", oauth_consumer_key); basestringParameters.Add("oauth_nonce", oauth_nonce); basestringParameters.Add("oauth_signature_method", oauth_signature_method); basestringParameters.Add("oauth_timestamp", oauthtimestamp); basestringParameters.Add("oauth_token", oauth_token); //Build the signature string string baseString = String.Empty; baseString += "GET" + "&"; baseString += Uri.EscapeDataString(url.Split('?')[0]) + "&"; foreach (KeyValuePair<string, string> entry in basestringParameters) { baseString += Uri.EscapeDataString(entry.Key + "=" + entry.Value + "&"); } //Remove the trailing ambersand char last 3 chars - %26 baseString = baseString.Substring(0, baseString.Length - 3); //Build the signing key string signingKey = Uri.EscapeDataString(oauth_consumer_secret) + "&" + Uri.EscapeDataString(oauth_token_secret); //Sign the request HMACSHA1 hasher = new HMACSHA1(new ASCIIEncoding().GetBytes(signingKey)); string oauthsignature = Convert.ToBase64String(hasher.ComputeHash(new ASCIIEncoding().GetBytes(baseString))); //Tell Twitter we don't do the 100 continue thing ServicePointManager.Expect100Continue = false; //authorization header HttpWebRequest webRequest = (HttpWebRequest)WebRequest.Create(@url); string authorizationHeaderParams = String.Empty; authorizationHeaderParams += "OAuth "; authorizationHeaderParams += "oauth_nonce=\"" + Uri.EscapeDataString(oauth_nonce) + "\","; authorizationHeaderParams += "oauth_signature_method=\"" + Uri.EscapeDataString(oauth_signature_method) + "\","; authorizationHeaderParams += "oauth_timestamp=\"" + Uri.EscapeDataString(oauthtimestamp) + "\","; authorizationHeaderParams += "oauth_consumer_key=\"" + Uri.EscapeDataString(oauth_consumer_key) + "\","; authorizationHeaderParams += "oauth_token=\"" + Uri.EscapeDataString(oauth_token) + "\","; authorizationHeaderParams += "oauth_signature=\"" + Uri.EscapeDataString(oauthsignature) + "\","; authorizationHeaderParams += "oauth_version=\"" + Uri.EscapeDataString(oauth_version) + "\""; webRequest.Headers.Add("Authorization", authorizationHeaderParams); webRequest.Method = "GET"; webRequest.ContentType = "application/x-www-form-urlencoded"; TwitterTrending tw = new TwitterTrending(); //Allow us a reasonable timeout in case Twitter's busy webRequest.Timeout = 3 * 60 * 1000; try { //Proxy settings HttpWebResponse webResponse = webRequest.GetResponse() as HttpWebResponse; Stream dataStream = webResponse.GetResponseStream(); // Open the stream using a StreamReader for easy access. StreamReader reader = new StreamReader(dataStream); string responseFromServer = reader.ReadToEnd(); XmlDocument doc = ConvertJsonToXml(responseFromServer); XmlNodeList names = doc.GetElementsByTagName("name"); tw.TrendList = new List<Trends>(); foreach (XmlNode n in names) { Trends t = new Trends(); t.Name = n.InnerText; tw.TrendList.Add(t); } tw.Location = city_name; } catch (Exception ex) { tw.Error = ex.Message; } return tw; }
public TwitterTrending fetchTrending(string city_name) { // Get WOEID string woeid = getWoeid(city_name); string url = "https://api.twitter.com/1.1/trends/place.json?id=" + woeid; // Create OAuth parameters for twitter API string oauth_nonce = Convert.ToBase64String( new ASCIIEncoding().GetBytes(DateTime.Now.Ticks.ToString())); TimeSpan time_span = DateTime.UtcNow - new DateTime(1970, 1, 1, 0, 0, 0, 0, DateTimeKind.Utc); string oauthtimestamp = Convert.ToInt64(time_span.TotalSeconds).ToString(); SortedDictionary <string, string> basestringParameters = new SortedDictionary <string, string>(); basestringParameters.Add("id", woeid); basestringParameters.Add("oauth_version", oauth_version); basestringParameters.Add("oauth_consumer_key", oauth_consumer_key); basestringParameters.Add("oauth_nonce", oauth_nonce); basestringParameters.Add("oauth_signature_method", oauth_signature_method); basestringParameters.Add("oauth_timestamp", oauthtimestamp); basestringParameters.Add("oauth_token", oauth_token); //Build the signature string string baseString = String.Empty; baseString += "GET" + "&"; baseString += Uri.EscapeDataString(url.Split('?')[0]) + "&"; foreach (KeyValuePair <string, string> entry in basestringParameters) { baseString += Uri.EscapeDataString(entry.Key + "=" + entry.Value + "&"); } //Remove the trailing ambersand char last 3 chars - %26 baseString = baseString.Substring(0, baseString.Length - 3); //Build the signing key string signingKey = Uri.EscapeDataString(oauth_consumer_secret) + "&" + Uri.EscapeDataString(oauth_token_secret); //Sign the request HMACSHA1 hasher = new HMACSHA1(new ASCIIEncoding().GetBytes(signingKey)); string oauthsignature = Convert.ToBase64String(hasher.ComputeHash(new ASCIIEncoding().GetBytes(baseString))); //Tell Twitter we don't do the 100 continue thing ServicePointManager.Expect100Continue = false; //authorization header HttpWebRequest webRequest = (HttpWebRequest)WebRequest.Create(@url); string authorizationHeaderParams = String.Empty; authorizationHeaderParams += "OAuth "; authorizationHeaderParams += "oauth_nonce=\"" + Uri.EscapeDataString(oauth_nonce) + "\","; authorizationHeaderParams += "oauth_signature_method=\"" + Uri.EscapeDataString(oauth_signature_method) + "\","; authorizationHeaderParams += "oauth_timestamp=\"" + Uri.EscapeDataString(oauthtimestamp) + "\","; authorizationHeaderParams += "oauth_consumer_key=\"" + Uri.EscapeDataString(oauth_consumer_key) + "\","; authorizationHeaderParams += "oauth_token=\"" + Uri.EscapeDataString(oauth_token) + "\","; authorizationHeaderParams += "oauth_signature=\"" + Uri.EscapeDataString(oauthsignature) + "\","; authorizationHeaderParams += "oauth_version=\"" + Uri.EscapeDataString(oauth_version) + "\""; webRequest.Headers.Add("Authorization", authorizationHeaderParams); webRequest.Method = "GET"; webRequest.ContentType = "application/x-www-form-urlencoded"; TwitterTrending tw = new TwitterTrending(); //Allow us a reasonable timeout in case Twitter's busy webRequest.Timeout = 3 * 60 * 1000; try { //Proxy settings HttpWebResponse webResponse = webRequest.GetResponse() as HttpWebResponse; Stream dataStream = webResponse.GetResponseStream(); // Open the stream using a StreamReader for easy access. StreamReader reader = new StreamReader(dataStream); string responseFromServer = reader.ReadToEnd(); XmlDocument doc = ConvertJsonToXml(responseFromServer); XmlNodeList names = doc.GetElementsByTagName("name"); tw.TrendList = new List <Trends>(); foreach (XmlNode n in names) { Trends t = new Trends(); t.Name = n.InnerText; tw.TrendList.Add(t); } tw.Location = city_name; } catch (Exception ex) { tw.Error = ex.Message; } return(tw); }