通过 reports.query() 请求 YouTubeAnalytics v2 API 抛出 400 错误

Request through reports.query() for YouTubeAnalytics v2 API throws 400 error

我正在尝试从 YouTubeAnalytics 的 V1 迁移到 V2 API。但是我不知道我应该用什么格式来陈述查询。

代码我正在尝试 运行:

return analytics.reports().query()
            .setIds(id)
            .setMetrics("views")
            .setDimensions("video")
            .execute();

但我收到错误代码 400,如下所示:

IOException: 400 Bad Request
com.google.api.client.googleapis.json.GoogleJsonResponseException: 400 Bad Request
{
  "code" : 400,
  "errors" : [ {
    "domain" : "global",
    "message" : "Could not parse content (CNHFwpxMq_TDnbpX_3GdjueOg) of field ids.",
    "reason" : "badRequest"
  } ],
  "message" : "Could not parse content (CNHFwpxMq_TDnbpX_3GdjueOg) of field ids."
}

有什么建议吗?

你有没有尝试过这样的事情:

    return analytics.reports()
        .query() // Metrics.
        .setIds(id)
        .setStartDate("2012-01-01")
        .setEndDate("2012-08-14")
        .setMetrics("views,subscribersGained,subscribersLost")
        .setDimensions("video")
        .setSort("-views")
        .setMaxResults(10)
        .execute();

您可以在此处查看这些方法: YouTube API v2

我传递 ID 的方式不正确。此外,API(v2) 不支持具有给定指标和维度集的此查询。正确的代码是:

return analytics.reports().query()
                .setIds("channel==" + id)
                .setStartDate("2018-11-29")
                .setEndDate("2018-12-01")
                .setMetrics("views")
                .setDimensions("day")
                .execute();

您可以在此处浏览支持的查询列表: https://developers.google.com/youtube/reporting/