2013-03-05 22 views
0

我試圖使用API​​ v2.0將視頻從c#web應用程序上傳到Youtube。以編程方式提交HTML表單以進行基於瀏覽器的上傳

這是我發現的文檔:

YouTube API 2.0 docs

我能做到的AuthSub的Web應用程序和發送視頻元數據到YouTube; 之後,我可以讀取使瀏覽器上傳所需的TOKEN和URL。

現在我要填寫這個HTML表單來執行真正的上傳。

<form action="post_url?nexturl=http://example.com" method ="post" enctype="multipart/form-data"> 
<input type="file" name="file"/> 
<input type="hidden" name="token" value="token_value"/> 
<input type="submit" value="go" /> 
</form> 

我搜索網爲例進行編程如何「模仿」,並從代碼隱藏發送此HTML表單沒有運氣。

這是我的實際代碼僅用於測試目的寫着:

遠程服務器返回錯誤:(400)錯誤的請求

YouTubeRequestSettings settings = new YouTubeRequestSettings("TEST_APP", "DEVELOPER_KEY", "USERNAME", "PASSWORD"); 
     YouTubeRequest request = new YouTubeRequest(settings); 


     Video newVideo = new Video(); 

     newVideo.Title = "My Test Movie"; 
     newVideo.Tags.Add(new MediaCategory("Autos", YouTubeNameTable.CategorySchema)); 
     newVideo.Keywords = "cars, funny"; 
     newVideo.Description = "My description"; 
     newVideo.YouTubeEntry.Private = false; 
     newVideo.Tags.Add(new MediaCategory("mydevtag, anotherdevtag", YouTubeNameTable.DeveloperTagSchema)); 

     FormUploadToken token_youtube = request.CreateFormUploadToken(newVideo); 
     string url_per_post_youtube = token_youtube.Url + "?nexturl=" + HttpContext.Current.Request.Url.AbsoluteUri + "test.aspx"; 

     byte[] fileBytes = File.ReadAllBytes("C:\\VIDEO.MOV"); 
     StringBuilder sb = new StringBuilder(); 

     foreach (byte b in fileBytes) 
     { 
      sb.Append(Convert.ToString(b, 2).PadLeft(8, '0')); 
     } 

     ASCIIEncoding encoding = new ASCIIEncoding(); 
     string postData = "token=" + token_youtube.Token.ToString(); 
     postData += ("&file=" + sb); 
     byte[] data = encoding.GetBytes(postData); 
     try 
     { 
      // Prepare web request... 
      HttpWebRequest myRequest = 
       (HttpWebRequest)WebRequest.Create(url_per_post_youtube); 
      myRequest.Method = "POST"; 
      myRequest.ContentType = "multipart/form-data"; 
      myRequest.KeepAlive = true; 
      myRequest.ContentLength = data.Length; 
      Stream newStream = myRequest.GetRequestStream(); 
      // Send the data. 
      newStream.Write(data, 0, data.Length); 
      newStream.Close(); 

      string strResponse; 
      StreamReader stIn = new StreamReader(myRequest.GetResponse().GetResponseStream()); 
      strResponse = stIn.ReadToEnd(); 
      stIn.Close(); 
     } 
     catch (Exception ex) 
     { 
      string asd = ex.Message; 
     } 

有了這個代碼與YouTube的遠程服務器的回覆

A 400 response code identifies a bad request. For example, you will receive a 400 response code if you submit a request to the wrong URL or include an unsupported or nonexistent parameter in your request. The API response content will explain the reason why the API returned a 400 response code

我無法找到了解什麼方式appenin'

回答

1

所以你需要一種方法來了解什麼是appenin'?我認爲最好的方法是使用工具查找,確切地說您發送給YouTube的消息。

fiddler是一個非常有用的監控http流量的工具。有了這個,你可以看到你正在發送的內容。要麼你沒有發送你認爲你正在發送的消息,要麼你錯誤地發送了你需要發送的消息。這應該讓你確定其中哪一個發生了。

相關問題