gpt4 book ai didi

c# - 在 C# oAuthTwitterWrapper 中获取推文的转发

转载 作者:太空宇宙 更新时间:2023-11-03 13:29:06 26 4
gpt4 key购买 nike

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

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

提前致谢..

最佳答案

好的,我认为这就是您开始手动执行此操作的方式。

我还没有测试过,所以可能有一些拼写错误,让我知道,我会相应地更新它的答案。

这会调用此处指定的 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 中。

关于c# - 在 C# oAuthTwitterWrapper 中获取推文的转发,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21301719/

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