通过api call c#

时间:2015-12-28 18:52:23

标签: c# image file-upload vimeo vimeo-api

我正在尝试设置通过vimeo api完成拉动视频上传的缩略图。我正在为c#windows服务开发这个,请注意,没有官方库。目前我使用this库。我可以通过遵循vimeo文档成功上传视频,但是,当我尝试将图像上传为视频的缩略图时,我遇到了问题。根据{{​​3}},在步骤2中,我需要通过PUT请求上传我的缩略图。它说,我需要做以下事情:

PUT https://i.cloud.vimeo.com/video/518016424
.... binary data of your file in the body ....

我无法弄清楚如何做到这一点。我可以使用

获取图像的二进制数据
byte[] byte_array_of_image = File.ReadAllBytes(file);

但是如何将此数据发送到api并获得响应(使用或不使用库)?如果有帮助的话,这是我上传视频和缩略图的代码。

var vc = VimeoClient.ReAuthorize(
            accessToken: ConfigurationManager.AppSettings["ACCESS_TOKEN"],
            cid: ConfigurationManager.AppSettings["API_KEY"],
            secret: ConfigurationManager.AppSettings["API_SECRET"]
            );
            string temporary_video_dir = ConfigurationManager.AppSettings["TEMP_VIDEO_URL"];
            Dictionary<string,string> automatic_pull_parameters = new Dictionary<string, string>();
            automatic_pull_parameters.Add("type", "pull");
            automatic_pull_parameters.Add("link", temporary_video_dir);
var video_upload_request = vc.Request("/me/videos", automatic_pull_parameters, "POST");
                string uploaded_URI = video_upload_request["uri"].ToString();
                string video_id = uploaded_URI.Split('/')[2];
                Library.WriteErrorLog("Succesfully uploaded Video in test folder. Returned Vimeo ID for video: "+ video_id);
 var picture_resource_request = vc.Request("/videos/" + video_id + "/pictures", null, "POST");
                string picture_resource_link = picture_resource_request["uri"].ToString();
                //Library.WriteErrorLog("uri: " + picture_resource_link);

                byte[] binary_image_data = File.ReadAllBytes("http://testclient.xitech.com.au/Videos/Images/Closing_2051.jpg");
                string thumbnail_upload_link = picture_resource_link.Split('/')[4];

请帮忙!坚持了几个小时。

2 个答案:

答案 0 :(得分:2)

WebClient有一个名为UploadData的方法,就像手套一样。下面有一个例子,说明你可以做什么。

WebClient wb = new WebClient();
wb.Headers.Add("Authorization","Bearer" +AccessToken);
var file =  wb.DownloadData(new Uri("http://testclient.xitech.com.au/Videos/Images/Closing_2051.jpg"));
var asByteArrayContent = wb.UploadData(new Uri(picture_resource_request ), "PUT", file);
var asStringContent = Encoding.UTF8.GetString(asByteArrayContent);

答案 1 :(得分:0)

参考帖子:-Vimeo API C# - Uploading a video

答案没有被接受,但在我的情况下可以尝试很好地工作。 参见下面的代码:-

CREATE VIEW inventorylist_with_percent
AS
SELECT il1.*,
       il1.turnover / (SELECT sum(il2.turnover)
                              FROM inventorylist il2) * 100 percent
       FROM inventorylist il1;