Easily access Google APIs from Java
The Google API Client Library for Java provides functionality common to all Google APIs, for example HTTP transport, error handling, authentication, JSON parsing, media download/upload, and batching. The library includes a powerful OAuth 2.0 library with a consistent interface; lightweight, efficient XML and JSON data models that support any data schema; and support for protocol buffers.
To call a Google API using Google's client libraries for Java, you need the generated Java library for the Google API you are accessing. These generated libraries include the core google-api-java-client library along with API-specific information such as the root URL. They also include classes that represent entities in the context of the API, and that are useful for making conversions between JSON objects and Java objects.
To call a Google API using Google's client libraries for Java, you need the generated Java library for the Google API you are accessing. These generated libraries include the core google-api-java-client library along with API-specific information such as the root URL. They also include classes that represent entities in the context of the API, and that are useful for making conversions between JSON objects and Java objects.
Beta features
Features marked with @Beta at the class or method level are subject to change. They might be modified or removed in any major release. Do not use beta features if your code is a library itself (that is, if your code is used on the CLASSPATH of users outside your control).
Deprecated features
Deprecated non-beta features will be removed eighteen months after the release in which they are first deprecated. You must fix your usages before this time. If you don't, any type of breakage might result, and you are not guaranteed a compilation error.
Highlights of the Google API Client Library for Java
It's simple to call Google APIs
You can call Google APIs using Google service-specific generated libraries with the Google API Client Library for Java. (To find the generated client library for a Google API, visit the list of supported Google APIs.) Here's an example that uses the Calendar API Client Library for Java to make a call to the Google Calendar API:
// Show events on user's calendar. View.header("Show Calendars"); CalendarListfeed=client.calendarList().list().execute(); View.display(feed);
The library makes batching and media upload/download easier
The library offers helper classes for batching, media upload, and media download.The library makes auth easier
The library includes a powerful authentication library that can reduce the amount of code you need to handle OAuth 2.0. Sometimes a few lines is all you need. For example:
/** Authorizes the installed application to access user's protected data. */ privatestaticCredentialauthorize()throwsException{ // load client secrets GoogleClientSecretsclientSecrets=GoogleClientSecrets.load(JSON_FACTORY, newInputStreamReader(CalendarSample.class.getResourceAsStream("/client_secrets.json"))); // set up authorization code flow GoogleAuthorizationCodeFlowflow=newGoogleAuthorizationCodeFlow.Builder( httpTransport,JSON_FACTORY,clientSecrets, Collections.singleton(CalendarScopes.CALENDAR)).setDataStoreFactory(dataStoreFactory) .build(); // authorize returnnewAuthorizationCodeInstalledApp(flow,newLocalServerReceiver()).authorize("user"); }
The library runs on Google App Engine
App Engine-specific helpers make quick work of authenticated calls to APIs, and you do not need to worry about exchanging code for tokens.
For example:
For example:
@Override protectedvoiddoGet(HttpServletRequestreq,HttpServletResponseresp)throwsIOException{ AppIdentityCredentialcredential= newAppIdentityCredential(Arrays.asList(UrlshortenerScopes.URLSHORTENER)); Urlshortenershortener= newUrlshortener.Builder(newUrlFetchTransport(),newJacksonFactory(),credential) .build(); UrlHistoryhistory=shortener.URL().list().execute(); ... }
The library runs on Android 4.4 or higher.
The Google Client Library for Java's Android-specific helper classes are well-integrated with Android AccountManager. For example:
@Override publicvoidonCreate(BundlesavedInstanceState){ super.onCreate(savedInstanceState); // Google Accounts credential= GoogleAccountCredential.usingOAuth2(this,Collections.singleton(TasksScopes.TASKS)); SharedPreferencessettings=getPreferences(Context.MODE_PRIVATE); credential.setSelectedAccountName(settings.getString(PREF_ACCOUNT_NAME,null)); // Tasks client service= newcom.google.api.services.tasks.Tasks.Builder(httpTransport,jsonFactory,credential) .setApplicationName("Google-TasksAndroidSample/1.0").build(); }
Installation is easy
If you are not using a generated library, you can download the binary for the Google API Client Library for Java directly from the downloads page, or you can use Maven or Gradle. To use Maven, add the following lines to your pom.xml file:
To use Gradle, add the following lines to your build.gradle file:
For more details about installing and setting up the Google API
Client Library for Java, see the
download
and setup instructions.
<project> <dependencies> <dependency> <groupId>com.google.api-client</groupId> <artifactId>google-api-client</artifactId> <version>1.32.1</version> </dependency> </dependencies> </project>
To use Gradle, add the following lines to your build.gradle file:
repositories{ mavenCentral() } dependencies{ compile'com.google.api-client:google-api-client:1.32.1' }
Supported environments
The Google API Client Library for Java supports these Java environments:
- Java 7 or higher, standard (SE) and enterprise (EE).
- Google App Engine.
- Android 4.4 or higher — but if a Google Play Services library is available for the Google service you need, use that library instead of this one. The Google Play library will give you the best possible performance and experience.