2013-02-24 73 views
0

我需要一些幫助。我嘗試使用YouTube API上傳視頻。我在C#中使用Windows窗體。 這是我做的:youtube api:上傳不起作用

var service = new YouTubeService("kslmfkmlskf",this._sharedConfiguration.youtubeApiKey); 

service.setUserCredentials(this._sharedConfiguration.youtubeUserName, this._sharedConfiguration.youtubeUserPassword); 

var newEntry = new YouTubeEntry(); 
string title = "test"; 
string description = "test test test"; 
string keywords = "testa testb testc"; 

newEntry.Media = new Google.GData.YouTube.MediaGroup(); 
newEntry.Media.Categories.Add(new MediaCategory("Movies", YouTubeNameTable.CategorySchema)); 
newEntry.Media.Keywords = new MediaKeywords(keywords); 
newEntry.Media.Description = new MediaDescription(description); 
newEntry.Media.Title = new MediaTitle(title); 
newEntry.MediaSource = new MediaFileSource("E:\\cine_buzz\\automate_trailer\\toupload\\demo.mov", "video/quicktime"); 

var createEntry = new YouTubeEntry(); 
createEntry = service.Upload(newEntry); 

,我有一個異常返回是這樣的:

{Google.GData.Client.GDataRequestException: Execution of request failed: https://uploads.gdata.youtube.com/feeds/api/users/default/uploads ---> System.Net.WebException: Le serveur distant a retourné une erreur : (400) Demande incorrecte. 
à System.Net.HttpWebRequest.GetResponse() 
à Google.GData.Client.GDataRequest.Execute() 
--- Fin de la trace de la pile d'exception interne --- 
à Google.GData.Client.GDataRequest.Execute() 
à Google.GData.Client.GDataGAuthRequest.Execute(Int32 retryCounter) 
à Google.GData.Client.GDataGAuthRequest.Execute() 
à Google.GData.Client.MediaService.EntrySend(Uri feedUri, AtomBase baseEntry, GDataRequestType type, AsyncSendData data) 
à Google.GData.Client.Service.Insert(Uri feedUri, AtomEntry newEntry, AsyncSendData data) 
à Google.GData.Client.Service.Insert[TEntry](Uri feedUri, TEntry entry) 
à Google.GData.YouTube.YouTubeService.Upload(String userName, YouTubeEntry entry) 
à Google.GData.YouTube.YouTubeService.Upload(YouTubeEntry entry) 
à CineBuzzAutomateTrailer.frmUploadYoutube.upload() dans C:\Users\Yeurl\Documents\Visual Studio 2010\Projects\CineBuzzAutomateTrailer\CineBuzzAutomateTrailer\frmUploadYoutube.cs:ligne 105 
à CineBuzzAutomateTrailer.frmUploadYoutube.btnTest_Click(Object sender, EventArgs e) dans C:\Users\Yeurl\Documents\Visual Studio 2010\Projects\CineBuzzAutomateTrailer\CineBuzzAutomateTrailer\frmUploadYoutube.cs:ligne 113 
à System.Windows.Forms.Control.OnClick(EventArgs e) 
à System.Windows.Forms.Button.OnClick(EventArgs e) 
à System.Windows.Forms.Button.OnMouseUp(MouseEventArgs mevent) 
à System.Windows.Forms.Control.WmMouseUp(Message& m, MouseButtons button, Int32 clicks) 
à System.Windows.Forms.Control.WndProc(Message& m) 
à System.Windows.Forms.ButtonBase.WndProc(Message& m) 
à System.Windows.Forms.Button.WndProc(Message& m) 
à System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m) 
à System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m) 
à System.Windows.Forms.NativeWindow.DebuggableCallback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam) 
à System.Windows.Forms.UnsafeNativeMethods.DispatchMessageW(MSG& msg) 
à System.Windows.Forms.Application.ComponentManager.System.Windows.Forms.UnsafeNativeMethods.IMsoComponentManager.FPushMessageLoop(IntPtr dwComponentID, Int32 reason, Int32 pvLoopData) 
à System.Windows.Forms.Application.ThreadContext.RunMessageLoopInner(Int32 reason, ApplicationContext context) 
à System.Windows.Forms.Application.ThreadContext.RunMessageLoop(Int32 reason, ApplicationContext context) 
à System.Windows.Forms.Application.Run(Form mainForm) 
à CineBuzzAutomateTrailer.Program.Main() dans C:\Users\Yeurl\Documents\Visual Studio 2010\Projects\CineBuzzAutomateTrailer\CineBuzzAutomateTrailer\Program.cs:ligne 18 
à System.AppDomain._nExecuteAssembly(RuntimeAssembly assembly, String[] args) 
à System.AppDomain.ExecuteAssembly(String assemblyFile, Evidence assemblySecurity, String[] args) 
à Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly() 
à System.Threading.ThreadHelper.ThreadStart_Context(Object state) 
à System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean ignoreSyncCtx) 
à System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state) 
à System.Threading.ThreadHelper.ThreadStart()} 

<errors xmlns="http://schemas.google.com/g/2005"> 
- <error> 
    <domain>GData</domain> 
     <code>InvalidEntryException</code> 
     <internalReason>Validation failed</internalReason> 
    </error> 
</errors> 

我不明白這有什麼錯我做什麼。 Thanx求助

回答

2

我認爲你的API訪問沒有配置API控制檯。

我建議你用V3代替GData。有一個與C#和這個非常相似的Java示例。

https://code.google.com/p/youtube-api-samples/source/browse/samples/java/youtube-cmdline-uploadvideo-sample/src/main/java/com/google/api/services/samples/youtube/cmdline/youtube_cmdline_uploadvideo_sample/UploadVideo.java

您還應該設置從API控制檯的API訪問這裏,並從那裏打開YouTube數據API第3版。 「https://code.google.com/apis/console

0

試試這個代碼,這將工作。

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

newVideo.YouTubeEntry.Location = new GeoRssWhere(37, -122); 
// alternatively, you could just specify a descriptive string 
// newVideo.YouTubeEntry.setYouTubeExtension("location", "Mountain View, CA"); 

newVideo.YouTubeEntry.MediaSource = new MediaFileSource("c:\\file.mov", 
    "video/quicktime"); 
Video createdVideo = request.Upload(newVideo); 

參考:https://developers.google.com/youtube/2.0/developers_guide_dotnet?csw=1#Direct_Upload