Questions tagged [google-api-java-client]

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.

Google APIs Client Library for Java

Overview

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.

The library has the following features:

Accessing Google APIs

To use Google's Java client libraries to call any Google API, you need two libraries:

  • The core Google APIs Client Library for Java (google-api-java-client), which is the generic runtime library described here. This library provides functionality common to all APIs, for example HTTP transport, error handling, authentication, JSON parsing, media download/upload, and batching.
  • An auto-generated Java library for the API you are accessing, for example the generated Java library for the BigQuery API. These generated libraries include API-specific information such as the root URL, and classes that represent entities in the context of the API. These classes are useful for making conversions between JSON objects and Java objects.

To find the generated library for a Google API, visit Google APIs Client Library for Java. The API-specific Java packages include both the core google-api-java-client and the client-specific libraries.

If you are using the old GData library, you need to migrate.

Developing for Android

If you are developing for Android and the Google API you want to use is included in the Google Play Services library, you should use that library for the best performance and experience.

To access other Google APIs, you can use the Google APIs Client Library for Java, which supports Android 1.5 (or higher).

Other Java environments

In addition to Android 1.5 or higher, the Google APIs Client Library for Java supports the following Java environments: - Java 5 (or higher), standard (SE) and enterprise (EE) - Google App Engine

Not supported: Google Web Toolkit (GWT), Java mobile (ME), and Java 1.4 (or earlier).

Highlighted Features

  • The library makes it simple to call Google APIs.

    You can call Google APIs using Google service-specific generated libraries with the Google APIs Client Library for Java. Here's an example that makes a call to the Google Calendar API:

    java // Show events on user's calendar. View.header("Show Calendars"); CalendarList feed = client.calendarList().list().execute(); View.display(feed);

  • The library makes authentication easier.

    The authentication library can reduce the amount of code needed to handle OAuth 2.0, and sometimes a few lines is all you need. For example:

    java /** Authorizes the installed application to access user's protected data. */ private static Credential authorize() throws Exception { // load client secrets GoogleClientSecrets clientSecrets = GoogleClientSecrets.load(JSON_FACTORY, new InputStreamReader(CalendarSample.class.getResourceAsStream("/client_secrets.json"))); // set up authorization code flow GoogleAuthorizationCodeFlow flow = new GoogleAuthorizationCodeFlow.Builder( httpTransport, JSON_FACTORY, clientSecrets, Collections.singleton(CalendarScopes.CALENDAR)).setDataStoreFactory(dataStoreFactory) .build(); // authorize return new AuthorizationCodeInstalledApp(flow, new LocalServerReceiver()).authorize("user"); }

  • The library makes batching and media upload/download easier.

    The library offers helper classes for batching, media upload, and media download.

  • 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:

    java @Override protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws IOException { AppIdentityCredential credential = new AppIdentityCredential(Arrays.asList(UrlshortenerScopes.URLSHORTENER)); Urlshortener shortener = new Urlshortener.Builder(new UrlFetchTransport(), new JacksonFactory(), credential) .build(); UrlHistory history = shortener.URL().list().execute(); ... }

  • The library runs on Android (@Beta).

    If you are developing for Android and the Google API you want to use is included in the Google Play Services library, you should use that library for the best performance and experience.

    To access other Google APIs, you can use the Google Client Library for Java's Android-specific helper classes, which are are well-integrated with Android AccountManager. For example:

    java @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(); }

  • The library is easy to install.

    The Google APIs Client Library for Java is easy to install, and you can download the binary directly from the Downloads page, or you can use Maven or Gradle. To use Maven, add the following lines to your pom.xml file:

    maven <project> <dependencies> <dependency> <groupId>com.google.api-client</groupId> <artifactId>google-api-client</artifactId> <version>1.20.0</version> </dependency> </dependencies> </project>

    To use Gradle, add the following lines to your build.gradle file:

    gradle repositories { mavenCentral() } dependencies { compile 'com.google.api-client:google-api-client:1.20.0' }

Dependencies

This library is built on top of two common libraries, also built by Google, and also designed to work with any HTTP service on the web: * Google HTTP Client Library for Java * Google OAuth Client Library for Java

Important Warnings

@Beta

Features marked with the @Beta annotation at the class or method level are subject to change. They might be modified in any way, or even removed, in any major release. You should 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 own control).

