toing_toing
toing_toing

Reputation: 2442

Upload thumbnail image to vimeo via api call c#

I am trying to set the thumbnail for a pull video upload done through the vimeo api. I am developing this for a c# windows service and please note that there are no official libraries for this. For the moment I use this library. I am able to successfully upload the video by following the vimeo documentation, however, when I try to upload an image to be the thumbnail of a video I get an issue. According to the vimeo picutre upload documentation, in step 2, i need to upload my thumbnail image via a PUT request. It says, that I need to do the following:

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

I can't figure out how to do this. I can get the binary data of the image by using

byte[] byte_array_of_image = File.ReadAllBytes(file);

but how can I send this data to the api and get a response (with or without using the library)? If it would help, here is my code to upload the video and thumbnail done so far.

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];

Please help! Stuck for hours now.

Upvotes: 1

Views: 997

Answers (2)

Dheeraj Singh
Dheeraj Singh

Reputation: 109

reference post:- Vimeo API C# - Uploading a video

the answer is not upvoted but it could be tried worked well in my case. see the code below:-

     public ActionResult UploadChapterVideoVimeo(HttpPostedFileBase file, string productID = "")
{
    if (file != null){      
                      var authCheck = Task.Run(async () => await vimeoClient.GetAccountInformationAsync()).Result;
                if (authCheck.Name != null)
                {
                    
                    BinaryContent binaryContent = new BinaryContent(file.InputStream, file.ContentType);
                    int chunkSize = 0;
                    int contenetLength = file.ContentLength;
                    int temp1 = contenetLength / 1024;
                    if (temp1 > 1)
                    {
                        chunkSize = temp1 / 1024;
                        chunkSize = chunkSize * 1048576;
                    }
                    else
                    { chunkSize = chunkSize * 1048576; }
                    binaryContent.OriginalFileName = file.FileName;
                    var d = Task.Run(async () => await vimeoClient.UploadEntireFileAsync(binaryContent, chunkSize, null)).Result;
                    vmodel.chapter_vimeo_url = "VIMEO-" + d.ClipUri;
                }

                return RedirectToAction("ProductBuilder", "Products", new { productId = EncryptedProductID, message = "Successfully Uploaded video", type = 1 });
            }
        }
        catch (Exception exc)
        {
            return RedirectToAction("ProductBuilder", "Products", new { productId = EncryptedProductID, message = "Failed to Uploaded video " + exc.Message, type = 0 });
        }
    }
    return null;        }

Upvotes: 0

Cleiton
Cleiton

Reputation: 18113

WebClient has a method called UploadData that fits like a glove. Below there is an example that what you can do.

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);

Upvotes: 2

Related Questions