2012-04-01 64 views
4

我希望在dailymotion上用c#代碼上傳視頻,但dailymotion不提供用c#上傳視頻的c#代碼。如何上傳與DailyMotion視頻與C#?有人有完整的代碼嗎?

我搜索在Dailymotion文件API,我發現這不是明確的捲曲代碼:

curl -F 'access_token=...' \ 
    -F 'url=http://upload-02.dailymotion.com/files/5ccb48b8e8aef3fcb8959739f993e1b9.3gp' \ 
    https://api.dailymotion.com/me/videos 

和我tryed變調,但它不工作:

string contentFile = "c:\name_of_my_video_file.flv"; 
      byte[] byteArray = Encoding.ASCII.GetBytes(contentFile); 
      MemoryStream fs = new MemoryStream(byteArray); 

      // Provide the WebPermission Credintials 
      // Create a request using a URL that can receive a post. 
      string uri = "https://api.dailymotion.com/me/videos"; 
      WebRequest request = WebRequest.Create(uri); 
      // Set the Method property of the request to POST. 
      request.Method = "POST"; 
      request.Credentials = new NetworkCredential("logindailymotion","passworddailymotion"); 

      // Create POST data and convert it to a byte array. 
      string postData = "access_token=my_api_key&url=http://upload-02.dailymotion.com/files/name_of_my_video_file.flv"; 
      byte[] byteArray = Encoding.UTF8.GetBytes(postData); 
      // Set the ContentType property of the WebRequest. 
      request.ContentType = "application/x-www-form-urlencoded"; 
      // Set the ContentLength property of the WebRequest. 
      request.ContentLength = byteArray.Length; 
      // Get the request stream. 

      // Notify the server about the size of the uploaded file 
      request.ContentLength = fs.Length; 

      // The buffer size is set to 2kb 
      int buffLength = 2048; 
      byte[] buff = new byte[buffLength]; 
      int contentLen; 

      // Opens a file stream (System.IO.FileStream) to read the file to be uploaded 
      //FileStream fs = fileInf.OpenRead(); 

      try 
      { 
       // Stream to which the file to be upload is written 
       Stream strm = request.GetRequestStream(); 

       // Read from the file stream 2kb at a time 
       contentLen = fs.Read(buff, 0, buffLength); 

       // Till Stream content ends 
       while (contentLen != 0) 
       { 
        // Write Content from the file stream to the FTP Upload Stream 
        strm.Write(buff, 0, contentLen); 
        contentLen = fs.Read(buff, 0, buffLength); 
       } 

       // Close the file stream and the Request Stream 
       strm.Close(); 
       fs.Close(); 
      } 
      catch (Exception ex) 
      { 
       throw ex; 
      } 

     } 

是否有C#中的更好的文檔代碼?

有人有正確的代碼嗎?

+0

我不知道上傳部分,但你使用ASCII的視頻文件? - 使用File.ReadAllBytes()代替它會更好嗎? – Star 2012-04-01 23:47:44

回答

6

我創建了一個完整的示例來回答您的問題並上傳視頻。現在有on GitHub

編輯:我用API信息解釋如何做他們的OAuth AuthenticationVideo Publishing

這裏是除了對於從JSON反序列化的對象的全部代碼:

代碼

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

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

     var fileToUpload = @"C:\Program Files\Common Files\Microsoft Shared\ink\en-US\join.avi"; 

     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(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) 
    { 
     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}&published={3}", 
      HttpUtility.UrlEncode("some title"), 
      HttpUtility.UrlEncode("tag1"), 
      HttpUtility.UrlEncode("news"), 
      HttpUtility.UrlEncode("true")); 

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

同樣,I put it on GitHub

+0

我編譯並執行你的代碼,程序做了一個很好的處理,沒有錯誤,(請參閱控制檯結果:http://www.dz-axium.com/dailymotion.png)。但注意到:沒有上傳視頻。 – user1018697 2012-04-02 14:53:24

+0

@ user1018697,事實證明你需要額外的授權,這必須由用戶執行,然後他們將被重定向。我在GitHub上更新了應用程序,現在在secrets.xml中,您需要將您的callbackurl **正好設置爲您在DailyMotion開發人員設置中設置的**,例如, 'http:// site.com/path/to/file.aspx'或其他。請重新下載並重試,如果這能解決您的問題,請接受。 – 2012-04-02 16:23:44