【问题标题】:Upload video to specific channel / Youtube将视频上传到特定频道/Youtube
【发布时间】:2015-01-10 11:44: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);
        }

任何帮助都会很棒!

【问题讨论】:

    标签: c# youtube-api youtube-data-api youtube.net-api


    【解决方案1】:

    下面的代码对我来说很好,可以上传到我在用户名中放入 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;
    
                }
    
            }
    

    【讨论】:

    • 如何调用此方法,请您提供示例以将 vdo 上传到 youtube 频道?
    【解决方案2】:

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

    • 你得到你的授权凭证(我用过OAuth2.0):https://developers.google.com/youtube/registering_an_application#Create_OAuth2_Tokens
    • 您使用 YouTube v3 API(有一个 nuget 包)。
    • 您编写的代码与thiago.adriano26 在他的回答中几乎相同。
    • 第一次运行您的应用程序时,它会打开浏览器供您验证并选择您想要的频道(我不确定 Google 为什么这样做,因为您已经在代码中指定了用户 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)将允许您从浏览器中选择一个新频道。

    【讨论】:

      猜你喜欢
      • 2013-02-03
      • 2017-03-18
      • 2017-08-08
      • 1970-01-01
      • 2014-11-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多