1

Iam trying to migrate from V1 to V2 of the YouTubeAnalytics API. But I cannot figure out the format in which I should state the query.

Code Iam trying to run:

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

But I get error code 400 like below:

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."
}

Any suggestions?

Carol
  • 347
  • 5
  • 17

2 Answers2

1

Have you tried something like this:

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

You can check out those methods here: YouTube API v2

Willy
  • 23
  • 1
  • 5
  • Thanks Willy, I still get a 400 error code but the message is different. Have updated my question to reflect this. – Carol Dec 01 '18 at 05:08
1

I was passing the id in an incorrect manner. Also the API(v2) doesn't support this query with the given set of metrics and dimensions. The correct code is:

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

You can browse through the supported list of queries here: https://developers.google.com/youtube/reporting/

Carol
  • 347
  • 5
  • 17
  • Life saver, thanks a ton! It wasn't clear that start and end dates are necessary – ruX Mar 10 '19 at 01:11