googleapis / google-http-java-client

Google HTTP Client Library for Java
Apache License 2.0
1.39k stars 448 forks source link

"GET with non-zero content length is not supported" when sending GET request through Youtube Data API #1078

Closed ersatzhero closed 4 years ago

ersatzhero commented 4 years ago

Hello,

Used versions:

google-api-services-youtube: v3-rev20200618-1.30.9 google-api-client: 1.30.9 this dependency brings google-http-client: 1.34.0 but also tried using 1.36.0

I created a Spring Boot application, which should send a search to the Youtube API. The request fails with

java.lang.IllegalArgumentException: GET with non-zero content length is not supported at com.google.common.base.Preconditions.checkArgument(Preconditions.java:164) ~[guava-28.2-android.jar:na] at com.google.api.client.util.Preconditions.checkArgument(Preconditions.java:67) ~[google-http-client-1.34.0.jar:1.34.0] at com.google.api.client.http.javanet.NetHttpRequest.execute(NetHttpRequest.java:141) ~[google-http-client-1.34.0.jar:1.34.0] at com.google.api.client.http.javanet.NetHttpRequest.execute(NetHttpRequest.java:84) ~[google-http-client-1.34.0.jar:1.34.0] at com.google.api.client.http.HttpRequest.execute(HttpRequest.java:1012) ~[google-http-client-1.34.0.jar:1.34.0] at com.google.api.client.googleapis.services.AbstractGoogleClientRequest.executeUnparsed(AbstractGoogleClientRequest.java:542) ~[google-api-client-1.30.9.jar:1.30.9] at com.google.api.client.googleapis.services.AbstractGoogleClientRequest.executeUnparsed(AbstractGoogleClientRequest.java:475) ~[google-api-client-1.30.9.jar:1.30.9] at com.google.api.client.googleapis.services.AbstractGoogleClientRequest.execute(AbstractGoogleClientRequest.java:592) ~[google-api-client-1.30.9.jar:1.30.9]

Creating my Youtube client.

    @Bean
    HttpTransport httpTransport() throws GeneralSecurityException, IOException {
        return GoogleNetHttpTransport.newTrustedTransport();
    }

    @Bean
    JsonFactory jsonFactory() {
        return JacksonFactory.getDefaultInstance();
    }

    @Bean
    HttpRequestInitializer clientParametersAuthentication(@Value("${google.clientId}") String clientId,
                                                          @Value("${google.clientSecret}") String clientSecret) {
        return new ClientParametersAuthentication(clientId, clientSecret);
    }

    @Bean
    YouTube youtube(HttpTransport httpTransport,
                    JsonFactory jsonFactory,
                    HttpRequestInitializer httpRequestInitializer,
                    @Value("${google.applicationName}") String applicationName) {
        return new YouTube.Builder(httpTransport, jsonFactory, httpRequestInitializer)
                .setApplicationName(applicationName)
                .build();
    }

Executing search on Youtube API

    @GetMapping("/missingVideos")
    public Integer missingVideos() throws IOException {
        YouTube.Search.List list = youtubeClient.search()
                .list(Collections.emptyList())
                .setQ("channelId=" + channelToSearch);
        SearchListResponse execute = list.execute();
        return execute.getItems().size();
    }

The content lenght, that is getting checked, is set in class HttpRequest:938 to a default value of -1. With my request to the Youtube API, I just do a GET request, so there is no content to calculate. It stays at -1 and produces this error.

I would say it needs to be set to 0, in case of a GET request.

ersatzhero commented 4 years ago

Using ClientParametersAuthentication as the HttpRequestInitializer breaks everything. I guess that is not intended usage of that.