Search code examples
c#.net-corevimeovimeo-api

How do I get the video upload link for uploading a video to my Vimeo account?


How do I get the video upload link for uploading a video to my Vimeo account using the Vimeo API with C# and .Net Core?

Answer will be a method that will be called like this:

var vimeoUploadUrl =  await getVimeoUploadUrl(videoFileSizeInBytes, accessToken).ConfigureAwait(false);

Solution

  • To do this you have to have a Vimeo account (this works with Plus level or better, don't know about the others), have created a Vimeo app (in Vimeo), given it permission to upload, and gotten an access token. With all that in place, here is the code:

        HttpClient httpClient = new HttpClient();
    
        public async Task<string> getVimeoUploadUrl(int videoFileSize, string accessToken)
        {
            var vimeoUploadUrl = "";
    
            string vimeoApiUrl = "https://api.vimeo.com/me/videos"; // Vimeo URL
    
            try
            {
    
                string body =
                    @"{'upload': {'approach': 'post','size': '" + videoFileSize + "'}}".Replace("'", "\"");
    
                HttpContent content = new StringContent(body);
    
                using (var requestMessage = new HttpRequestMessage(HttpMethod.Post, vimeoApiUrl))
                {
                    requestMessage.Headers.Authorization =
                        new AuthenticationHeaderValue("Bearer", accessToken);
                    requestMessage.Headers.Add("Accept", "application/vnd.vimeo.*+json;version=3.4");
                    requestMessage.Headers.Add("ContentType", "application/json");
                    requestMessage.Content = content;
                    var response = await httpClient.SendAsync(requestMessage).ConfigureAwait(false);
    
                    response.EnsureSuccessStatusCode();
    
                    var result = await response.Content.ReadAsStringAsync().ConfigureAwait(false);
    
                    var myJsonObject = JObject.Parse(result);
    
                    vimeoUploadUrl = myJsonObject.SelectToken("upload.upload_link").ToString();                  
                }
    
            }
            catch (Exception err)
            {
            // Do your own error handling!   
            }
    
            return vimeoUploadUrl;
    
        }