我正在尝试使用新的Twitter REST API(版本1.1)执行搜索查询,该API使用OAuth身份验证。
我已按照以下链接找到的代码,但将其修改为GET请求而不是POST。 http://www.codeproject.com/Articles/247336/Twitter-OAuth-authentication-using-Net
“守则”在此之后再现。
由于我在本地开发,我还在以下链接中遵循了fisrt答案中的步骤: Twitter oAuth callbackUrl - localhost development
因此,我在Twitter上创建了第二个应用程序,并使用“127.0.0.1”作为回调URL(我同时将WebSite地址设置保留到我的生产中)。
尽管如此,我仍然收到401 Unauthorized Error。
有人理解为什么我无法获得授权吗?
var oauth_token = "MyAccessToken";
var oauth_token_secret = "MyAccessTokenSecret";
var oauth_consumer_key = "MyConsumerKey";
var oauth_consumer_secret = "MyConsumerSecret";
var oauth_version = "1.0";
var oauth_signature_method = "HMAC-SHA1";
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();
var resource_url = "https://api.twitter.com/1.1/search/tweets.json?q=MyQuery";
var status = "Updating status via REST API if this works";
var baseFormat = "oauth_consumer_key={0}&oauth_nonce={1}&oauth_signature_method={2}" +
"&oauth_timestamp={3}&oauth_token={4}&oauth_version={5}&status={6}";
var baseString = string.Format(baseFormat,
oauth_consumer_key,
oauth_nonce,
oauth_signature_method,
oauth_timestamp,
oauth_token,
oauth_version,
Uri.EscapeDataString(status)
);
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)));
}
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)
);
var postBody = "status=" + Uri.EscapeDataString(status);
ServicePointManager.Expect100Continue = false;
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(resource_url);
request.Headers.Add("Authorization", authHeader);
request.Method = "GET";
request.ContentType = "application/x-www-form-urlencoded";
WebResponse response = request.GetResponse();
答案 0 :(得分:1)
修改
您需要将查询字符串拆分为其组成参数,并将它们包含在您的基本字符串中。
注意:Twitter developer page不会将'status'列为此调用的参数,并且'rpp'参数已被'count'替换。试试这个:
var q = "Word1+Word2";
var count = "100";
var LatCity = "37.781157"; //example
var LngCity = "-122.398720"; //example
var geocode = string.Format("{0},{1},50km", LatCity, LngCity);
var callback = "?";
var baseFormat = "callback={0}&count={1}&geocode={2}&
oauth_consumer_key={3}&oauth_nonce={4}&
oauth_signature_method={5} &oauth_timestamp={6}&
oauth_token={7}&oauth_version={8}&q={9}";
var baseString = string.Format(baseFormat,
Uri.EscapeDataString(callback),
Uri.EscapeDataString(count),
Uri.EscapeDataString(geocode)
oauth_consumer_key,
oauth_nonce,
oauth_signature_method,
oauth_timestamp,
oauth_token,
oauth_version,
Uri.EscapeDataString(q)
);
此外,您只在basetring中包含基本网址(不包含查询字符串):
var base_url = "https://api.twitter.com/1.1/search/tweets.json";
baseString = string.Concat("GET&", Uri.EscapeDataString(base_url),
"&", Uri.EscapeDataString(baseString));