Deprecations

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.

Documentation

Links

1147 questions
8
votes
4 answers

How to send an email using Gmail API and Java

I have a Web Service deployed on Openshift. Currently what I'm developing is a kind of sending an automated email at a certain point, using my Gmail account. So I have been documenting myself for two or three days and I've concluded that I've two…
joninx
  • 1,775
  • 6
  • 31
  • 59
8
votes
3 answers

Maven usage of google api

I'm using the google calendar java API for a project. The calendar part is fine, maven seems to download it and use it without any hassle. My problem comes from the main dependency of this lib: the com.google.api.client api. In particular, when I…
m09
  • 7,490
  • 3
  • 31
  • 58
8
votes
2 answers

How to send a POST request with JSON data using google api client library for java?

I'm trying to send a post request using the google api client library but not able to succeed. This is the snippet I'm using UrlEncodedContent urlEncodedContent = new UrlEncodedContent(paramMap); //paramMap contains email and password…
7
votes
2 answers

Google Calendar API OAuth2 Troubles on Android Honeycomb

I am working on an Android Honeycomb (v3.0) application that has a requirement of communicating with the Google Calendar API. I would like to allow my application to access a particular Google account's Calendar data in order to read and create…
7
votes
5 answers

java.lang.NoSuchMethodError: com.google.api.client.http.HttpTransport.isMtls()Z

I have an application I deploy on appengine using java8. Lately when I tried deploying I get this error on run time: Uncaught exception from servlet java.lang.NoSuchMethodError: com.google.api.client.http.HttpTransport.isMtls()Z at…
7
votes
0 answers

Use a service account to get the list of users from Google domain

Hello all. I have been assigned the task of fetching unanswered emails from the inbox of each member of our Google domain using Spring Boot, but I haven't been able to do it. In first place, I need the list of users from the domain. This can be…
7
votes
1 answer

Get the list of action items from Google Drive API

Hi, everyone. I have been trying to use Google Drive API for getting a list with the action items assigned in all files (docs or spreadsheets) in my company's domain using Spring Boot and the google-api-services-drive, but I have faced some…
7
votes
5 answers

Missing a valid API key about Google Translation API Client issue?

I follow the https://cloud.google.com/translate/docs/reference/libraries#client-libraries-usage-java to get started java client demo. I have already set authentication json file to the environment variable GOOGLE_APPLICATION_CREDENTIALS. However, I…
7
votes
1 answer

Why does google-api-client depend on guava-jdk5?

I have a dependency on Guava in my Maven dependencies: com.google.guava guava 23.3-jre I also have a dependency to Google API Client in my…
7
votes
1 answer

401 Unauthorized error when using a server account to impersonate a user in order to access their Google Drive

I am writing a back-end process in Java that will impersonate a user and add/remove documents on their Google Drive. The server account seems to authenticate correctly but when I try to impersonate a user, I get a 401 Unauthorized error. Please see…
7
votes
3 answers

How to get more than 100 results from Google Custom Search API

I am trying to use Google Custom Search API for research purposes in Java. As a result I'm gonna need a big result set for each query. However it seems that I'm limited with first 100 results which is much less than what I need. I use the list…
7
votes
1 answer

Change READ TIMEOUT for a batch HTTP Request on using Google API Client Library for Java

I am making batch requests for adding members to groups. For this I am using OAuth2.0 and obtaining the object of class type Credential.When executed, the batch.execute() throws a java.net.SocketTimeoutException : Read timed out To change the…
7
votes
2 answers

Spring : Google authentication redirect_uri_mismatch and URL wont open on browser

I am working on a Spring-MVC application running on tomcat in which I would like to use Google drive functionality. I tried with a service account on my local machine and I had no problems. But when I uploaded the code on server, the browser URL…
We are Borg
  • 5,117
  • 17
  • 102
  • 225
7
votes
1 answer

Gmail atom feed with 2-legged OAuth receive 401 error

We are experiencing the issue of Gmail atom feed with 2-legged OAuth, an error message is "401 unauthorized". The error message is like below. Unauthorized
7
votes
1 answer

Managing Access Token with Google Drive Oauth 2.0

I'm using Google API Java Client to manage access to Google Drive API from Google App Engine in Java. I get a user access token and refresh token and save both in our database. Although, I think only the refresh token needs to be persistent. How do…