Click here to Skip to main content
14,451,447 members
Rate this:
Please Sign up or sign in to vote.
See more: , +
So I try to upload a video to Dailymotion with API using C# and after uploading 4 to 5 videos successfully I get an exception the remote server returned an error (403) Forbidden Please help how to solve this Thanks

I am getting an error in publish video method at

var response = request.GetResponse();




My CODE:

What I have tried:

using System;
using System.Diagnostics;
using System.IO;
using System.Net;
using System.Text;
using System.Web;
using Newtonsoft.Json;

namespace SO_Dailymotion_Upload
{
    class Program
    {
        static void Main(string[] args)
        {
            var accessToken = GetAccessToken();
            Authorize(accessToken);

            Console.WriteLine("Access token is " + accessToken);


            var fileToUpload = @"C:\Users\Hassan\Downloads\P6090053.3gp";

            Console.WriteLine("File to upload is " + fileToUpload);

            var uploadUrl = GetFileUploadUrl(accessToken);

            Console.WriteLine("Posting to " + uploadUrl);

            var response = GetFileUploadResponse(fileToUpload, accessToken, uploadUrl);
             
            Console.WriteLine("Response:\n");

            Console.WriteLine(response + "\n");

            Console.WriteLine("Publishing video.\n");
            var uploadedResponse = PublishVideo(response, accessToken);
         
            Console.WriteLine(TEST);
            Console.WriteLine(uploadedResponse);

            Console.WriteLine("Done. Press enter to exit.");
            Console.ReadLine();
        }

        private static UploadResponse GetFileUploadResponse(string fileToUpload, string accessToken, string uploadUrl)
        {
            var client = new WebClient();
            client.Headers.Add("Authorization", "OAuth " + accessToken);

            var responseBytes = client.UploadFile(uploadUrl, fileToUpload);

            var responseString = Encoding.UTF8.GetString(responseBytes);

            var response = JsonConvert.DeserializeObject<UploadResponse>(responseString);

            return response;
        }
      
        
        private static UploadedResponse PublishVideo(UploadResponse uploadResponse, string accessToken)
        {
            Console.WriteLine(uploadResponse);
            var request = WebRequest.Create("https://api.dailymotion.com/me/videos?url=" + HttpUtility.UrlEncode(uploadResponse.url)+ "" );
            request.Method = "POST";
            request.ContentType = "application/x-www-form-urlencoded";
            request.Headers.Add("Authorization", "OAuth " + accessToken);

            var requestString = String.Format("title={0}&tags={1}&channel={2}&private={3}&published={4}",
                HttpUtility.UrlEncode("TEST"),
                HttpUtility.UrlEncode("tag1"),
                HttpUtility.UrlEncode("news"),
                HttpUtility.UrlEncode("true"),
                HttpUtility.UrlEncode("true"));

            var requestBytes = Encoding.UTF8.GetBytes(requestString);

            var requestStream = request.GetRequestStream();

            requestStream.Write(requestBytes, 0, requestBytes.Length);

            var response = request.GetResponse();
            Console.WriteLine(response);
            var responseStream = response.GetResponseStream();
            Console.WriteLine(responseStream);
            string responseString;
            using (var reader = new StreamReader(responseStream))
            {
                responseString = reader.ReadToEnd();
                Console.WriteLine(responseString);
            }

            var uploadedResponse = JsonConvert.DeserializeObject<UploadedResponse>(responseString);
            
            return uploadedResponse;
        }

        private static string GetAccessToken()
        {
            var request = WebRequest.Create("https://api.dailymotion.com/oauth/token");
            request.Method = "POST";
            request.ContentType = "application/x-www-form-urlencoded";

            var requestString = String.Format("grant_type=password&client_id={0}&client_secret={1}&username={2}&password={3}",
                HttpUtility.UrlEncode(SettingsProvider.Key),
                HttpUtility.UrlEncode(SettingsProvider.Secret),
                HttpUtility.UrlEncode(SettingsProvider.Username),
                HttpUtility.UrlEncode(SettingsProvider.Password));

            var requestBytes = Encoding.UTF8.GetBytes(requestString);

            var requestStream = request.GetRequestStream();

            requestStream.Write(requestBytes, 0, requestBytes.Length);

            var response = request.GetResponse();

            var responseStream = response.GetResponseStream();
            string responseString;
            using (var reader = new StreamReader(responseStream))
            {
                responseString = reader.ReadToEnd();
            }

            var oauthResponse = JsonConvert.DeserializeObject<OAuthResponse>(responseString);

            return oauthResponse.access_token;
        }

        private static void Authorize(string accessToken)
        {
            var authorizeUrl = String.Format("https://api.dailymotion.com/oauth/authorize?response_type=code&client_id={0}&scope=read+write+manage_videos+delete&redirect_uri={1}",
                HttpUtility.UrlEncode(SettingsProvider.Key),
                HttpUtility.UrlEncode(SettingsProvider.CallbackUrl));

            Console.WriteLine("We need permissions to upload. Press enter to open web browser.");
            Console.ReadLine();

            Process.Start(authorizeUrl);

            var client = new WebClient();
            client.Headers.Add("Authorization", "OAuth " + accessToken);

            Console.WriteLine("Press enter once you have authenticated and been redirected to your callback URL");
            Console.ReadLine();
        }

        private static string GetFileUploadUrl(string accessToken)
        {
            var client = new WebClient();
            client.Headers.Add("Authorization", "OAuth " + accessToken);

            var urlResponse = client.DownloadString("https://api.dailymotion.com/file/upload");

            var response = JsonConvert.DeserializeObject<UploadRequestResponse>(urlResponse).upload_url;

            return response;
        }
    }
}
Posted
Updated 4-Sep-19 3:33am

1 solution

Rate this:
Please Sign up or sign in to vote.

Solution 1

So it worked for 4 to 5 videos then stopped?

Try this Google[^] search - then you will discover that you are limited
Quote:
To prevent spam, all Dailymotion users are limited to: 10 uploads per day. 2 hours of video per day. 60 minutes per video.
You have exceeded your quota.
   
Comments
tyson Hamda 4-Sep-19 9:59am
   
Thanks!

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100