Google APIs Client Library for Java

repository·main·Indexed 23 days ago

https://github.com/googleapis/google-api-java-client

A flexible Java client library for accessing HTTP-based APIs, featuring specialized support for Google APIs. It provides a consistent OAuth 2.0 library, lightweight XML and JSON data models, protocol buffer support, and generated libraries for Google APIs. Compatible with Java 7+, Android 1.6+, and Google App Engine. Includes modular extensions for GSON, Jackson2, and platform-specific integrations for Android, Servlets, and App Engine.

Tokens
13.4K
Snippets
26
Records
57
Agent score
80%

What's inside google-api-java-client

  1. Overview of the Google APIs Client Library for Java

    main

    The Google APIs Client Library for Java is a flexible and efficient client for accessing any HTTP-based API, including Google APIs.

    Key features include:

    • A consistent OAuth 2.0 library.
    • Lightweight XML and JSON data models supporting any schema.
    • Support for protocol buffers.
    • Access to a wide range of generated libraries for Google APIs.

    Supported Environments:

    • Java 7 or higher
    • Android 1.6 or higher
    • Google App Engine
  2. How resumable vs direct media download works

    main

    The library supports two modes for downloading media:

    1. Resumable Media Download (Default): Downloads the file in configurable chunks. This is ideal for large files as it is more resilient to connection failures and allows for retries if a server error is encountered.
    2. Direct Media Download: Downloads the entire media content in one single HTTP request. This reduces the number of requests but increases the risk of failure for large files due to potential connection interruptions.
  3. Persist OAuth 2.0 credentials using DataStoreFactory

    main

    Access tokens typically expire after 1 hour. GoogleCredentials handles automatic refreshing if a long-lived refresh token is available (obtained via access_type=offline during the authorization flow). To maintain these tokens across application restarts, you must persist them using a DataStoreFactory.

    Available implementations include:

    • AppEngineDataStoreFactory: Persists credentials using the Google App Engine Data Store API.
    • FileDataStoreFactory: Persists credentials in a file.
    • MemoryDataStoreFactory: Persists credentials in memory (volatile; only useful for the lifetime of the process).

    You can also provide a custom implementation of DataStoreFactory to use your own storage mechanism.

  4. Auth helpers for web apps on Google App Engine

    main

    If you are building a web application that interacts with user data via OAuth 2.0-enabled APIs, the library provides helpers to:

    • Simplify obtaining access tokens using AuthorizationCodeFlow.
    • Manage tokens after acquisition by marking them as PersistenceCapable (e.g., using Google Cloud Datastore).
    • Simplify making authenticated calls using the access token's Credential.
    • Insulate servlet logic from authentication implementation details.
  5. Understanding @Beta and Deprecated features

    main

    When using the library, be aware of the following stability markers:

    @Beta Features

    Features annotated with @Beta at the class or method level are subject to change or removal in any major release. Warning: Do not use @Beta features if you are developing a library intended for use by others (i.e., code that will reside on the CLASSPATH of external users).

    Deprecated Features

    Non-beta features marked as deprecated will be removed eighteen months after the release in which they were first deprecated. You must update your code within this window to avoid breakage, as removal may not always result in a compilation error.

  6. Select the correct Google API Client module for your environment

    main

    The Google API Client Library for Java is modular. You should choose the module that matches your target platform or preferred data format to ensure compatibility and optimal performance.

    Core Module

    • google-api-client: The base library designed for compatibility across all supported Java platforms, including Android.

    Platform Extensions

    • Android: Use google-api-client-android for Java Google Android applications (requires SDK >= 2.1). It depends on google-api-client and google-http-client-android.
    • Servlet/JDO: Use google-api-client-servlet for Java servlet web applications. It depends on google-api-client and google-oauth-client-servlet.
    • App Engine: Use google-api-client-appengine for Google App Engine applications. It depends on google-api-client, google-api-client-servlet, google-oauth-client-appengine, and google-http-client-appengine.

    Data Format Extensions

    If you require specific serialization or transport formats, use these modules:

    • GSON: google-api-client-gson (depends on google-api-client and google-http-client-gson).
    • Jackson2: google-api-client-jackson2 (depends on google-api-client and google-http-client-jackson2).
    • Protocol Buffers: google-api-client-protobuf (depends on google-api-client and google-http-client-protobuf).
    • XML: google-api-client-xml (depends on google-api-client and google-http-client-xml).
  7. Understand @Beta and Deprecation policies

    main

    The library follows specific stability guidelines:

    • @Beta: Features marked with @Beta are subject to change or removal in any major release. Avoid using beta features if you are building a library intended for use by third parties (i.e., code that will reside on the CLASSPATH of users outside your control).
    • Deprecations: Non-beta features marked as deprecated will be removed eighteen months after the release in which they were first deprecated. You must update your code before this period ends to avoid potential breakage.
  8. Getting started with Google App Engine and the Java Client Library

    main

    To set up a Google App Engine application using the Java Client Library, follow these steps:

    1. Install the library: Either download the JAR files and place them in your war/WEB-INF/lib directory, or use Maven.
    2. Understand OAuth 2.0 flows: Learn about the authorization code flow for standard applications or the Google App Engine Identity API for App Engine-specific authentication.
    3. Review samples: Examine the Calendar App Engine sample to see how the library and auth helpers work together to access end-user data in a web app environment.
  9. Access Google APIs on Android using helper classes

    main

    When developing for Android, if the API you need is available in the [Google Play Services library], use that for optimal performance. For all other Google APIs, use the Google APIs Client Library for Java's Android-specific helper classes. These classes integrate with the Android AccountManager for identity management.

    To initialize a service, use GoogleAccountCredential.usingOAuth2() to create credentials and then pass them to the service's Builder.

    @Override
    public void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      // Google Accounts
      credential = GoogleAccountCredential.usingOAuth2(this, Collections.singleton(TasksScopes.TASKS));
      SharedPreferences settings = getPreferences(Context.MODE_PRIVATE);
      credential.setSelectedAccountName(settings.getString(PREF_ACCOUNT_NAME, null));
      // Tasks client
      service = new com.google.api.services.tasks.Tasks.Builder(httpTransport, jsonFactory, credential)
              .setApplicationName("Google-TasksAndroidSample/1.0").build();
    }