Я могу загружать картинки и видео с приведенным ниже примером кода, но при попытке загрузить изображения выдает следующую ошибку. Я получаю успех на этапах INIT, APPEND и FINALIZE, но при проверке шага MEDIA WITH TWEET получаю ошибку.
INIT
private static string path = @"..\Desktop\file.jpg";
private static string size = new FileInfo(path).Length.ToString();
var client = new RestClient("https://upload.twitter.com/1.1/media/upload.json")
{
Authenticator = RestSharp.Authenticators.OAuth1Authenticator.ForProtectedResource("consumerKey","consumerSecret","accessToken","accessTokenSecret")
};
var mimeType = MimeMapping.GetMimeMapping(path.Split('\\').Last());
var request = new RestRequest(Method.POST);
request.AddParameter("command", "INIT");
request.AddParameter("total_bytes", size);
request.AddParameter("media_type", mimeType);
request.AddParameter("media_category", "tweet_image");
IRestResponse response = client.Execute(request);
RESULT
{"media_id ": 1191965427213250561," media_id_string ":" 1191965427213250561 "," expires_after_secs ": 86399," media_key ":" 3_1191965427213250561 "}
APPEND * 101 *
const int chunkSize = 2 * 1024 * 1024; //2MB
var fileArr = File.ReadAllBytes(path);
var chkList = GetBinaryChunks(fileArr, chunkSize);
var count = 0;
foreach (var item in chkList)
{
var client1 = new RestClient("https://upload.twitter.com/1.1/media/upload.json")
{
Authenticator = RestSharp.Authenticators.OAuth1Authenticator.ForProtectedResource("consumerKey","consumerSecret","accessToken","accessTokenSecret")
};
var request1 = new RestRequest(Method.POST);
request1.AddHeader("Content-Type", "multipart/form-data");
request1.AddParameter("command", "APPEND");
request1.AddParameter("media_id", response.Data.media_id_string);
request1.AddFileBytes("media", item, "test");
request1.AddParameter("segment_index", count.ToString());
IRestResponse response1 = client1.Execute(request1);
count++;
}
1012 * *1011* 1012 * 101 *
1014 *1012* 1014
var client2 = new RestClient("https://upload.twitter.com/1.1/media/upload.json")
{
Authenticator = RestSharp.Authenticators.OAuth1Authenticator.ForProtectedResource("consumerKey","consumerSecret","accessToken","accessTokenSecret")
};
var request2 = new RestRequest(Method.POST);
request2.AddHeader("Content-Type", "application/x-www-form-urlencoded");
request2.AddParameter("command", "FINALIZE");
request2.AddParameter("media_id", response.Data.media_id_string);
IRestResponse response2 = client2.Execute(request2);
РЕЗУЛЬТАТ
{"media_id": 1191965427213250561, "media_id_string": "1191965427213250561", "media_key": "3_1191965427213250561", "размер": 2504642, "expires_after_secs": 8: "8":"image_type": "image / jpeg", "w": 5760, "h": 3840}}
НОСИТЕЛЬ С ТВИТОМ
var client6 = new RestClient("https://api.twitter.com/1.1/statuses/update.json")
{
Authenticator = RestSharp.Authenticators.OAuth1Authenticator.ForProtectedResource("consumerKey","consumerSecret","accessToken","accessTokenSecret")
};
var request6 = new RestRequest(Method.POST);
request6.AddParameter("status", "6mb jpg");
request6.AddParameter("in_reply_to_status_id", "xxxxxxxxxxxxxxx");
request6.AddParameter("media_ids", response.Data.media_id_string);
IRestResponse response6 = client6.Execute(request6);
РЕЗУЛЬТАТ
{"ошибки ": [{" code ": 324," message ":" Invalid Media "}]}