如何从youtube API v3中根据videoId获取相关视频?

问题描述 投票:0回答:1

我正在从 YouTube API v3 开发 YouTube 克隆,而我需要根据当前播放视频的 videoId 来获取相关视频列表。

async function getApiData(videoId) {
    const res = await apiRequest('/search', {
        params: {
            part: 'snippet',
            videoCategoryId: videoId, // relatedToVideoId : videoId
            maximumResult: 10,
            type: 'video'   
        }
    })
    console.log("res", res)
    return res;
}

在参数中,我在不同的请求中传递了 videoCategoryId/latedToVideoId。 但它会发送错误消息。

根据 videoId 从 YouTube API 获取相关视频列表的正确方法是什么?

感谢您的支持

javascript reactjs youtube-api youtube-data-api
1个回答
0
投票

我认为要使用 YouTube API v3 根据给定的

videoId
检索相关视频,您应该使用
relatedToVideoId
参数而不是
videoCategoryId

videoCategoryId
参数用于按类别过滤视频,而不是用于检索相关视频。

我在下面添加示例代码:

async function getRelatedVideos(videoId) {
    const res = await apiRequest('/search', {
        params: {
            part: 'snippet',
            type: 'video',
            relatedToVideoId: videoId,
            maxResults: 10, // It's 'maxResults', not 'maximumResult'
        }
    });

    console.log("res", res);
    return res;
}
© www.soinside.com 2019 - 2024. All rights reserved.