在尝试以下网址请求时尝试从Twitter获得成功回复时遇到问题:https://api.twitter.com/1.1/statuses/user_timeline.json?screen_name=username&count=1
我在codeproject.com上找到了一个很棒的例子,它让我可以获得用户的推文,但我希望能够在url字符串中附加一些属性来过滤我收到的推文数量。我使用在没有OAuth的twitter API的1.0版本中执行此操作。当我尝试下面的代码时,我得到WebRequest对象抛出的未经授权的异常。
var oauth_token = "twitterAppData";
var oauth_token_secret = "twitterAppData";
var oauth_consumer_key = "twitterAppData";
var oauth_consumer_secret = "twitterAppData";
// oauth implementation details
var oauth_version = "1.0";
var oauth_signature_method = "HMAC-SHA1";
// unique request details
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();
// message api details
var status = "Updating status via REST API if this works";
var resource_url = "https://api.twitter.com/1.1/statuses/user_timeline.json";
var screen_name = "user1234";
var count = "1";
// create oauth signature
var baseFormat = "oauth_consumer_key={0}&oauth_nonce={1}&oauth_signature_method={2}" +
"&oauth_timestamp={3}&oauth_token={4}&oauth_version={5}&screen_name={6}&count={7}";
var baseString = string.Format(baseFormat,
oauth_consumer_key,
oauth_nonce,
oauth_signature_method,
oauth_timestamp,
oauth_token,
oauth_version,
Uri.EscapeDataString(screen_name),
Uri.EscapeDataString(count)
);
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)));
}
// create the request header
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)
);
// make the request
ServicePointManager.Expect100Continue = false;
var postBody = "screen_name=" + Uri.EscapeDataString(screen_name);//
var postbody2 = "count=" + Uri.EscapeDataString(count);
resource_url += "?" + postBody + "&" + postbody2;
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();
string responseData = new StreamReader(response.GetResponseStream()).ReadToEnd();
如果我取出所有参与& count = 1
的部分,则上面的代码块有效请提前感谢您为我提供的任何帮助或指示。
Stuart Turbefield
答案 0 :(得分:4)
看起来参数需要在OAUTH签名字符串中按字母顺序排列。我尝试将COUNT切换到开头,它可以工作:
var baseFormat =“ count = {7} & oauth_consumer_key = {0}& oauth_nonce = {1}& oauth_signature_method = {2}”+ “&安培; oauth_timestamp = {3}&安培;组oauth_token = {4}&安培; oauth_version = {5}&安培; SCREEN_NAME = {6}”;
var baseString = string.Format(baseFormat, oauth_consumer_key, oauth_nonce, oauth_signature_method, oauth_timestamp, 组oauth_token, oauth_version, Uri.EscapeDataString(SCREEN_NAME) Uri.EscapeDataString(计数) );
祝你好运,多亏了你,我正在寻找1.1 api的例子。
ķ。