将视频上传到特定频道/Youtube

本文关键字:频道 Youtube 视频 | 更新日期: 2023-09-27 18:30:11

我正在尝试将视频上传到Youtube。已成功将视频上载到帐户的频道。之后,我创建了一个名为"Deneme1"的新频道。我试着用api上传到那个频道;但已上载到main。

我的代码:

    public static string UploadVideo(string FilePath, string Title, string Description)
    {
        YouTubeRequestSettings settings;
        YouTubeRequest request;
        string devkey = "api key";            
        string username = "mail@gmail.com";
        string password = "password";
        settings = new YouTubeRequestSettings("Deneme1", devkey, username, password) { Timeout = 10000000 };
        request = new YouTubeRequest(settings);
        Video newVideo = new Video();
        newVideo.Title = Title;
        newVideo.Description = Description;
        newVideo.Private = true;
        newVideo.YouTubeEntry.Private = false;
        newVideo.Keywords = "asd";
        newVideo.Tags.Add(new MediaCategory("Sports", YouTubeNameTable.CategorySchema));
        newVideo.YouTubeEntry.MediaSource = new MediaFileSource(FilePath, "video/flv");
        Video createdVideo = request.Upload(newVideo);
        return createdVideo.VideoId;
    }
    protected void Page_Load(object sender, EventArgs e)
    {
         try
        {
            string videopath, videotitle, videodesc;
            videopath = @"C:'Users'Ercin'Dropbox'Cloudy'Visual Studio'Projects'videoupload'videoupload'badstart.flv";
            videotitle = "test title";
            videodesc = "test description";
            UploadVideo(videopath, videotitle, videodesc);
        }
        catch (Exception exception)
        {
            Response.Write("Upload failed: " + exception.Message);
        }

任何帮助都将是美妙的!

将视频上传到特定频道/Youtube

下面的代码对我来说很好,可以上传到我用用户名输入的ChannelId的特定频道。

public static Google.Apis.YouTube.v3.YouTubeService AuthenticateOaut(string clientId, string clientSecret, string userName)
        {
            string[] scopes = new string[] { Google.Apis.YouTube.v3.YouTubeService.Scope.Youtube,  // view and manage your YouTube account
                                             Google.Apis.YouTube.v3.YouTubeService.Scope.YoutubeForceSsl,
                                             Google.Apis.YouTube.v3.YouTubeService.Scope.Youtubepartner,
                                             Google.Apis.YouTube.v3.YouTubeService.Scope.YoutubepartnerChannelAudit,
                                             Google.Apis.YouTube.v3.YouTubeService.Scope.YoutubeReadonly,
                                             Google.Apis.YouTube.v3.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;
                Google.Apis.YouTube.v3.YouTubeService service = new Google.Apis.YouTube.v3.YouTubeService(new Google.Apis.YouTube.v3.YouTubeService.Initializer()
                {
                    HttpClientInitializer = credential,
                    ApplicationName = "Web client 1",
                });
                return service;
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.InnerException);
                return null;
            }
        }

从头开始(这对一些人有帮助),我会告诉你我做了什么:

  • 您将获得授权凭据(我使用过OAuth2.0):https://developers.google.com/youtube/registering_an_application#Create_OAuth2_Tokens
  • 您使用YouTube v3 API(有一个nuget包)
  • 你写的代码和thiago.adriano26的答案差不多
  • 当你第一次运行你的应用程序时,它会打开浏览器让你验证并选择你想要的频道(我不确定谷歌为什么这么做,因为你已经在代码中指定了用户ID,但他们还是这么做了…)
  • 完成此步骤后,将在以下位置生成一个令牌:C:'Users'User'AppData'Roaming'Google.Apis.Auth'Google.Apis.A‌​uth.OAuth2.Responses‌​.TokenResponse-<UserId>。这似乎将所使用的UserId链接到实际信道。只要这一点存在,两者之间的联系就会存在。删除它,或使用另一个UserId(每个通道都有自己的UserIdChannelId,请参阅此处:https://support.google.com/youtube/answer/3250431?hl=en)将允许您从浏览器中选择一个新频道