我使用c#代码来使用api,当我不添加逗号(,)但使用逗号返回未经授权时,它可以正常工作,逗号编码是否有任何问题?
urlhttps://stream.twitter.com/1.1/statuses/filter.json?track=twitter&位置= -122.75,36.8,-121.75,37.8
头参数
basestringParameters。添加("跟踪"、"推特");basestringParameters。Add("位置",-122.75,36.8,-121.75,37.8);
my base String
GET& https % 3 % 2 f % 2 fstream.twitter.com % 2 f1.1 % 2 fstatuses % 2 ffilter.json&位置% 3 d - 122.75 % 2 c36.8 % 2 c - 121.75 % 2 c37.8 % 26 oauth_consumer_key % 3 dxtom0end8pm7hv643yqjzkhjh % 26 oauth_nonce % 3 dnjm1njgxmzq2nzk5mjawotc0 % 26 oauth_signature_method % 3 dhmac-sha1 % 26 oauth_timestamp % 3 d1432519880 % 26 oauth_token % 3 d603356335-n3lrakyyobdbnb4pemaqkjnsihveecx9dkhic93q % 26 oauth_version % 3 d1.0 % 26跟踪% 3 dtwitter
授权头
OAuthoauth_nonce ="NjM1NjgxMzQ2Nzk5MjAwOTc0 oauth_signature_method ="HMAC-SHA1 oauth_timestamp ="1432519880",oauth_consumer_key ="xtOm0eNd8pm7hv643YqjZKhjH oauth_token ="603356335 - n3lrakyyobdbnb4pemaqkjnsihveecx9dkhic93q",oauth_signature ="vYeOXGxuFRMiP % 2 bthetfwhvg8jvo % 3 d",oauth_version = " 1.0 ">
/p>完整代码
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; }
谢谢!
尝试将逗号","替换为"%2C"