我的目标是获取特定推文的所有转发ID。我尝试使用oAuthTwitterWrapper进行C#twitter身份验证并尝试更改代码,但没有任何成功。当我更改searchFormat以满足我的要求时,我从twitter收到Forbidden消息。 有人请帮忙!
oAuthTwitterWrapper Wrapper - https://github.com/andyhutch77/oAuthTwitterWrapper Stack Exchange Link - Authenticate and request a user's timeline with Twitter API 1.1 oAuth
提前致谢..
答案 0 :(得分:1)
好的,我认为你可以手动开始这样做。
我没有测试过,所以可能会有一些拼写错误,请告诉我,我会相应地更新答案。
这会调用此处指定的api:
https://dev.twitter.com/docs/api/1.1/get/statuses/retweets/%3Aid
// You need to set your own keys and tweet id
var oAuthConsumerKey = "superSecretKey";
var oAuthConsumerSecret = "superSecretSecret";
var oAuthUrl = "https://api.twitter.com/oauth2/token";
var tweetId = "21947795900469248";
// Do the Authenticate
var authHeaderFormat = "Basic {0}";
var authHeader = string.Format(authHeaderFormat,
Convert.ToBase64String(Encoding.UTF8.GetBytes(Uri.EscapeDataString(oAuthConsumerKey) + ":" +
Uri.EscapeDataString((oAuthConsumerSecret)))
));
var postBody = "grant_type=client_credentials";
HttpWebRequest authRequest = (HttpWebRequest)WebRequest.Create(oAuthUrl);
authRequest.Headers.Add("Authorization", authHeader);
authRequest.Method = "POST";
authRequest.ContentType = "application/x-www-form-urlencoded;charset=UTF-8";
authRequest.AutomaticDecompression = DecompressionMethods.GZip | DecompressionMethods.Deflate;
using (Stream stream = authRequest.GetRequestStream())
{
byte[] content = ASCIIEncoding.ASCII.GetBytes(postBody);
stream.Write(content, 0, content.Length);
}
authRequest.Headers.Add("Accept-Encoding", "gzip");
WebResponse authResponse = authRequest.GetResponse();
// deserialize into an object
TwitAuthenticateResponse twitAuthResponse;
using (authResponse)
{
using (var reader = new StreamReader(authResponse.GetResponseStream())) {
JavaScriptSerializer js = new JavaScriptSerializer();
var objectText = reader.ReadToEnd();
twitAuthResponse = JsonConvert.DeserializeObject<TwitAuthenticateResponse>(objectText);
}
}
// Get the retweets by Id
var retweetFormat = "https://api.twitter.com/1.1/statuses/retweets/{0}.json";
var retweetsUrl = string.Format(retweetFormat, tweetId);
HttpWebRequest retweetRequest = (HttpWebRequest)WebRequest.Create(retweetsUrl);
var retweetHeaderFormat = "{0} {1}";
timeLineRequest.Headers.Add("Authorization", string.Format(retweetHeaderFormat, twitAuthResponse.token_type,
twitAuthResponse.access_token));
retweetRequest.Method = "Get";
WebResponse retweetResponse = timeLineRequest.GetResponse();
var retweetJson = string.Empty;
using (retweetResponse)
{
using (var reader = new StreamReader(retweetResponse.GetResponseStream()))
{
retweetJson = reader.ReadToEnd();
}
}
//parse the json from retweetJson to read the returned id's
public class TwitAuthenticateResponse {
public string token_type { get; set; }
public string access_token { get; set; }
}
如果这有效并且您有时间请通过GitHub提交拉取请求,我会将其包含在oauthtwitterwrapper中。