The Google APIs Client Library for Java is a flexible, efficient, and powerful Java client library for accessing any HTTP-based API on the web, not just Google APIs.
npx @tessl/cli install tessl/maven-com-google-api-client--google-api-client@2.8.0The Google APIs Client Library for Java is a flexible, efficient, and powerful Java client library for accessing any HTTP-based API on the web, not just Google APIs. It provides OAuth 2.0 authentication, lightweight XML and JSON data models, protocol buffer support, and access to generated libraries for Google APIs.
pom.xml:<dependency>
<groupId>com.google.api-client</groupId>
<artifactId>google-api-client</artifactId>
<version>2.8.0</version>
</dependency>Or to your Gradle build.gradle:
implementation 'com.google.api-client:google-api-client:2.8.0'import com.google.api.client.googleapis.services.AbstractGoogleClient;
import com.google.api.client.googleapis.services.json.AbstractGoogleJsonClient;
import com.google.api.client.googleapis.auth.oauth2.GoogleCredential;
import com.google.api.client.googleapis.javanet.GoogleNetHttpTransport;
import com.google.api.client.json.JsonFactory;
import com.google.api.client.json.gson.GsonFactory;import com.google.api.client.googleapis.javanet.GoogleNetHttpTransport;
import com.google.api.client.googleapis.auth.oauth2.GoogleCredential;
import com.google.api.client.json.gson.GsonFactory;
import com.google.api.client.http.HttpTransport;
import com.google.api.client.json.JsonFactory;
import java.io.IOException;
import java.security.GeneralSecurityException;
public class GoogleApiExample {
private static final JsonFactory JSON_FACTORY = GsonFactory.getDefaultInstance();
public static void main(String[] args) throws IOException, GeneralSecurityException {
// Create HTTP transport
HttpTransport httpTransport = GoogleNetHttpTransport.newTrustedTransport();
// Load credentials (example with service account)
GoogleCredential credential = GoogleCredential.fromStream(
new FileInputStream("path/to/service-account-key.json"))
.createScoped(Arrays.asList("https://www.googleapis.com/auth/cloud-platform"));
// Use the transport and credentials with specific Google API clients
// (specific API clients would extend AbstractGoogleJsonClient)
}
}The Google API Client for Java is built around several key architectural components:
Core HTTP transport implementations with SSL/TLS support and mutual TLS capabilities for secure communication with Google APIs.
// Java.net transport
public static NetHttpTransport newTrustedTransport()
throws GeneralSecurityException, IOException;
// Apache HTTP transport
public static ApacheHttpTransport newTrustedTransport()
throws GeneralSecurityException, IOException;Complete OAuth 2.0 authentication system supporting various credential types including service accounts, user credentials, and compute engine metadata server authentication.
public class GoogleCredential extends Credential {
public static GoogleCredential fromStream(InputStream keyStream)
throws IOException;
public GoogleCredential createScoped(Collection<String> scopes);
}
public class GoogleAuthorizationCodeFlow extends AuthorizationCodeFlow {
public static class Builder extends AuthorizationCodeFlow.Builder {
public GoogleAuthorizationCodeFlow build();
}
}Abstract base classes and request handling framework for building type-safe Google API clients with automatic JSON parsing and error handling.
public abstract class AbstractGoogleClient {
protected AbstractGoogleClient(Builder builder);
public final HttpRequestFactory getRequestFactory();
public final String getApplicationName();
}
public abstract class AbstractGoogleJsonClient extends AbstractGoogleClient {
public final JsonFactory getJsonFactory();
public JsonObjectParser getObjectParser();
}Resumable upload and download functionality for large files with built-in progress tracking, error recovery, and configurable chunk sizes.
public final class MediaHttpUploader {
public MediaHttpUploader upload(GenericUrl initiationRequestUrl, HttpContent content);
public void setProgressListener(MediaHttpUploaderProgressListener progressListener);
public void setChunkSize(int chunkSize);
}
public final class MediaHttpDownloader {
public void download(GenericUrl requestUrl, OutputStream outputStream);
public void setProgressListener(MediaHttpDownloaderProgressListener progressListener);
}Efficient batch request processing to combine multiple API calls into a single HTTP request, reducing network overhead and improving performance.
public final class BatchRequest {
public BatchRequest queue(HttpRequest request, Class<T> dataClass,
Class<E> errorClass, BatchCallback<T, E> callback);
public void execute() throws IOException;
}
public interface BatchCallback<T, E> {
void onSuccess(T t, HttpHeaders responseHeaders) throws IOException;
void onFailure(E e, HttpHeaders responseHeaders) throws IOException;
}Structured JSON error response parsing and exception handling with detailed error information from Google API responses.
public class GoogleJsonResponseException extends HttpResponseException {
public GoogleJsonError getDetails();
public static GoogleJsonResponseException from(JsonFactory jsonFactory,
HttpResponse response);
}
public final class GoogleJsonError extends GenericJson {
public Integer getCode();
public String getMessage();
public List<ErrorInfo> getErrors();
}Core utility classes providing version information, certificate management, and common functionality used throughout the library.
public final class GoogleUtils {
public static final String VERSION;
public static final Integer MAJOR_VERSION;
public static final Integer MINOR_VERSION;
public static final Integer BUGFIX_VERSION;
public static synchronized KeyStore getCertificateTrustStore()
throws IOException, GeneralSecurityException;
}