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

Cannot resolve import com.google.api.client.json.gson.GsonFactory

Am trying to use the GsonFactory class in my app: StudentApi.Builder builder = new StudentApi.Builder(AndroidHttp.newCompatibleTransport(), new GsonFactory(), null); but it says cannot resolve symbol 'GsonFactory' I have the import in my class…
Ojonugwa Jude Ochalifu
  • 26,627
  • 26
  • 120
  • 132
11
votes
3 answers

Access Not Configured Android Google API Key

I am trying to use Youtube Data API v3 to search on android, though when I try to search using my API key i always get this message: { "error": { "errors": [ { "domain": "usageLimits", "reason": "accessNotConfigured", "message":…
11
votes
4 answers

Cancel Google Drive upload possible?

I have written an app using the official API to upload to Google drive, this works perfectly. However I can't find anyway to cancel an upload. I'm running it in an ASyncTask, so far I have tried making the drive object, the file path, the token…
11
votes
4 answers

Google Credential: This developer account does not own the application

I'm using Google client libraries and trying to make a GET request to Google Play API. GoogleCredential credential= new GoogleCredential.Builder().setTransport(netHttpTransport) .setJsonFactory(jacksonFactory) …
user1383845
  • 183
  • 1
  • 2
  • 8
9
votes
1 answer

IllegalArgumentException when authenticating using Google's Java API client library

I'm trying to run QuickStart.java https://developers.google.com/sheets/api/quickstart/java on NetBeans 8.2. I've imported all the Google Libraries and I'm having this issue Exception in thread "main" java.lang.IllegalArgumentException at…
Mr. Kevin
  • 327
  • 4
  • 12
9
votes
4 answers

Write to GoogleSheet via API with Java

I am trying to write a value to a cell with Google Sheet API with Java. For reading I used guide from Java Quickstart which worked fine for me. For writing to Google Sheet I use: service.spreadsheets().values().update(spreadsheetId, "Sheet1!A4:H",…
9
votes
1 answer

Google Webmasters API for Java returns empty site list

I have written a simple site list query code which uses Oauth with service account based on Google's documentation. The authentication key file (.p12) being used is valid as well as the account. The problem is that sites list method returns an empty…
9
votes
2 answers

StorageServiceAccountSample application reports "KeyStore JKS implementation not found"

The very first line of StorageServiceAccountSample HTTP_TRANSPORT = GoogleNetHttpTransport.newTrustedTransport(); causes the "KeyStore JKS implementation not found": W/System.err(20142): java.security.KeyStoreException:…
Sailor Jerry
  • 201
  • 2
  • 8
9
votes
1 answer

How to get started with Google Custom Search API?

I'm trying to write a Java program that uses Google's Custom Search API to retrieve search results. As a first-time user of API's, though, the documentation on the website is extremely confusing - does one retrieve results by connecting to the…
Jess
  • 1,515
  • 3
  • 23
  • 32
9
votes
1 answer

How do I request paginated BigQuery query results using pageTokens with the Google Client lib for Java?

I want to run BigQuery queries with thousands of rows of total results, but I only want to retrieve a page of 100 results at a time (using the maxResults and pageToken parameters). The BigQuery API supports the use of pageToken parameters on…
Michael Manoochehri
  • 7,931
  • 6
  • 33
  • 47
9
votes
2 answers

OAuth Google API for Java unable to impersonate user

I would like to impersonate a user and add files to the users Google Drive on their behalf from a server process. I've setup a service account and can successfully access the Drive as the service account adding and listing files, etc. using the…
David
  • 699
  • 7
  • 18
8
votes
1 answer

Can I get authorization for Youtube's Reporting API from a non UI application?

I want to run an application (on AWS Lambda) periodically that fetches reports using Youtube's reporting API and stores the reports in a database. I'm the owner of the Youtube channel. However, I'm facing issues setting up the authorization. The…
8
votes
2 answers

How to get authorization for accessing Google Play developer API

I am trying to access Google play developer API https://developers.google.com/android-publisher/ to build an inapp purchase product for my company Application. We have to get authorization to make a GET call to the Google play developer API.…
8
votes
1 answer

How to upload Android and Android TV APK to Google Play via Google Play API

Background Android application has 2 version: 1 for mobile devices, 1 for Android TV. Android TV APK version code should be significantly higher than mobile devices Android APK version code. APK for mobile devices has version codes like 10, 11, 12,…
8
votes
2 answers

Unable to block room using Google calendar Api

This is my java code using this code I am trying to create event with room (room is added using resource Google Calendar API) event created success fully with room A. However when I check in Google Calendar and try see available room in that A…
1 2
3
76 77