如何解决Twitter API错误“无效媒体”

时间:2019-10-24 11:17:17

标签: c# twitter

我正在尝试上传15 MB的视频。在步骤INIT中,成功从服务返回了media_id信息

在APPEND步骤中,我们使用Chunk结构,并成功传递每个块值为4mb的字节数组

在“完成”步骤中,我们成功完成了上传过程

但是,当我们使用STATUS检查货件时,会出现以下错误。

{“ media_id”:1187325998846660609,“ media_id_string”:“ 1187325998846660609”,“ media_key”:“ 7_1187325998846660609”,“ size”:8795782,“ processing_info”:{“ state”:“ Failed”,“ progress_percent”:100 ,“错误”:{“代码”:1,“名称”:“ InvalidMedia”,“消息”:“文件无效”}}}

public class InitModel
    {
        public string media_id { get; set; }
        public string media_id_string { get; set; }
        public string expires_after_secs { get; set; }
    }

#region Init
            var client = new RestClient("https://upload.twitter.com/1.1/media/upload.json")
            {
                Authenticator = RestSharp.Authenticators.OAuth1Authenticator.ForProtectedResource("consumerKey", "consumerSecret", "accessToken", "accessTokenSecret")
            };
            var request = new RestRequest(Method.POST);
            request.AddParameter("command", "INIT");
            request.AddParameter("total_bytes", size);
            request.AddParameter("media_type", "video/mp4");
            request.AddParameter("media_category", "tweet_video");

            IRestResponse<InitModel> response = client.Execute<InitModel>(request);


            #endregion

#region Append
const int chunkSize = 4 * 1024 * 1024;
using (var filex = File.OpenRead(path))
{
    int bytesRead, chunkID = 0;
    var buffer = new byte[chunkSize];
    while ((bytesRead = filex.Read(buffer, 0, buffer.Length)) > 0)
    {
    if (bytesRead < chunkSize)
    {
    var lastBuffer = new byte[bytesRead];
    buffer = new byte[bytesRead];
    buffer = lastBuffer;
    }
    try
    {
    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);
    request1.AddFileBytes("media", buffer, "test");
    request1.AddParameter("segment_index", chunkID.ToString());
    IRestResponse response1 = client1.Execute(request1);
    }
    catch (Exception ex)
    {
    Console.WriteLine($"[ERROR] {ex.Message}");
    return;
    }
    chunkID++;
    }
}
#endregion

#region Finalize
            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);

            IRestResponse response2 = client2.Execute(request2);
            #endregion
#region Status
var client4 = new RestClient("https://upload.twitter.com/1.1/media/upload.json")
            {
                Authenticator = RestSharp.Authenticators.OAuth1Authenticator.ForProtectedResource("consumerKey", "consumerSecret", "accessToken", "accessTokenSecret")
            };
            var request4 = new RestRequest(Method.GET);
            request4.AddParameter("command", "STATUS");
            request4.AddParameter("media_id", response.Data.media_id);

            IRestResponse response4 = client4.Execute(request4);
    #endregion

0 个答案:

没有答案