2013-04-04 248 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的用户部分。