You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何解决YouTube Analytics API获取频道最多评论视频的查询异常?

问题描述

  • 尝试通过YouTube Analytics API或YouTube Data API获取频道评论最多的视频时,查询*comments*指标的请求无法正常执行,但相同维度下查询*views*指标的请求可正常运行。
  • 尽管官方文档说明默认按升序排序,但查询*views*时必须指定降序(sort=-views),使用升序或移除sort参数均会返回400错误。

相关请求示例

无效的comments查询请求

curl --location --request GET 'https://youtubeanalytics.googleapis.com/v2/reports?ids=channel==MINE&dimensions=video&metrics=comments&maxResults=1&sort=-comments&startDate=2022-07-12&endDate=2022-09-12'

有效的views查询请求

curl --location --request GET 'https://youtubeanalytics.googleapis.com/v2/reports?ids=channel==MINE&dimensions=video&metrics=views&maxResults=1&sort=-views&startDate=2022-07-12&endDate=2022-09-12'

升序排序的错误响应

{    
    "error": {        
        "code": 400,        
        "message": "The query is not supported. Check the documentation at https://developers.google.com/youtube/analytics/v2/available_reports for a list of supported queries.",        
        "errors": [            
            {                
                "message": "The query is not supported. Check the documentation at https://developers.google.com/youtube/analytics/v2/available_reports for a list of supported queries.",                
                "domain": "global",                
                "reason": "badRequest"            
            }        
        ]    
    }
}

解决方案

1. 修复comments指标查询失败的问题

YouTube Analytics API中,video维度与comments指标的组合并未被官方支持,因此无法通过该API直接查询。建议改用YouTube Data API实现需求,步骤如下:

  • 调用channels.list接口,传入mine=true参数,获取当前频道的contentDetails.relatedPlaylists.uploads字段(即频道上传视频的专属播放列表ID)。
  • 调用playlistItems.list接口,传入上述播放列表ID,批量获取频道内所有视频的ID。
  • 调用videos.list接口,传入视频ID列表,指定part=statistics参数,获取每个视频的commentCount(评论数)字段,最后自行按commentCount降序排序,取排名第一的视频即可。

2. 修复排序异常的问题

这是API文档与实际执行逻辑不一致的问题,目前没有官方说明的解决方式,只能遵循实际运行要求:当需要获取指标最大值(如最多播放量、最多评论数)时,必须显式指定sort=-[指标名],不能省略sort参数或使用升序排序。

内容的提问来源于stack exchange,提问作者Erick M

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.19 06:30:49