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
7
votes
4 answers

Transfer ownership for ALL files in user's google drive - using google-api-java-client and the Drive SDK

We have a google corporate account and need to transfer ALL of a user's google drive files to another account in certain instances. We want to do what is described at the following link for "all files" but programatically via the latest Drive API…
7
votes
1 answer

Invalid grant when accessing Google API

I'm trying to invoke any of the Google API using "Service account" authorization access. I have downloaded ".pk2" file and activated "URL Shortener API" in Services tab of Google API console. Whenever I try to invoke any API (URL shortener or…
user12384512
  • 3,362
  • 10
  • 61
  • 97
7
votes
3 answers

Java Google Calendar api "access_denied" on Service Account

I'm trying to connect to a calendar using the Java Google Calendar api. The java application uses a service account. I've the following code: java.io.File licenseFile = new…
7
votes
2 answers

Thumbnail 404 error

I'm trying to get the thumbnail of a file but am getting an error 404, the url that return drive is like this in some …
Alexander
  • 261
  • 2
  • 16
7
votes
2 answers

VerifyError after upgrading to Appengine 1.6.6; Cannot create instance of UrlFetchTransport - google-api-java-client 1.8.0 / 1.9.0-beta

After the upgrade this error appears : java.lang.VerifyError: (class: com/mydomain/server/webservices/OAuth2Utils, method: newFlow signature: ()Lcom/google/api/client/googleapis/auth/oauth2/GoogleAuthorizationCodeFlow;) Incompatible argument to…
koma
  • 6,486
  • 2
  • 27
  • 53
6
votes
2 answers

Authenticating with Google API using AccountManager

I've been struggling with this for a couple days now. I'm trying to make calls to Google Calendar using authentication via Android's AccountManager. I retrieve an auth token using the usual method: AccountManager manager =…
Tyler Treat
  • 14,640
  • 15
  • 80
  • 115
6
votes
4 answers

java google custom search api

I'm trying to use the java client for the Google custom search api but couldn't find any sample tutorials on the web. Can someone provide a simple example for me to get started? Thank you!
wolve80
  • 105
  • 3
  • 7
6
votes
1 answer

google-api-java-client NetHttpTransport causes NoClassDefFoundError

I just started studying google-api-java-client on Android. Added next 3 libraries into project. (I don't use Maven.) google-api-client-1.4.1-beta.jar google-api-client-googleapis-1.4.1-beta.jar google-api-client-googleapis-extensions-andr And…
Tomcat
  • 1,405
  • 3
  • 22
  • 37
6
votes
0 answers

Autocompletion in android - for youtube search

I hope my query is quite clear from the title itself. I need to implement the auto-completion for youtube(video suggestions in a list as the user begins typing). I need this feature for one of my Android activities. Experts, please advise as to how…
user264953
  • 1,837
  • 8
  • 37
  • 63
6
votes
4 answers

Gmail API 403 Insufficient Permissions domain global

@Override protected List< String > doInBackground( Void... params ) { try { //This line below is the cause of the insufficient permissions error …
Eae
  • 4,191
  • 15
  • 55
  • 92
6
votes
1 answer

How to upload video on youtube using access token and api in android?

I build web and mobile app and they are synchronized. So, after I login and authorize google account to get access token, refresh token, expires to upload video on youtube and store in database. And on mobile, I get access token, refresh token,…
6
votes
1 answer

How to use Google QPX API with Java for flight search?

I am trying to build a simple Java code that obtains flight options using Google QPX API, for the flight from New York to London. I signed up with Google and got API_key. I read the documentation, but unfortunately, I couldn't find any example that…
Akram
  • 61
  • 1
  • 2
6
votes
2 answers

Using Google Custom Search on Google Shopping

I would like to make an API call to retrieve Google Shopping result (basically the price of the product) I logged in to Google Custom Search, created a new Search engine called General, and chose in websites: http://www.google.com/shopping. However,…
Dejell
  • 13,947
  • 40
  • 146
  • 229
6
votes
2 answers

Migrating 50TB data from local Hadoop cluster to Google Cloud Storage

I am trying to migrate existing data (JSON) in my Hadoop cluster to Google Cloud Storage. I have explored GSUtil and it seems that it is the recommended option to move big data sets to GCS. It seems that it can handle huge datasets. It seems though…
obaid
  • 285
  • 1
  • 4
  • 12
6
votes
3 answers

The resource 'projects/' was not found" error when trying to get list of running instances

My goal is to test out google's orchestrator and the compute engine api by first retrieving a list of active instances. The orchestrator project including the servlet file is stored in a jar. I'm trying to test out the java google compute engine…