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

Google OAuth Client API - java.lang.NoClassDefFoundError: Failed resolution of: Lorg/mortbay/jetty/Server

I am currently trying to run through the upload video sample code for Youtube API v3. There is something wrong with the creation of the jetty server. I ran through this code the first time without problems, but now I'm getting an error. Here is how…
selflearningcode
  • 738
  • 1
  • 7
  • 10
4
votes
1 answer

Google Drive Rest API v3 - how to move a file to the trash?

The Google Drive Rest API v3 has a Drive.Files.Delete method, but that permanently deletes the file.How do i move the file to the trash? I looked at the docs for updating file metadata, and i tried to do this, but it doesn't seem to work: File file…
cgifox
  • 639
  • 11
  • 23
4
votes
2 answers

How to mock Google API AndroidPublisher request

I've got a class that makes a request to the AndroidPublisher API to get the Purchases.Subscriptions.get resource. How does one mock out the OAuth request and call to get the subscription resource using the classes from the package…
4
votes
1 answer

HttpListenerException "access is denied" using GoogleWebAuthorizationBroker.AuthorizeAsync

I'm trying to do OAuth2 using Azure hosted web apps, and I can't use service accounts (there is a number of solutions available here, but they all stick to service accounts/certs), while I need the user to authenticate and authorize by…
4
votes
1 answer

Youtube support in google-api-java-client vs gdata-java-client

I'm trying to use the google-api-java-client (I suppose this is what is referred to as 2.0) for writing an android application for youtube. (gdata-java-client (1.x) does not support android, I understand). I have loooked through the youtube…
Kumar
  • 267
  • 5
  • 13
4
votes
2 answers

2-legged OAuth with google-api-java-client

Does anyone know how to use 2-legged OAuth with google-api-java-client? I'm trying to access the Google Apps Provisioning API to get the list of users for a particular domain. The following does not work HttpTransport transport =…
user452499
  • 101
  • 1
  • 6
4
votes
2 answers

Java Jar -java.lang.NoClassDefFoundError: com.google.api.client.http.HttpTransport from

i am using webmaster tool Java api V3 to query data , my code work fine in eclipse but when i export it to a Jar i get the error below Exception in thread "main" java.lang.NoClassDefFoundError: com.google.api.client.http.HttpTransport at…
4
votes
1 answer

App Engine Java API pageSize

Why has google decided to ignore pageSize parameter and why the overall size of the collected items differs when using different page size? Here is an example: Appsactivity.Activities.List request = service.activities().list() …
4
votes
2 answers

Retrofit2 (interceptor) + GoogleApiClient how to refresh token

What is the best way to have a valid IdToken in every request? My first bet would be an okhttpclient interceptor which adds the token to every request. But I dont know how to get a valid token inside the interceptor. The documentation of…
4
votes
2 answers

How can I get a list of calendar resources via Google API

I try to use fetch list of Calendar Resources (https://support.google.com/a/answer/60766?hl=en) from my company's Google Apps, using Google API. I try to use it using OAuth 2.0 Playground page (https://developers.google.com/oauthplayground). What I…
Krzysztof Wolny
  • 10,576
  • 4
  • 34
  • 46
4
votes
1 answer

SessionExpiredException occured when trying to create a post using Google Blogger API OAuth2

I am using the below code to update an existing blog post. I'm getting SessionExpiredException. What am I doing wrong? GoogleService service = new BloggerService("MyBloggerIntegration-v1"); HttpTransport httpTransport =…
cegprakash
  • 2,937
  • 33
  • 60
4
votes
1 answer

Where is the Java Google Gmail Api located?

I know this may seem like a really stupid question, but I'm actually having a lot of trouble finding the Google Gmail API. I've searched all over Google's developer website, but all I get are downloads to the general google-api-client. Those are…
Scooter
  • 1,031
  • 1
  • 14
  • 33
4
votes
2 answers

com.google.a.a.a.a.l 400 Bad Request { "error" : "invalid_grant" } while inserting row in bigquery

Hi I am working on an android app in which I have integrated BigQuery. I am getting lots of exceptions when inserting records in BigQuery tables. I am not an expert in this so it would be great if you guys could help me on this. 1. Error log …
N Sharma
  • 33,489
  • 95
  • 256
  • 444
4
votes
2 answers

members.list() in Google Admin SDK Directory API (Java)

I have a piece of code(in Java) to list all members of a group on a personal Google Apps Domain. This uses the google Directory API. Here is the snippet: public static void listMembers(String groupKey,Directory service) throws IOException { …
4
votes
1 answer

Making local copy of file using Google Drive API

I want to make local copy(external storage) of a '.ppt' file present on Google Drive using Google Drive android Api. I am reading one byte and writing it to my local file. But when I am trying to open it shows that file is corrupt. Please help me to…