gpt4 book ai didi

c# - 在 url 和参数中传递逗号 (,) 时,Twitter 流式传输 api 返回未授权

转载 作者:太空宇宙 更新时间:2023-11-03 15:40:33 24 4
gpt4 key购买 nike

我正在使用 c# 代码来使用 api,当我不添加逗号 (,) 时它工作正常但是使用逗号它返回未授权,逗号编码有任何问题吗?

网址

https://stream.twitter.com/1.1/statuses/filter.json?track=twitter&locations=-122.75,36.8,-121.75,37.8

标题参数

basestringParameters.Add("track", "twitter"); basestringParameters.Add("locations", "-122.75,36.8,-121.75,37.8");

我的基础字符串

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

授权 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"

完整代码

    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”替换逗号“,”

关于c# - 在 url 和参数中传递逗号 (,) 时,Twitter 流式传输 api 返回未授权,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30430659/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com