如何在不使用搜索请求的情况下通过频道 ID 获取频道的视频

jul*_*ier 5 youtube-api node.js

在我的 NodeJS 应用程序中,我使用此代码获取特定频道的视频:

 var myOauth = 'my oauth object';
 var channelId = 'my channel id';
 youtube.search.list({ auth: myOauth, part: 'snippet', 
                       channelId: channelId, type:'video',
                       order:'date', maxResults:50 
                     }, 
                     function(err, response) {
                       //do something here
                     }
 );
Run Code Online (Sandbox Code Playgroud)

此解决方案有效,但每个请求的配额成本为 100。https ://developers.google.com/youtube/v3/docs/search/list

我想通过其他方式获取视频,例如“playlistItems”,其中配额成本为 1。https ://developers.google.com/youtube/v3/docs/playlistItems/list

谢谢!

编辑:找到解决方案

我找到了一种新方法,只需 3 个配额成本即可通过频道 ID 获取特定频道的视频。

获取通过 Oauth 认证的用户订阅列表:

   youtube.subscriptions.list({
        auth: oauth, part: 'snippet,contentDetails', 
        mine:true, maxResults:50, 
        pageToken:pageToken }, 
        function(err, response) {
            if(!err) {
                var firstChannelId = response.item[0].snippet.resourceId.channelId;
                console.log(firstChannelId);
            }
        }
    );
Run Code Online (Sandbox Code Playgroud)

从频道 ID 中获取频道播放列表 ID:

   youtube.channels.list({    auth: res.oauth, part: 'contentDetails', 
            id:firstChannelId, maxResults:50 }, 
            function(err, response) {
                var channelPlaylistId = response.item[0].contentDetails.relatedPlaylists.uploads;
                console.log(channelPlaylistId);
            }
        );
Run Code Online (Sandbox Code Playgroud)

从播放列表浏览项目:

   youtube.playlistItems.list({    auth: res.oauth, part: 'snippet', 
        playlistId:channelPlaylistId,
        maxResults:50, pageToken:pageToken }, 
        function(err, response) {
            console.log(JSON.stringify(response.items));
        }
    );
Run Code Online (Sandbox Code Playgroud)

sta*_*all 0

解决方案来自@julien-dumortier的问题帖子。

我找到了一种新方法,可以通过频道 ID 获取特定频道的视频,而只需 3 个配额成本。

获取Oauth认证用户的订阅列表:

youtube.subscriptions.list({
    auth: oauth, part: 'snippet,contentDetails', 
    mine:true, maxResults:50, 
    pageToken:pageToken }, 
    function(err, response) {
        if(!err) {
            var firstChannelId = response.item[0].snippet.resourceId.channelId;
            console.log(firstChannelId);
        }
    }
);
Run Code Online (Sandbox Code Playgroud)

从频道ID获取频道播放列表ID:

youtube.channels.list({    auth: res.oauth, part: 'contentDetails', 
    id:firstChannelId, maxResults:50 }, 
    function(err, response) {
        var channelPlaylistId = response.item[0].contentDetails.relatedPlaylists.uploads;
        console.log(channelPlaylistId);
    }
);
Run Code Online (Sandbox Code Playgroud)

浏览播放列表中的项目:

youtube.playlistItems.list({    auth: res.oauth, part: 'snippet', 
    playlistId:channelPlaylistId,
    maxResults:50, pageToken:pageToken }, 
    function(err, response) {
        console.log(JSON.stringify(response.items));
    }
);
Run Code Online (Sandbox Code Playgroud)