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

gson got com.google.gson.internal.LinkedTreeMap

I have the following json { "kind": "youtube#channelListResponse", "etag": "\"cbz3lIQ2N25AfwNr-BdxUVxJ_QY/QM4kW8nb-nymx1biZnF8bIvgOfE\"", "pageInfo": { "totalResults": 1, "resultsPerPage": 1 }, "items": [ { "kind":…
Vihaan Verma
  • 12,815
  • 19
  • 97
  • 126
5
votes
4 answers

Google Admin SDK 403 Not Authorized to Access this Resource/API

I use the following code in a java web application to try to get all users of a group: GoogleCredential credential = GoogleCredential.fromStream(Util.class.getResourceAsStream("[credential_file].json")).createScoped(SCOPES); Directory directory =…
theyuv
  • 1,556
  • 4
  • 26
  • 55
5
votes
0 answers

Retrieving current location in android of API Level 19 to 22

I use the code below in my project to retrieve the current location of the user. However, this seems to work on my test devices of API Level 23, but does not work on my test devices of API levels 19 to 22. The request for a location will return null…
Joeri Verlooy
  • 613
  • 5
  • 19
5
votes
1 answer

How to retrieve youtube thumbnails using Google API Client Library for Java

I would be obliged, if you could kindly let me know the means by which I can retrieve the youtube thumbnails using Google API Client Library for Java, similar to the way in which we fetched thumbnails using gdata. List thumbnails = new…
user264953
  • 1,837
  • 8
  • 37
  • 63
5
votes
2 answers

How to apply multiple filters in google analytics

How to filter multiple dimensions in Google analytics. Nether of the following work: .setFilters("ga:userType==anonymous").setFilters( "ga:dimension3==1234") .setFilters("ga:userType==anonymous","ga:dimension3==1234") The second one gives an…
5
votes
2 answers

Connecting to Google Compute Engine using Oauth

I'm attempting to connect to Google Compute Engine using Java, but getting an exception that doesn't mean much to me. The example I'm following says the following: /** Authorizes the installed application to access user's protected data. */ private…
5
votes
3 answers

Downloading Google Sheets spreadsheets from Google Drive on Android

I am working on an Android app that uses the gdata-java-client to download documents for display only. So far I have an application that authenticates with the services and displays a list of user documents. When the user selects a document another…
5
votes
1 answer

AppIdentityCredential And AppEngineDataStoreFactory

I don't understand why I can't save AppIdentityCredential with AppEngineDataStoreFactory AppEngineDataStoreFactory newFactory = new AppEngineDataStoreFactory(); AppIdentityCredential credential = new…
5
votes
2 answers

Uploading video via YouTube API

I'm uploading video to YouTube programmatically using YouTube API. Some of my videos need to be labeled age-restricted, so I want to specify AgeGating video attribute. When video.setAgeGating(gating) is specified the appropriate part name has to be…
mzhuk
  • 140
  • 10
5
votes
2 answers

How to get user profile on Google API using the JAVA library?

I have a Java ClientRequest to the Google API that returns a JSON containing the profile based on an access_token. The URL is: https://www.googleapis.com/oauth2/v1/userinfo?access_token=ya29.1.AADtN_VALuuUTBm8ENfNTz8s... And the response is: { …
5
votes
3 answers

"401 Unauthorized" when trying to watch changes on Google Drive with Java API Client

Really stuck here. The code, built from examples provided by Google: public static void main(String[] args) { try { HttpTransport httpTransport = new NetHttpTransport(); JsonFactory jsonFactory = new JacksonFactory(); …
seinecle
  • 10,118
  • 14
  • 61
  • 120
5
votes
1 answer

What is the Maven dependency for Directory API Client Library for Java?

My maven project uses Directory API Client Library for Java and i needed to include it as a dependency. But in Directory API Client Library for Java page, under the Add Library to Your Project title when i select maven as my build environment it…
DarRay
  • 2,550
  • 6
  • 27
  • 39
5
votes
1 answer

Using Android Drive API from Background Service

There is an example of how to use Drive API https://developers.google.com/drive/quickstart-android. It works well, but I have some trouble while trying to implement uploading file to GDrive from background service. In all examples which I found,…
Denys
  • 51
  • 1
5
votes
2 answers

Refresh Token with Google API Java Client Library

I'm using the Google API Java Client http://code.google.com/p/google-api-java-client/ and am able to get the access token successfully for Android. // Google Accounts credential = GoogleAccountCredential.usingOAuth2(this,…
5
votes
3 answers

Google API Java Client - Get user info

I'm having trouble figuring out a seemingly simple task in the Google API Java client library version 1.12.0-beta. I can authenticate with OAuth2 and can retrieve and manipulate parts of Google Drive that I need for my application. However, I…