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
6
votes
1 answer

Valid Google OAuth2 token unparseable?

I've got a valid OAuth2 token that Google accepts, but GoogleIdTokenVerifier cannot even parse it. The token is ya29.1.AADtN_XcjzHgauKetBvrbgHImGFg1pjiHRQAKHyTglBDjEZsTPUMQJ5p-xAKtk955_4r6MdnTe3HZ08 (no worries, it's already expired). It's obtained…
6
votes
2 answers

How to correctly obtain user's Calendar events using service account?

I'm trying to retrive Calendar events for a user in a domain. I have service account access, but i get 404 error when I try to get specific user events. Heres connection code: NetHttpTransport httpTransport =…
mihn
  • 801
  • 1
  • 11
  • 22
6
votes
2 answers

Google Analytics 3.0 auth flow

EDIT: Originally this question asked how I could authenticate with the Google Analytics API using only my API key. As vlatko pointed out, this isn't possible. Now I'm just focused on getting OAuth2 to work. I will be trying vlatko's suggestions when…
6
votes
3 answers

NoLinkedYoutubeAccount error 401 when uploading videos on Youtube with java

i would like to upload videos on youtube from my java web application: I want to receive (server side) my authToken and form-action so to upload a video. I've created a new google/youtube account I've created a youtube channel in my account i've…
5
votes
1 answer

Android Calendar API insert recurring event

Using the Google Calendar Api. After browsing the rfc2445, I'm still unable to set recurrence on an event. private String[] days = {"SU", "MO", "TU", "WE", "TH", "FR", "SA"}; private String rrule = "RRULE:FREQ=WEEKLY;WKST=MO;BYDAY="; private Event…
5
votes
1 answer

gdata-java-client + oauth2 + access_token secret

I'm currently trying to use the new java client(s) and due to legacy reasons for current state of google libraries, I need to use both the gdata and the new google java client api. Obviously I'd like to use OAuth2 -- however with OAuth2 I am not…
5
votes
0 answers

Internal Server error in GoogleDrive V2 API when using maxResults param

I am using GoogleDrive's V2 java client library to list the files from my GDrive. I am getting the below error from Google end frequently. { "code" : 500, "errors" : [ { "domain" : "global", "message" : "Internal Error", "reason" :…
5
votes
1 answer

Version compatibility between google-api-client and google-api-services

There is a project which depends on google-api-client and google-api-services-drive. google-api-client verion looks like this: 1.23.0, and there is a changelog on github for this library. google-api-services-drive version looks like this:…
manuna
  • 729
  • 14
  • 37
5
votes
2 answers

Best way to use Google APIs using OAuth 2.0 on Android

I'm trying to migrate an Android application using OAuth 1.0a to OAuth 2.0. (using the Google API Client Library for Java/Android for my OAuth 2.0 needs). What is the best/preferred solution for accessing Google APIs using OAuth 2.0 on an Android…
ddewaele
  • 22,363
  • 10
  • 69
  • 82
5
votes
1 answer

How to initialise Google drive using GoogleCedentials instead of the deprecated GoogleCedential

I have java code to connect to google drive using a service account, this works: JsonFactory JSON_FACTORY = new GsonFactory(); GoogleCredential credential = GoogleCredential.fromStream(new FileInputStream("sa.json")) …
5
votes
1 answer

How to retrieve the email body content with Gmail API?

I'm studying Gmail API. Let's say I'm interested to see which mail contains the string "foobar2000". This is my code so far: Main.java package manhattan.email.bot; import com.google.api.client.googleapis.javanet.GoogleNetHttpTransport; import…
anta40
  • 6,511
  • 7
  • 46
  • 73
5
votes
1 answer

How to get programmatically the current GKE project id from one of its clusters?

I'd like to get the current GKE project id from within one of its clusters via the Java client or the GCloud API itself. I'm running java containers in a GKE cluster of a specific Google Cloud project I initialize the ClusterManagerClient with the…
5
votes
2 answers

Google API for Google Docs, requesting a list of documents -- 400 Bad Request

After authentication from google servers for google docs, I do a simple getResponse, but I get a 400 Bad Request. I can't understand where am I going wrong. The sample code is, below private void executeRefreshAlbums() { HttpRequest…
Sana
  • 9,895
  • 15
  • 59
  • 87
5
votes
3 answers

Get timezone of area with country code in Java

I have to pass a message (jms) with timezone info like (America/Los_Angeles) but I have only country name and code. If it possible get timezone info with Java code. Somewhere I read this: System.out.println(TimeZone.getTimeZone("US")); But its…
Manvi
  • 1,136
  • 2
  • 18
  • 41
5
votes
1 answer

Google Play Android Developer API - list entitlements - "Insufficient Permission"

When trying to list entitlements am getting: 403 Forbidden { "code" : 403, "errors" : [ { "domain" : "global", "message" : "Insufficient Permission", …