0

这真让我抓狂!我正在使用下面的代码。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(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) 在 Google.GData.Client.ResumableUpload.ResumableUploader.AsyncInsertWorker(AsyncResumableUploadData data, AsyncOperation asyncOp, SendOrPostCallback completionMethodDelegate) 值不能为空。参数名称:inputStream 不能为空

                **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);
}
4

1 回答 1

0

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

于 2013-04-09T18:30:50.577 回答