How do i know what category id is what category na

2019-09-19 23:21发布

I'm uploading a video to yourube: In form1 constructor:

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();
            }

Here in this example i'm using category number 22

video.Snippet.CategoryId = "22";

Only after uploaded the video i see in youtube site in my video that this category is: People & Blogs

If i'm browsing to this link https://developers.google.com/youtube/v3/docs/videoCategories/list i can play there with the Authorize requests using OAuth 2.0 on the bottom.

But i still don't understand where i can see a full list of all categories by names and by id's ? For example: Name: People & Blogs Id: 22

Couldn't find any site that show it.

2条回答
Rolldiameter
2楼-- · 2019-09-19 23:49

Below is VB .NET source code from my program. This code loads a ComboBox with a Custom Class (CategoryClass) that includes all of the valid CategoryIds and Titles. I also included my Custom Class: CategoryClass. You can use one of the free converters to convert this to 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
查看更多
地球回转人心会变
3楼-- · 2019-09-19 23:50

Make an API call to:

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

And the API will return all categories (names and IDs) for the region you selected. Note that the same category should have the same ID across all regions; however, some categories are not available in certain regions (which is why you must do the API call ... there are too many regions to list all the possible permutations in a friendly way).

查看更多
登录 后发表回答