供您参考,我使用的是”TweetinviAPI(5.0.4)”nugget包
我在这里找到了一些与媒体发布推文相关的东西,但我不知道如何在C#中使用它:
https://developer.twitter.com/en/docs/twitter-api/tweets/manage-tweets/api-reference/post-tweets
Here is an example --> media.media_ids
目前我有一个JsonProperty,叫做“text”。完全没问题。以下是我的“海报”类:
namespace MainData.Twitter_API_v2
{
public class TweetsV2Poster
{
private readonly ITwitterClient client;
public TweetsV2Poster(ITwitterClient client)
{
this.client = client;
}
public Task<ITwitterResult> PostTweet(TweetV2PostRequest tweetParams)
{
return this.client.Execute.AdvanceRequestAsync(
(ITwitterRequest request) =>
{
var jsonBody = this.client.Json.Serialize(tweetParams);
var content = new StringContent(jsonBody, Encoding.UTF8, "application/json");
request.Query.Url = "https://api.twitter.com/2/tweets";
request.Query.HttpMethod = Tweetinvi.Models.HttpMethod.POST;
request.Query.HttpContent = content;
}
);
}
}
public class TweetV2PostRequest
{
/// <summary>
/// The text of the tweet to post.
/// </summary>
[JsonProperty("text")]
public string Text { get; set; } = string.Empty;
}
}
这里我叫“海报”类:
//Get the byte[] --> itemshopImg
GetItemshopImage dailyData = new GetItemshopImage();
var itemshopImg = dailyData.GetItemshopImg();
//Create Twitter Client
var client = new TwitterClient(
"x",
"x",
"x",
"x"
);
//Upload Tweet Image
var tweetItemshopImg = await client.Upload.UploadTweetImageAsync(itemshopImg);
//Post Tweet
var poster = new TweetsV2Poster(client);
ITwitterResult result = await poster.PostTweet(
new TweetV2PostRequest
{
Text = "myText"
});
我的目标是使用“tweetItemshopImg”变量中的Id (开始时图像中的media.id),并将其赋予“TweetsV2Poster”类中的JSON属性。
1条答案
按热度按时间jmo0nnb31#
要生成documentation中所示的JSON示例:
修改
TweetV2PostRequest
,如下所示:然后调用
poster.PostTweet()
如下:注意事项:
BaseTweetsV2Parameters
源代码的search出现了几个派生类型,比如GetTweetsV2Parameters
,但没有PostTweetsV2Parameters
。TweetsV2Poster
如下:没有Assert,表明JSON与示例匹配(除了格式化)。