Com.google.api.client.googleapis.json.GoogleJsonResponseException: 404 Not Found with YouTube Analytics API

YouTube Analytics API Java sample code doesn't work

There are three types of APIs that can be used on YouTube.

Sample code is open to the public, although the explanation of each API is omitted. When I tried to run the Java Analytics API sample in it, I got the following error, so I will summarize the solution for this. YouTubeAnalyticsReports.java

Result
Default Channel: ( xxxxxxxxxxxxxxxxxxx )

IOException: 404 Not Found
Not Found
com.google.api.client.googleapis.json.GoogleJsonResponseException: 404 Not Found
Not Found
at com.google.api.client.googleapis.json.GoogleJsonResponseException.from(GoogleJsonResponseException.java:146)
at com.google.api.client.googleapis.services.json.AbstractGoogleJsonClientRequest.newExceptionOnError(AbstractGoogleJsonClientRequest.java:113)
at com.google.api.client.googleapis.services.json.AbstractGoogleJsonClientRequest.newExceptionOnError(AbstractGoogleJsonClientRequest.java:40)
at com.google.api.client.googleapis.services.AbstractGoogleClientRequest$1.interceptResponse(AbstractGoogleClientRequest.java:321)
at com.google.api.client.http.HttpRequest.execute(HttpRequest.java:1056)
at com.google.api.client.googleapis.services.AbstractGoogleClientRequest.executeUnparsed(AbstractGoogleClientRequest.java:419)
at com.google.api.client.googleapis.services.AbstractGoogleClientRequest.executeUnparsed(AbstractGoogleClientRequest.java:352)
at com.google.api.client.googleapis.services.AbstractGoogleClientRequest.execute(AbstractGoogleClientRequest.java:469)
at com.google.api.services.samples.youtube.cmdline.analytics.YouTubeAnalyticsReports.executeViewsOverTimeQuery(YouTubeAnalyticsReports.java:134)
at com.google.api.services.samples.youtube.cmdline.analytics.YouTubeAnalyticsReports.main(YouTubeAnalyticsReports.java:103)

Solution

Set the API version to V2. If you get the above error even though you have set the API scope and client_secrets.json from Google Console, the API version is old.

In the sample code, Analytics API V1 is used, but for some reason V1 cannot be used, and it worked when I changed it to V2, so I use V2. I tried to put out a pull request on google. https://github.com/youtube/api-samples/pull/292 pom.xml Modify pom.xml as follows. Line:14

- <project.youtube.version>v3-rev182-1.22.0</project.youtube.version>
- <project.youtube.analytics.version>v1-rev63-1.22.0</project.youtube.analytics.version>
- <project.youtube.reporting.version>v1-rev10-1.22.0</project.youtube.reporting.version>
- <project.http.version>1.20.0</project.http.version>
- <project.oauth.version>1.20.0</project.oauth.version>
+ <project.youtube.version>v3-rev222-1.25.0</project.youtube.version>
+ <project.youtube.analytics.version>v2-rev272-1.25.0</project.youtube.analytics.version>
+ <project.youtube.reporting.version>v1-rev748-1.25.0</project.youtube.reporting.version>
+ <project.http.version>1.36.0</project.http.version>
+ <project.oauth.version>1.31.0</project.oauth.version>

YouTubeAnalyticsReports.java Modify YouTubeAnalyticsReports.java as follows Line:12

- import com.google.api.services.youtubeAnalytics.YouTubeAnalytics;
- import com.google.api.services.youtubeAnalytics.model.ResultTable;
- import com.google.api.services.youtubeAnalytics.model.ResultTable.ColumnHeaders;
+ import com.google.api.services.youtubeAnalytics.v2.YouTubeAnalytics;
+ import com.google.api.services.youtubeAnalytics.v2.model.QueryResponse;
+ import com.google.api.services.youtubeAnalytics.v2.model.ResultTableColumnHeader;

Line:124

- private static ResultTable executeViewsOverTimeQuery(YouTubeAnalytics analytics,
                                                         String id) throws IOException {
+ private static QueryResponse executeViewsOverTimeQuery(YouTubeAnalytics analytics,
                                                           String id) throws IOException {

Line:128

- .query("channel==" + id,     // channel id
-         "2012-01-01",         // Start date.
-         "2012-01-14",         // End date.
-         "views,uniques")      // Metrics.
+ .query()
+ .setIds("channel==" + id)
+ .setMetrics("views")
+ .setStartDate("2020-01-01")
+ .setEndDate("2020-12-31")

Line:145

- private static ResultTable executeTopVideosQuery(YouTubeAnalytics analytics,
+ private static QueryResponse executeTopVideosQuery(YouTubeAnalytics analytics,

Line:149

- .query("channel==" + id,                          // channel id
-         "2012-01-01",                              // Start date.
-         "2012-08-14",                              // End date.
-         "views,subscribersGained,subscribersLost") // Metrics.
+ .query()
+ .setIds("channel==" + id)
+ .setMetrics("views,subscribersGained,subscribersLost")
+ .setStartDate("2019-01-01")
+ .setEndDate("2020-12-31")

Line:167

- private static ResultTable executeDemographicsQuery(YouTubeAnalytics analytics,
+ private static QueryResponse executeDemographicsQuery(YouTubeAnalytics analytics,

Line:170

- .query("channel==" + id,     // channel id
-         "2007-01-01",         // Start date.
-         "2012-08-14",         // End date.
-         "viewerPercentage")   // Metrics.
+ .query()
+ .setIds("channel==" + id)
+ .setMetrics("viewerPercentage")
+ .setStartDate("2020-01-01")
+ .setEndDate("2020-12-31")

Line:187

- private static void printData(PrintStream writer, String title, ResultTable results) {
+ private static void printData(PrintStream writer, String title, QueryResponse results) {

Line:194

- for (ColumnHeaders header : results.getColumnHeaders()) {
+ for (ResultTableColumnHeader header : results.getColumnHeaders()) {

Line:202

- ColumnHeaders header = results.getColumnHeaders().get(colNum);
+ ResultTableColumnHeader header = results.getColumnHeaders().get(colNum);

Now you can get it by running it.

Recommended Posts

Com.google.api.client.googleapis.json.GoogleJsonResponseException: 404 Not Found with YouTube Analytics API
Compatible with Android 10 (API 29)