C# Vimeo 上传 - 不起作用

问题描述 投票:0回答:2

我正在尝试使用 Vimeo 文档将视频上传到我在 Vimeo 中的专业帐户:https://developer.vimeo.com/api/upload/videos#upload-your-video

为了尝试一下,我制作了一个简单的 C# 控制台应用程序: 我可以获得upload_ticket。

当我使用 WebClient.UploadData“放置”视频时,文件被发送。

try
        {
            WebClient wc = new WebClient();
            wc.Headers.Clear();
            wc.Headers.Add("Authorization", "bearer xxxxxxxxxxx");
            wc.Headers.Add("type", "streaming");                

            var vimeoTicket = JsonConvert.DeserializeObject<JObject>(wc.UploadString("https://api.vimeo.com/me/videos", "POST", ""));

            var file = File.ReadAllBytes(@"d:\3.mp4");

            wc.Headers.Clear();                

            var result= wc.UploadData(new Uri(vimeoTicket["upload_link_secure"].ToString()), "PUT", file);              

            WebClient wc1 = new WebClient();
            wc1.Headers.Clear();
            wc1.Headers.Add("Content-Range", "bytes */*");

            //This line will get me an execption {"The remote server returned an error: (308) Resume Incomplete."}
            var ff1 = wc1.UploadData(vimeoTicket["upload_link_secure"].ToString(), "PUT", new byte[0]);                
        }
        catch (Exception h)
        {

            throw;
        }

在 API 文档中,它说“如果此文件存在,这将返回一个带有 HTTP 308 状态代码和包含服务器上字节数的 Range 标头的响应。”

那么为什么我会遇到异常,并且没有像文档中那样得到任何响应??

谢谢你

c# vimeo-api
2个回答
0
投票

由于某种原因,WebClient 将响应 308 处理为错误,目前我已设法将所需的代码放入异常块中 - 直到我找到一种方法来说服 WebClinet 308 不是敌人......


0
投票
    [HttpPost]
    public string SaveVideoToVimeo(string fileName, long size, string caption, string description)
    {
        System.Net.ServicePointManager.SecurityProtocol = System.Net.SecurityProtocolType.Tls12;
        try
        {
            var path = Path.Combine(Server.MapPath("~/upload/question/files"), fileName + ".webm");

            string vVimeURL = "https://api.vimeo.com/me/videos/";
            WebClient wc = new WebClient();

            wc.Headers.Clear();
            wc.Headers.Add("Authorization", "bearer xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx");
            wc.Headers.Add("Content-Type", "application/json");
            wc.Headers.Add("Accept", "application/vnd.vimeo.*+json;version=3.4");
            wc.Encoding = System.Text.Encoding.UTF8;
            string vData = "{\"upload\": {" + "\"approach\":\"tus\"," + "\"size\":\"" + size + "\" }, \"name\": \"" + caption + "\", \"description\":\"" + description + "\"}";
            var result = wc.UploadString(vVimeURL, "POST", vData);

            var vimeoTicket = JsonConvert.DeserializeObject<JObject>(result,
                    new JsonSerializerSettings { NullValueHandling = NullValueHandling.Ignore });
            var LocationLink = vimeoTicket["upload"];
            var link = vimeoTicket["link"];

            string xx = vimeoTicket["uri"].ToString();
            wc.Headers.Clear();
            wc.Headers.Add("Content-Type", "application/offset+octet-stream");
            wc.Headers.Add("Accept", "application/vnd.vimeo.*+json;version=3.4");
            wc.Headers.Add("Tus-Resumable", "1.0.0");
            wc.Headers.Add("Upload-Offset", "0");

            string vupload_link = LocationLink["upload_link"].ToString(); ;
            byte[] vResponse = wc.UploadFile(vupload_link, "PATCH", path);

            var returndata = new { code = 0, message = "Kayıt Başarıyla Yüklendi", style = "success", link = link };

            return JsonConvert.SerializeObject(returndata, Formatting.Indented);

        }
        catch (Exception ex)
        {
            var returndata = new { code = ex.HResult, message = ex.Message, style = "error", link = "" };
            return JsonConvert.SerializeObject(returndata, Formatting.Indented);
        }

    }
© www.soinside.com 2019 - 2024. All rights reserved.