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
21
votes
5 answers

com.google.api.client.json.jackson.JacksonFactory; missing in Google Drive example

I tried running the quickstart-sample, and this dependency: com.google.apis google-api-services-drive v2-rev78-1.15.0-rc from Drive wiki API…
eclipse
  • 2,831
  • 3
  • 26
  • 34
20
votes
1 answer

How do I get the User ID Token from a Credential object?

I have been following this tutorial to include Google Sign-in support to my Desktop app. The library I'm using is this one. Everything works and this is the implementation of the authorize() method: public Credential authorize() throws IOException…
Morgan
  • 907
  • 1
  • 13
  • 35
19
votes
2 answers

GmailApiQuickstart -

I am embarrassed that I'm simply failing with an example piece of code, but I'll blame it on the fact that it is late... I have taken a copy and paste of: https://developers.google.com/gmail/api/quickstart/quickstart-java and downloaded the client…
Andy
  • 1,421
  • 1
  • 16
  • 22
19
votes
6 answers

"Login Required" 401 Unauthorized message when calling the v3 Google Calendar API using a Service Account via OAuth 2.0

First, let me explain what I am trying to do, as this is a two part question. I am building a JAX-RS service that internally authenticates with a Google account via OAuth2, so it can access and manipulate a Google calendar. This service will be…
18
votes
2 answers

AppEngine Java Google+ Signin missing gplus_id

I'm trying to sign-in to G+ with java on Google AppEngine as explained here In Step 8, the code gets gPlusId as follow String gPlusId = request.queryParams("gplus_id"); and this request should be coming from the ajax call in Step 6, which only…
Ahmed Waheed
  • 1,281
  • 5
  • 21
  • 39
17
votes
3 answers

What is an example of using OAuth 2.0 and Google Spreadsheet API with Java?

Where is example code showing how to use the Google Data Java Client Library and its support for OAuth 2.0 with the Google Spreadsheet API (now called the Google Sheets API)?
user2952212
  • 230
  • 1
  • 3
  • 7
17
votes
1 answer

Sending POST request using com.google.api.client.http.HttpRequest object in Google API

I have to send POST request with following structure. POST https://www.googleapis.com/fusiontables/v1/tables Authorization: /* auth token here */ Content-Type: application/json { "name": "Insects", "columns": [ { …
14
votes
2 answers

java.net.UnknownHostException Unable to resolve host "accounts.google.com": No address associated with hostname while inserting rows in bigquery

Hi I am working on android app in which I have integrated BigQuery. I see we are getting a lot of exception sometimes while inserting records in BigQuery tables. We are not expertise in this but started to learn this new technology. It would be…
N Sharma
  • 33,489
  • 95
  • 256
  • 444
14
votes
1 answer

Google Drive SDK for OCR

I have just setup the quickstart google drive sdk application for Android available on this link I am trying to upload images and then do OCR on them. The sample application on the Android quickstart works fine, but when I try to set the boolean for…
13
votes
2 answers

Rare NullPointerException in GoogleApiClient using Firebase

I am using GoogleApiClient to login users using their Google account. Basically, I am using Firebase Auth with Google sign in. But I am getting this crash on some devices every single day. When I test on some of my own devices (OnePlus 3, Nexus 5X,…
12
votes
1 answer

Proguard with Android and Google API Client

first of all, sorry if this topic has been answered, but I have not found it... I'm new with Android and Proguard. At the ending of developing my appication, I want to reduce its size using proguard, of course. As I am using some libraries from…
emiquelgavalda
  • 121
  • 1
  • 3
12
votes
2 answers

Stored Credential from Google API to be reused using Java

I have successfully authorized my desktop application. It stores a file called StoredCredential. In order to not have to keep doing the copy URL into browser, accept, etc., steps every time I run the app, I want to use the credentials that I already…
Killerpixler
  • 4,200
  • 11
  • 42
  • 82
11
votes
1 answer

How to update deprecated com.google.api.client.extensions.android.http.AndroidHttp

I have the follow object HttpTransport t = AndroidHttp.newCompatibleTransport(); but whole AndroidHttp class (com.google.api.client.extensions.android.http.AndroidHttp) is marked as deprecated. I don't know what is the class that replaces this,…
11
votes
5 answers

Google Play Developer API - 400 Invalid Value - InAppPurchases

My question is similar to this one. However, I am using the API Java Client Library with a service account, making calls to the API from my server. My code is following this guide, which is very simple. However, I can't seem to get an appropriate…
11
votes
1 answer

Download files from Google Storage using Java

I have successfully automated the process to move data from Google Big Query, to Google Storage. Now I need to download the data from Google Storage to my environment in an automated way as well. I am trying to do a normal HTTP request, but…
1
2
3
76 77