带有视频文件块的Facebook AdVideo上传出现异常

时间:2019-01-11 11:41:31

标签: c# facebook-graph-api

我正在使用Facebook Graph API将“上传广告视频”上传到Facebook,但无法正常工作。通话中缺少一些字节。

我的问题是video_file_chunk,在发送分块的“文件开始”和“结束”偏移量时,情况有所不同。

第一次迭代 起始偏移为0 结束偏移为1048576

在响应中,我们得到第二次迭代的偏移量

起始偏移为1048578 结束偏移为2048578

这里显然缺少一个字节。在最后一次迭代中,代码被破坏了。给出一些通用异常。

“您的视频上载在完成之前就超时了。这可能是由于网络连接速度慢或您尝试上载的视频太大。请重试。”

var fb = GetFB();

POST     /generic-api

    {    
      "appName" : "myApp",
      "apiPath" : "/api/myFeature",    
      "urlParams" : [    
        {"key1" : "myKey1", "value1" : "myValue1"},    
        {"key2" : "myKey2", "value2" : "myValue2"},
        (...)    
      ],    
      "headerParams": [    
        {"key1" : "myKey1", "value1" : "myValue1"},    
        {"key2" : "myKey2", "value2" : "myValue2"},
        (...)
      ],    
      "body" :"" // Json body object stringified    
    }

在此先感谢您的帮助。

1 个答案:

答案 0 :(得分:0)

facebook SDK错误!!!

修复fbsdk代码: https://github.com/facebook-csharp-sdk/facebook-csharp-sdk/blob/v7.0.6/Source/Facebook/FacebookClient.cs行:541

...........

indexOfDisposableStreams.Add(streams.Count);
streams.Add(new MemoryStream(fileData));
indexOfDisposableStreams.Add(streams.Count);
streams.Add(new MemoryStream(Encoding.UTF8.GetBytes(MultiPartNewLine)));// facebook server can't process this. delete this line !

请重新构建Facebook SDK〜 ......

--------------示例代码:

 using (FileStream fs = new FileStream("E:\\cnp.mp4",
                           FileMode.Open,
                           FileAccess.Read))
        {


            dynamic parameters = new System.Dynamic.ExpandoObject();
            parameters.upload_phase = "start";
            parameters.file_size = fs.Length;
            var fileSize = (int)fs.Length;
            Console.WriteLine("file_size: {0}", fileSize);


            dynamic result = fb.Post("/" + FBAccountID + "/advideos", parameters);

            string upload_session_id = (string)result["upload_session_id"];

            Console.WriteLine("");
            Console.WriteLine("upload_session_id: {0}", (string)result["upload_session_id"]);
            Console.WriteLine("Video Id: {0}", (string)result["video_id"]);
            Console.WriteLine("");
            Console.WriteLine("start_offset: {0}", (string)result["start_offset"]);
            Console.WriteLine("end_offset: {0}", (string)result["end_offset"]);

            int startOffset = int.Parse((string)result["start_offset"]);
            int endOffset = int.Parse((string)result["end_offset"]);

            int length = endOffset - startOffset;

            int i = 1;
            int totalBytesRead = 0;
            BinaryReader br = new BinaryReader(fs);
            while (startOffset < endOffset)
            {                   
                 byte[] buff = new byte[length];
                 fs.Read(buff, 0, length);

                totalBytesRead += buff.Length;                

                Console.WriteLine("buff length: {0}", buff.Length);
                Console.WriteLine("Total Bytes Read: {0}", totalBytesRead);
                dynamic parameters1 = new System.Dynamic.ExpandoObject();
                parameters1.upload_phase = "transfer";
                parameters1.upload_session_id = upload_session_id;
                parameters1.start_offset = startOffset;
                parameters1.video_file_chunk = new FacebookMediaObject { ContentType = "video/mp4", FileName = "cnp " + i + ".mp4" }.SetValue(buff);
                i++;
                dynamic result1 = fb.Post("/" + FBAccountID + "/advideos", parameters1);

                Console.WriteLine("");
                Console.WriteLine("start_offset: {0}", (string)result1["start_offset"]);
                Console.WriteLine("end_offset: {0}", (string)result1["end_offset"]);
                startOffset = int.Parse((string)result1["start_offset"]);

                endOffset = int.Parse((string)result1["end_offset"]);
                length = endOffset - startOffset;
            }

            dynamic parameters2 = new System.Dynamic.ExpandoObject();
            parameters2.upload_phase = "finish";
            parameters2.upload_session_id = upload_session_id;
            parameters2.title = "Video title";
            dynamic result2 = fb.Post("/" + FBAccountID + "/advideos", parameters2);
            Console.WriteLine("success: {0}", result2["success"].ToString());