Authentication { public static HttpResponse makeGetRequest(String serviceUrl) throws IOException { GoogleCredentials credentials = GoogleCredentials.getApplicationDefault(); if (!(credentials instanceof IdTokenProvider)) { throw new IllegalArgumentException("Credentials are not an instance of IdTokenProvider."); } IdTokenCredentials tokenCredential = IdTokenCredentials.newBuilder() .setIdTokenProvider((IdTokenProvider) credentials) .setTargetAudience(serviceUrl) .build(); GenericUrl genericUrl = new GenericUrl(serviceUrl); HttpCredentialsAdapter adapter = new HttpCredentialsAdapter(tokenCredential); HttpTransport transport = new NetHttpTransport(); HttpRequest request = transport.createRequestFactory(adapter).buildGetRequest(genericUrl); return request.execute(); }  static HttpResponse makeGetRequest(String serviceUrl);  }
@Test public void canMakeGetRequest() throws IOException { String url = "https: HttpResponse response = Authentication.makeGetRequest(url); assertThat(response.parseAsString(), containsString("Example Domain")); assertThat(response.getContentType(), containsString("text/html")); assertThat(response.getStatusCode(), equalTo(200)); }
@Test public void failsMakeGetRequestWithoutProtocol() throws IOException { String url = "example.com/"; try { Authentication.makeGetRequest(url); } catch (IllegalArgumentException e) { assertThat(e.getMessage(), containsString("no protocol")); } }