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
2 answers

(JAVA) Google API analytics - unable to obtain new "access token" using "refresh token"

I want to obtain a new "access token" based on the "refresh token" saved in database. Here is the code I wrote: GoogleCredential.Builder credentialBuilder = new GoogleCredential.Builder() …
xoip
  • 51
  • 4
4
votes
1 answer

How can instantiate Class from T?

I've got following method: public execute(HttpRequest request) { ... // in parseAs i have to pass Class how can I instantiate it from T? request.execute().parseAs(classT); } PS: parseAs is method from google http client library.
skayred
  • 10,603
  • 10
  • 52
  • 94
4
votes
0 answers

Google Drive APIs returns 500 - Internal error when page size exceeds 150

I am using Google Drive's java Client Library to list and download files from my application. When trying to list files, I am getting 500 Internal Server error from Google. { "code" : 500, "errors" : [ { "domain" : "global", "message" : "Internal…
kv stack
  • 49
  • 2
4
votes
2 answers

Compatibility problems when using 2.0.0 google-api-client for Gmail calls

I've been working on a little project that connects to the user's Gmail inbox and reads the mails using google-api-client 2.0.0 and google-api-services-gmail version v1-rev20220404-2.0.0 When I try to build the Gmail service service = new…
4
votes
2 answers

How do I open GoogleSheets Service with a Stored Refresh Token using Java API v4

I am working on an application (Apache Drill) and looking to build a connector to Google Sheets using the Google's Java API (v4). I have this working, however, I would like to store the refresh token and here's where I'm stuck. Here's my existing…
4
votes
1 answer

What Is the Difference between gdata-java-client and google-api-java-client?

I'm working on a project that interfaces with the the Youtube API. I've come across the two projects (1) gdata-java-client and (2) google-api-java-client. The project's scopes (and names) are a bit confusing. It seems that google-api-java-client is…
speedplane
  • 15,673
  • 16
  • 86
  • 138
4
votes
1 answer

Using Google API Java Client for Uploading Database to Google Spreadsheet

I wanted to get some feedback before I jumped into this. I am interested in making my Android Application upload my SQLite database to Google Spreadsheet. I have gotten some mixed feedback from several people so I wanted to make sure I am going in…
gtdevel
  • 1,513
  • 6
  • 21
  • 38
4
votes
1 answer

Get Google Calendar Events Start and End Times with google-java-api-client in Android

How would one go about parsing the start and end times of events in a users Google Calendar using the google-api-java-client? After installing this sample android project from Google code, I can get into my Google calendar and parse some…
dell116
  • 5,835
  • 9
  • 52
  • 70
4
votes
1 answer

Service account created folders do not apear in google drive web application

Not able to view folders created using google api in google drive. I am using service account json to connect to google drive. I can see the files getting created using api but cannot view the folders created using api." Connecting to google drive…
4
votes
3 answers

How can i log out with AndroidAccountManager

i can login with Android AccountManager with this code: http://code.google.com/p/google-api-java-client/wiki/AndroidAccountManager But i don't know how to log out?
Nam Vu
  • 5,669
  • 7
  • 58
  • 90
4
votes
2 answers

Google Sheets use API key instead of client_secret.json

In the QuickStart.java example on Java Quickstart they use OAuth client ID to identify the application, and this pops up a windows asking for Google credentials to use the application. You have to download a client_secret.json to modify a Google…
4
votes
3 answers

Google API v3 checking exist folder by passing folder name

I'm using google API v3 for check exist folder. If folder does not exist, then create the new folder. This is my code for creating folder private void createFolderInDrive() throws IOException { boolean existed =…
Kranatos
  • 107
  • 2
  • 10
4
votes
0 answers

ID Token Verification doesn't actually require a client ID/audience?

While developing an iOS and Android app for a backend API, I'm using separate client IDs for each. However, I noticed something that I find odd, and I'm hoping someone can explain why this works and if it's ok to work this way. What I noticed is…
4
votes
1 answer

google-oauth-client-jetty cannot run on Tomcat 7

When I am using Google OAuth2, I need to use the library com.google.oauth-client google-oauth-client-jetty 1.23.0 However,…
Wenyan
  • 273
  • 4
  • 12
4
votes
1 answer

Access to users Google Calendar events

I have configured in my google account application following CalDAV and Google Calendar API instructions. As result I have turned on 2 APIs for my application CalDAV API; Google Calendar API (GC API). I have created token to get access via my…
Sergii
  • 7,044
  • 14
  • 58
  • 116