使用可恢复的C#代码上传YouTube的问题

问题描述:

当我尝试使用C#中的代码使用可恢复的上传将视频上传到您的管道。 我收到一条错误消息“远程服务器返回错误:(410)已经过期。”。使用可恢复的C#代码上传YouTube的问题

以下是我们用于使用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的可恢复上载将成功执行视频上传。

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

使用谷歌.NET客户端库

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

使用的oauth2与YouTube API第3版。

/// <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

+0

撕开你就登录版本的建议后,我试图从版本2迁移到版本3得到了clientID的,API ID,我的SecretID帐户。将代码添加到代码中。但得到错误 400.这是一个错误。 错误:redirect_uri_mismatch 应用程序:YouTubeUpload 您可以通过电子邮件发送此应用程序的开发人员:[email protected] 请求中的重定向URI:http:// localhost:2340/authorize /与注册的重定向URI不匹配。 –

+0

在调试本地主机时将uri设置为localhost/authorize,或者使用本地主机而不是web应用程序。从最后的评论中删除您的客户端ID。你需要保持安全。 – DaImTo

+0

http://www.daimto.com/google-developer-console-oauth2/可能会帮助您 – DaImTo