The following are some common tasks that you must perform when using Google Cloud Service APIs such as enabling a service API, installing an API client library, performing client authentication, and so on.
Prerequisites
- Eclipse Version 4.xx. Before installing Eclipse assure that you Java installed (at the least the JRE). To download Java development environment go to Java SE at a Glance.
- Maven plugin installed. Make sure to set your Eclipse preferences as follows:
- Within Eclipse, select Window > Preferences (or on Mac, Eclipse > Preferences).
- Select Maven and select the following options:
- “Download Artifact Sources”
- “Download Artifact JavaDoc”
Create a Maven Project
- In Eclipse, select File->New->Project. The Select a wizard dialog window is displayed
- Expand the Maven folder and select Maven Project
- Click Next.
- In the next dialog window, check Create a simple project (skip archetype selection).
- Click Next. The New Maven project dialog is displayed.
- Enter the Group Id information, for instance com.clientauth.
- Enter the Artifact Id (use the name of the project) for instance ClientAuth.
- Accept the Version default 0.0.1-SNAPSHOT. Or assign a new version such as 1.0.
- Assure that the Packaging is jar.
- Enter the project name, for example ClientAuthentication.
- Click Finish.
This creates a default pom.xml file that you will use to define your application dependencies as shown next.
Define Dependencies in pom.xml
To the default pom.xml, you must add the dependencies specific to your application. The example shown next refers to a console application which uses Google Storage service. To obtain the latest dependencies (aka artifacts) information, perform the following steps:
OAuth2 API Dependency
- In your browser, navigate to https://developers.google.com/api-client-library/java/apis/.
- In the page, click Ctrl-F and in the box enter oauth2. This will take you to the row containing the OAuth2 library info.
- Click on the version link, let’s say v2. This displays the Google OAuth2 API Client Library for Java page.
- At the bottom, in the Add Library to Your Project section, click on the Maven tab. This displays the dependencies information similar to the following:
<project>
<dependencies>
<dependency>
<groupId>com.google.apis</groupId>
<artifactId>google-api-services-oauth2</artifactId>
<version>v2-rev126-1.22.0</version>
</dependency>
</dependencies>
</project>
- Copy and paste the <dependency> section in the pom.xml file.
- If you want to refer to other versions of the API library click on the link at the bottom of the page. See all versions available on the Maven Central Repository.
- You can define the version in a parametric way as follows:
<version>${project.oauth.version}</version>
Where the
${project.oauth.version}
is defined in the properties section as follows:
<properties>
<project.http.version>1.22.0</project.http.version>
<project.oauth.version>v2-rev126-1.22.0</project.oauth.version>
<project.storage.version>v1-rev105-1.22.0</project.storage.version>
<project.guava.version>21.0</project.guava.version>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
So the new format is as follows:
<dependency>
<groupId>com.google.apis</groupId>
<artifactId>google-api-services-oauth2</artifactId>
<version>${project.oauth.version}</version>
</dependency>
Guava Dependency
Guava is a suite of core and expanded libraries that include utility classes, google’s collections, io classes, and much much more.
- In your browser, navigate to https://mvnrepository.com/.
- In the search box, enter the name of the API library google guava.
- Click on the tile of the library Guava: Google Core Libraries For Java.
- In the displayed page click on the required version.
- Click on the Maven tab.
- Check the Include comment …. box
- Click on the box. This will copy the content to the clipboard.
- Paste the content in the pom file
Managing Dependencies
The Guava library version might conflict with the OAuth2 library version. In order to avoid the conflict we need to add a dependencyManagement section to the pom.xml file. Follow these steps:
- In Eclipse, in the pom.xml editor, click on the Dependencies tag.
- Click on the Manage button.
- In the left pane, select the Guava and OAuth libraries.
- Click the Add button. This create the dependencyManagement section. The following shows an example:
<dependencyManagement>
<dependencies>
<dependency>
<groupId>com.google.apis</groupId>
<artifactId>google-api-services-oauth2</artifactId>
<version>${project.oauth.version}</version>
</dependency>
<dependency>
<groupId>com.google.guava</groupId>
<artifactId>guava</artifactId>
<version>${project.guava.version}</version>
</dependency>
</dependencies>
</dependencyManagement>
HTTP Dependency
This library is needed to allow a Java application to make HTTP asynchronous requests over the network through the REST API of the cloud service it uses for example Google Storage.
- In your browser, navigate to https://mvnrepository.com/.
- In the search box, enter the name of the library google http client.
- Click on the tile of the library Google HTTP Client Library For Java.
- In the displayed page click on the required version.
- Click on the Maven tab.
- Check the Include comment …. box
- Click on the box. This will copy the content to the clipboard.
- Paste the content in the pom file
Jackson Extensions to HTTP Library Dependency
This library is needed to allow a Java application to perform XML and JSON parsing.
- In your browser, navigate to https://mvnrepository.com/.
- In the search box, enter the name of the library google http client.
- Click on the tile of the library Jackson 2 Extensions To The Google HTTP Client Library For Java.
- In the displayed page click on the required version (the same you used for the HTTP library).
- Click on the Maven tab.
- Check the Include comment …. box
- Click on the box. This will copy the content to the clipboard.
- Paste the content in the pom file
Google Storage API Dependency
- In your browser, navigate to https://developers.google.com/api-client-library/java/apis/.
- In the page, click Ctrl-F and in the box enter cloud storage. This will take you to the row containing the Cloud Storage library info.
- Click on the version link, let’s say v1. This displays the Cloud Storage JSON API Client Library for Java page.
- At the bottom, in the Add Library to Your Project section, click on the Maven tab.
- Copy and paste the dependency section in the pom.xml file.
Once you have updated the pom, make sure to update the project by right-clicking on the project name then selecting
Maven->Update Project…
Import a Maven Project
- Download the archived project from the specified location.
- Unzip the downloaded archive.
- In Eclipse, create a work space or use an existing one.
- Click OK.
- Click File->Import.
- In the wizard window, select Maven->Existing Maven Projects.
- Click Next.
- Navigate (click the Browse… button), to the location containing the unzipped code archive. The following is an example of a project to import:
- Click OK. You get a window similar to this:
- Click Finish.
What Can Go Wrong?
Local JARs
You may have local JARs that must be added to the project path. If they are not included you can have errors similar to this: .
To solve this kind of problems perform the following steps:
- In Eclipse, in the Package Explorer, right click on the project name.
- Navigate to Properties->Java Build Path.
- Click on the Libraries tag.
- Click the Add JARs… button
- Select your local JAR, from the lib folder for example, and click OK.
You will get a window similar to the following:
- Click OK.
The error should disappear from the list in the Problems window.
Execution Environment
You could get a warning about the execution environment similar to the following:
To solve this kind of problems perform the following steps:
- In Eclipse, in the Package Explorer, right click on the project name.
- Navigate to Properties->Java Build Path.
- Click on the Libraries tag.
- Select the current JRE System Library.
- Click the Remove button.
- Click the Add Library… button.
- Select the JRE System Library.
- Click Next.
- Click Finish. The new JRE System Library version should be listed.
- Click OK.
The warning should disappear from the list in the Problems window.
Compiler Version
You could get an error about the compiler version similar to the following:
To solve this kind of problems perform the following steps:
- In Eclipse, in the Package Explorer, right click on the project name.
- Navigate to Properties->Java Compiler.
- In the right pane, uncheck Enable project specific settings.
- Click the link Configure Workspace Settings….
- In the next window, select version 1.8 or higher.
- Check Use default compliance settings.
- Click OK.
- Click OK.
- Click Yes, in the popup asking to recompile the project.
The error should disappear from the list in the Problems window.
Create Runnable JAR
- In Eclipse, in the Package Explorer, right click on the project name.
- Click Export.
- Expand the Java folder.
- Click Runnable JAR file.
- Click Next.
- In the Launch configuration, select the one applicable to the project.
This is the configuration you define to run the application in Eclipse.
- In the Export destination enter or browse to the location where to store the JAR and enter the name for the JAR file.
- Click Finish.
- To execute the application, open a terminal window.
- Change the directory where the JAR is located.
- Enter a command similar to the following:
java -jar google-drive-client.jar
See Also