在将视频上传到youtube时,我如何知道哪个类别ID是什么类别名称?

时间:2015-06-16 04:46:29

标签: c# youtube youtube-api

我正在将视频上传到您的视频: 在form1构造函数中:

UserCredential credential;
            using (FileStream stream = new FileStream(@"D:\C-Sharp\Youtube-Manager\Youtube-Manager\Youtube-Manager\bin\Debug\client_secrets.json", FileMode.Open, FileAccess.Read))
            {
                credential = GoogleWebAuthorizationBroker.AuthorizeAsync(
                    GoogleClientSecrets.Load(stream).Secrets,
                    new[] { YouTubeService.Scope.Youtube, YouTubeService.Scope.YoutubeUpload },
                    "user",
                    CancellationToken.None,
                    new FileDataStore("YouTube.Auth.Store")).Result;
            }
            var youtubeService = new YouTubeService(new BaseClientService.Initializer()
            {
                HttpClientInitializer = credential,
                ApplicationName = Assembly.GetExecutingAssembly().GetName().Name
            });
            var video = new Video();
            video.Snippet = new VideoSnippet();
            video.Snippet.Title = "Default Video Title";
            video.Snippet.Description = "Default Video Description";
            video.Snippet.Tags = new string[] { "tag1", "tag2" };
            video.Snippet.CategoryId = "22"; // See https://developers.google.com/youtube/v3/docs/videoCategories/list
            video.Status = new VideoStatus();
            video.Status.PrivacyStatus = "public";
            var filePath = @"C:\Users\bout0_000\Videos\test.mp4";
            using (var fileStream = new FileStream(filePath, FileMode.Open))
            {

                const int KB = 0x400;
                var minimumChunkSize = 256 * KB;

                var videosInsertRequest = youtubeService.Videos.Insert(video,
                    "snippet,status", fileStream, "video/*");
                videosInsertRequest.ProgressChanged +=
                    videosInsertRequest_ProgressChanged;
                videosInsertRequest.ResponseReceived +=
                    videosInsertRequest_ResponseReceived;
                videosInsertRequest.ChunkSize = minimumChunkSize * 4;
                videosInsertRequest.Upload();
            }

此示例中我使用的是类别编号22

video.Snippet.CategoryId = "22";

只有在上传了我在视频中的YouTube视频中看到的视频后,此类别才是:人物&博客

如果我正在浏览此链接https://developers.google.com/youtube/v3/docs/videoCategories/list,我可以使用底部的OAuth 2.0进行授权请求。

但是我仍然不明白我在哪里可以看到所有类别的名单和id的完整列表?例如:姓名:People&博客ID:22

找不到任何显示它的网站。

2 个答案:

答案 0 :(得分:3)

进行API调用:

https://www.googleapis.com/youtube/v3/videoCategories?part=snippet&regionCode={two-character-region}&key={YOUR_API_KEY}

API将返回您选择的区域的所有类别(名称和ID)。请注意,相同类别应在所有区域中具有相同的ID;但是,有些类别在某些地区不可用(这就是为什么你必须进行API调用......有太多的区域以友好的方式列出所有可能的排列)。

答案 1 :(得分:0)

以下是我程序中的VB .NET源代码。此代码加载一个ComboBox,其自定义类(CategoryClass)包含所有有效的CategoryIds和标题。我还包括我的自定义类:CategoryClass。您可以使用其中一个免费转换器将其转换为C#.NET。

Private Sub GetVideoCategories()
    Dim objYouTubeService As YouTubeService
    AddToLog("GetVideoCategories Begin", True, False)
    Try
        objYouTubeService = New YouTubeService(New BaseClientService.Initializer() With { _
             .HttpClientInitializer = OAUth2Credential, _
             .ApplicationName = Assembly.GetExecutingAssembly().GetName().Name})
    Catch ex As Exception
        MsgBox(ex.Message, MsgBoxStyle.Critical, "GetVideoCategories - Initialize YouTubeService")
        End
    End Try
    Dim objCategories As VideoCategoryListResponse = Nothing
    Try
        Dim objRequest As VideoCategoriesResource.ListRequest = New VideoCategoriesResource.ListRequest(objYouTubeService, "id,snippet")
        objRequest.Hl = "en_US"
        objRequest.RegionCode = "US"
        objCategories = objRequest.Execute
    Catch ex As Exception
        MsgBox(ex.Message, MsgBoxStyle.Critical, "GetVideoCategories - VideoCategories List Request")
        End
    End Try
    cmbCategory.DisplayMember = "Title"
    cmbCategory.ValueMember = "Id"
    For Each obj As VideoCategory In objCategories.Items
        cmbCategory.Items.Add(New CategoryClass(obj.Id, obj.Snippet.Title))
        If obj.Snippet.Title.Contains("News") Then
            intDefaultCategoryIndex = cmbCategory.Items.Count - 1
        End If
    Next
    cmbCategory.SelectedIndex = intDefaultCategoryIndex
    AddToLog("GetVideoCategories End", True, False)
End Sub




Friend Class CategoryClass
Dim m_Id As String
Dim m_Title As String
Sub New(ByVal Id As String, ByVal Title As String)
    m_Id = Id
    m_Title = Title
End Sub
Property ID As String
    Get
        ID = m_Id
    End Get
    Set(value As String)
        m_Id = value
    End Set
End Property
Property Title As String
    Get
        Title = m_Title
    End Get
    Set(value As String)
        m_Title = value
    End Set
End Property
Overrides Function ToString() As String
    ToString = m_Id & "|" & m_Title
End Function
End Class