YouTube API - 视频上传 - 401未经授权

时间:2013-04-04 12:37:57

标签: c# youtube-api

这让我抓狂!我在下面使用以下代码。 90%的时间我收到以下错误。它时不时地工作没有问题。请帮忙。我检查了开发人员密钥,用户名和密码。

  

错误:System.Net.WebException:远程服务器返回错误:   (401)未经授权。在System.Net.HttpWebRequest.GetResponse()
  在   Google.GData.Client.ResumableUpload.ResumableUploader.UploadStreamPart(的Int32   partIndex,String httpMethod,Uri sessionUri,Authenticator   authentication,Stream payload,String mediaType,AsyncData data)
  在   Google.GData.Client.ResumableUpload.ResumableUploader.UploadStream(字符串   httpMethod,Uri sessionUri,Authenticator身份验证,Stream   payload,String mediaType,AsyncData data)at   Google.GData.Client.ResumableUpload.ResumableUploader.Insert(验证   身份验证,AbstractEntry有效负载,AsyncData数据)   Google.GData.Client.ResumableUpload.ResumableUploader.AsyncInsertWorker(AsyncResumableUploadData   数据,AsyncOperation asyncOp,Sen​​dOrPostCallback   completionMethodDelegate)值不能为null。参数名称:   inputStream不能为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);
}

1 个答案:

答案 0 :(得分:0)

我的猜测是您没有上传到与您拥有的存储凭据相对应的帐户。通常,您不应该在GData API v2代码中执行ConfigurationManager.AppSettings["YT_USERNAME"].ToString()之类的操作;相反,您应该使用值default作为URL的用户部分,该部分引用当前经过身份验证的用户。