【问题标题】:Get retweets of a tweet in C# oAuthTwitterWrapper在 C# oAuthTwitterWrapper 中获取推文的转推
【发布时间】:2014-02-13 14:52:55
【问题描述】:

我的目标是获取特定推文的所有转推 ID。我尝试使用 oAuthTwitterWrapper 进行 C# twitter 身份验证并尝试更改代码,但没有任何成功。当我更改 searchFormat 以满足我的要求时,我收到了来自 Twitter 的禁止消息。 有人请帮忙!

oAuthTwitterWrapper 包装器 - https://github.com/andyhutch77/oAuthTwitterWrapper 堆栈交换链接 - Authenticate and request a user's timeline with Twitter API 1.1 oAuth

提前谢谢..

【问题讨论】:

标签: c# asp.net twitter webclient oauthtwitterwrapper


【解决方案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 中。

【讨论】:

  • 没问题。祝你好运。 :)
猜你喜欢
  • 1970-01-01
  • 2017-09-30
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-07-30
  • 2021-01-27
  • 2017-04-11
  • 1970-01-01
相关资源
最近更新 更多