使用可恢复的c#代码进行youtube上传问题

时间:2015-09-04 09:36:38

标签: c# youtube youtube-api google-oauth google-client-login

当我尝试使用C#中的代码使用可恢复上传将视频上传到您的视频时。 我收到错误消息"远程服务器返回错误:(410)消失。"。

以下是我们用于使用C#上传视频的代码。

var credentials = new GDataCredentials(_account, _password);

_authenticator = new ClientLoginAuthenticator("YouTubeUploader", ServiceNames.YouTube, credentials){DeveloperKey = _developerKey};
_resumableUploader = new ResumableUploader();

_resumableUploader.AsyncOperationCompleted += new AsyncOperationCompletedEventHandler(UlAsyncOperationCompleted);
_resumableUploader.AsyncOperationProgress += new AsyncOperationProgressEventHandler(UlAsyncOperationProgress);

var v = new Video
                        {
                            Title = "some title",
                            Description = "some description",
                            Keywords = "list of keywords separated by comma",
                            Private = false
                        };
            v.Tags.Add(new MediaCategory("Entertainment"));

            string contentType = MediaFileSource.GetContentTypeForFileName("Video file name");
            v.MediaSource = new MediaFileSource("video file name", contentType);

            // add the upload uri to it
            var link =
                new AtomLink("http://uploads.gdata.youtube.com/resumable/feeds/api/users/" + Account + "/uploads")
                    {Rel = ResumableUploader.CreateMediaRelation};

            v.YouTubeEntry.Links.Add(link);
              XmlExtension unlistedAccessControlXmlExtension = CreateAccessControlXmlExtension("list", "denied");
            XmlExtension noCommentAccessControlXmlExtension = CreateAccessControlXmlExtension("comment", "denied");
            XmlExtension rat1 = CreateAccessControlXmlExtension("commentVote", "denied");
            XmlExtension rat2 = CreateAccessControlXmlExtension("videoRespond", "denied");
            XmlExtension rat3 = CreateAccessControlXmlExtension("rate", "denied");

            v.YouTubeEntry.ExtensionElements.Add(rat1);
            v.YouTubeEntry.ExtensionElements.Add(rat2);
            v.YouTubeEntry.ExtensionElements.Add(rat3);

            v.YouTubeEntry.ExtensionElements.Add(unlistedAccessControlXmlExtension);
            v.YouTubeEntry.ExtensionElements.Add(noCommentAccessControlXmlExtension);

            var info = new YouTubeFileInfo(desc.Id) { Status = YouTubeFileInfo.YouTubeFileStatus.Uploading, FileName = fileName };

            _resumableUploader.InsertAsync(_authenticator, v.YouTubeEntry, data);

我认为我们正面临这个问题,因为上传网址版本2更改为版本3。 我怀疑我们不应再使用" http://uploads.gdata.youtube.com/resumable/feeds/api/users/" +帐户+" /上传"

是否可以帮助我提供有关如何更改代码或第3版的新URL的信息。 因此,使用版本3的可恢复上传,视频上传将成功。

1 个答案:

答案 0 :(得分:0)

您遇到的问题是您尝试使用不再有效的登录名和密码登录。您需要将其更改为OAuth2,我还建议您升级到当前版本的YouTube API。

使用Google .net客户端库

PM> Install-Package Google.Apis.YouTube.v3

在YouTube API v3中使用Oauth2。

/// <summary>
        /// Authenticate to Google Using Oauth2
        /// Documentation https://developers.google.com/accounts/docs/OAuth2
        /// </summary>
        /// <param name="clientId">From Google Developer console https://console.developers.google.com</param>
        /// <param name="clientSecret">From Google Developer console https://console.developers.google.com</param>
        /// <param name="userName">A string used to identify a user.</param>
        /// <returns></returns>
        public static YouTubeService AuthenticateOauth(string clientId, string clientSecret, string userName)
        {

            string[] scopes = new string[] { YouTubeService.Scope.Youtube,  // view and manage your YouTube account
                                             YouTubeService.Scope.YoutubeForceSsl,
                                             YouTubeService.Scope.Youtubepartner,
                                             YouTubeService.Scope.YoutubepartnerChannelAudit,
                                             YouTubeService.Scope.YoutubeReadonly,
                                             YouTubeService.Scope.YoutubeUpload};

            try
            {
                // here is where we Request the user to give us access, or use the Refresh Token that was previously stored in %AppData%
                UserCredential credential = GoogleWebAuthorizationBroker.AuthorizeAsync(new ClientSecrets { ClientId = clientId, ClientSecret = clientSecret }
                                                                                             , scopes
                                                                                             , userName
                                                                                             , CancellationToken.None
                                                                                             , new FileDataStore("Daimto.YouTube.Auth.Store")).Result;

                YouTubeService service = new YouTubeService(new YouTubeService.Initializer()
                {
                    HttpClientInitializer = credential,
                    ApplicationName = "YouTube Data API Sample",
                });
                return service;
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.InnerException);
                return null;

            }

        }

代码摘自youtube sample project