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
104
votes
8 answers

Error: 'The project id used to call the Google Play Developer API has not been linked in the Google Play Developer Console.'

I am getting the following error while accessing Google Play Developer API using a service account: The project id used to call the Google Play Developer API has not been linked in the Google Play Developer Console. The service account email and…
82
votes
9 answers

Exception using HttpRequest.execute(): Invalid use of SingleClientConnManager: connection still allocated

I'm using google-api-client-java 1.2.1-alpha to execute a POST request, and am getting the following stacktrace when I execute() the HttpRequest. It happens immediately after I catch and ignore a 403 error from a previous POST to the same URL, and…
David Bullock
  • 6,112
  • 3
  • 33
  • 43
76
votes
2 answers

OAuth2 and Google API: access token expiration time?

We have a standalone Java application (see "Installed application") which runs periodically and uses Google API (updates some information from customer databases/ldap/...). To access Google APIs we store username and password in configuration…
61
votes
5 answers

What is "We’ve detected that your app is using an old version of the Google Play developer API" warning in Google Developer Console?

We do not use any Google Play Developer APIs explicitly, yet we are receiving the following warning: Is this related to https://developer.android.com/google/play/billing/billing_library_releases_notes ? We are currently using Google Play Billing…
Cheok Yan Cheng
  • 47,586
  • 132
  • 466
  • 875
59
votes
4 answers

Android: What is transport and jsonFactory in GoogleIdTokenVerifier.Builder?

in the blow code, whats is transport and jsonFactory ? (I do not understand) https://developers.google.com/identity/sign-in/android/backend-auth#using-a-google-api-client-library import…
41
votes
3 answers

Access to Google API - GoogleAccountCredential.usingOAuth2 vs GoogleAuthUtil.getToken()

Lately, I have been working a lot with Google APIs on Android especially Analytics, AdSense and Tasks API. I have seen some samples provided by Google where they use this statement to obtain a GoogleAccountCredential…
Madhur Ahuja
  • 22,211
  • 14
  • 71
  • 124
38
votes
5 answers

Loading Google API Javascript Client Library into Chrome Extension

I've been trying to wed the google api javascript client library with a chrome extension for a while now, but it seems the chrome extension has a terrible case of cold feet. The link to the script is https://apis.google.com/js/client.js Downloading…
woojoo666
  • 7,801
  • 7
  • 45
  • 57
32
votes
2 answers

Google Drive 403 rate limit on insert sometimes succeeds

tl;dr A Drive API call returns a failure status (403) even though the request was successfully processed. I insert 100 files in a loop. For this test I have DISABLED backoff and retry, so if an insert fails with a 403, I ignore it and proceed with…
pinoyyid
  • 21,499
  • 14
  • 64
  • 115
27
votes
4 answers

Unable to refresh access token : response is "unauthorized_client"

I am getting an error when I try to refresh access token: 400 Bad Request {error : "unauthorized_client"} From the Google token URI: { "error" : "invalid_request" } I read this answer here and the official Google documentation (which…
Martin V.
  • 3,560
  • 6
  • 31
  • 47
26
votes
4 answers

Google Auth API Javascript idpiframe initialization error on Chrome

I use GSuite and I'm coding a very simple web app to use Google Auth API, and I get an exception "idpiframe_initialization_failed". Now I have the exact HTML as shown in the google…
26
votes
6 answers

Google Drive API doesn't play well with ProGuard (NPE)

Currently, I'm having experience that, a piece of code, which makes use of Google Drive API is running fine without introducing ProGuard. However, after introducing ProGuard, I'm getting the following run-time error. at…
25
votes
2 answers

javax.net.ssl.SSLHandshakeException: Remote host closed connection during handshake while inserting rows in bigquery

Hi I am working on android app in which I have integrated bigquery. I see sometimes we are getting a lot of SSL exceptions while inserting data to big query tables. I don't know how to handle this . Please help what exactly is the cause of this…
N Sharma
  • 33,489
  • 95
  • 256
  • 444
24
votes
6 answers

Uploading image from Android to GCS

I am trying to upload an image from Android directly to Google cloud storage. But the API does not seem to work. They have some Java samples which are tied to the App engine. I don't see any sample which is proven to work on Android. On Android, I…
Gopinath
  • 12,981
  • 6
  • 36
  • 50
23
votes
6 answers

Google push notifications - Unauthorized WebHook callback channel

I'm facing problem with Google push notifications (for drive). I use service account which works perfectly for all other drive operations except drive changes watch. Below is application code which now fails with "Unauthorized WebHook callback…
21
votes
1 answer

HOWTO create GoogleCredential by using Service Account JSON

I am using the Java SDK for Android Publisher v2 and Oauth2 v2. Once I created the service account I was provided with a JSON of Google Play Android Developer service account with client id, email, private key etc. I have tried to look around and…
1
2 3
76 77