2013-04-04 203 views
0

這讓我瘋狂!我正在使用下面的代碼。 90%的時間我得到以下錯誤。每隔一段時間它都可以正常工作。請幫忙。我檢查了開發人員密鑰,用戶名和密碼。YouTube API - 視頻上傳 - 401未授權

Error: System.Net.WebException: The remote server returned an error: (401) Unauthorized. at System.Net.HttpWebRequest.GetResponse()
at Google.GData.Client.ResumableUpload.ResumableUploader.UploadStreamPart(Int32 partIndex, String httpMethod, Uri sessionUri, Authenticator authentication, Stream payload, String mediaType, AsyncData data)
at Google.GData.Client.ResumableUpload.ResumableUploader.UploadStream(String httpMethod, Uri sessionUri, Authenticator authentication, Stream payload, String mediaType, AsyncData data) at Google.GData.Client.ResumableUpload.ResumableUploader.Insert(Authenticator authentication, AbstractEntry payload, AsyncData data) at Google.GData.Client.ResumableUpload.ResumableUploader.AsyncInsertWorker(AsyncResumableUploadData data, AsyncOperation asyncOp, SendOrPostCallback completionMethodDelegate) Value cannot be null. Parameter name: inputStream can not be null

   **My Code:** 

       newVideo.Tags.Add(new MediaCategory("Nonprofit", YouTubeNameTable.CategorySchema)); 
       newVideo.Description = DateTime.Now.ToShortDateString(); 
       newVideo.YouTubeEntry.Private = false; 
       ResumableUploader m_ResumableUploader = null; 
       Authenticator YouTubeAuthenticator; 

       m_ResumableUploader = new ResumableUploader(100); //chunksize 1 MB 
       m_ResumableUploader.AsyncOperationCompleted += new AsyncOperationCompletedEventHandler(m_ResumableUploader_AsyncOperationCompleted); 
       m_ResumableUploader.AsyncOperationProgress += new AsyncOperationProgressEventHandler(m_ResumableUploader_AsyncOperationProgress); 

       YouTubeAuthenticator = new ClientLoginAuthenticator(ConfigurationManager.AppSettings["APPLICATION_NAME"].ToString(), ServiceNames.YouTube, ConfigurationManager.AppSettings["LOGIN_USERNAME"].ToString(), ConfigurationManager.AppSettings["LOGIN_PASSWORD"].ToString()); 

       YouTubeAuthenticator.DeveloperKey = ConfigurationManager.AppSettings["DEVELOPER_KEY"].ToString(); 
       string contentType = MediaFileSource.GetContentTypeForFileName(fileName); 
       newVideo.MediaSource = new MediaFileSource(filePath, contentType); 

       AtomLink link = new AtomLink("http://uploads.gdata.youtube.com/resumable/feeds/api/users/" + ConfigurationManager.AppSettings["YT_USERNAME"].ToString() + "/uploads"); 
       link.Rel = ResumableUploader.CreateMediaRelation; 
       newVideo.YouTubeEntry.Links.Add(link); 

       m_ResumableUploader.InsertAsync(YouTubeAuthenticator, newVideo.YouTubeEntry, new object()); 

我想我應該補充。它打破了在最後一行ytr.ParseVideo(e.ResponseStream);

void m_ResumableUploader_AsyncOperationCompleted(object sender, AsyncOperationCompletedEventArgs e) 
     { 

       YouTubeRequestSettings ys = new YouTubeRequestSettings(ConfigurationManager.AppSettings["APPLICATION_NAME"].ToString(), 
        ConfigurationManager.AppSettings["DEVELOPER_KEY"].ToString(), ConfigurationManager.AppSettings["LOGIN_USERNAME"].ToString(), 
                ConfigurationManager.AppSettings["LOGIN_PASSWORD"].ToString()); 

       YouTubeRequest ytr = new YouTubeRequest(ys); 
       Video v = ytr.ParseVideo(e.ResponseStream); 
} 

回答

0

我的猜測是你沒有上載到你存儲的憑證所對應的賬戶。一般來說,你不應該在你的GData API v2代碼中做類似ConfigurationManager.AppSettings["YT_USERNAME"].ToString()的事情;相反,您應該使用值default作爲引用當前已通過身份驗證的用戶的URL的用戶部分。