Open source Auth client library for Java
Clone or download
chingor13 Cleanup ImpersonatedCredentials (#212)
* Cleanup

* Fix tests

* No need for singular ERROR_PREFIX that doesn't provide extra information

* Fix spelling error
Latest commit 8225b92 Nov 14, 2018

Google Auth Library

Open source authentication client library for Java.

unstable Build Status codecov Maven

This project consists of 3 artifacts:

Note: This client is a work-in-progress, and may occasionally make backwards-incompatible changes.


If you are using Maven, add this to your pom.xml file (notice that you can replace google-auth-library-oauth2-http with any of google-auth-library-credentials and google-auth-library-appengine, depending on your application needs):


If you are using Gradle, add this to your dependencies

compile ''

If you are using SBT, add this to your dependencies

libraryDependencies += "" % "google-auth-library-oauth2-http" % "0.11.0"


This artifact contains base classes and interfaces for Google credentials:

  • Credentials: base class for an authorized identity. Implementations of this class can be used to authorize your application
  • RequestMetadataCallback: interface for the callback that receives the result of the asynchronous Credentials.getRequestMetadata(URI, Executor, RequestMetadataCallback)
  • ServiceAccountSigner: interface for a service account signer. Implementations of this class are capable of signing byte arrays using the credentials associated to a Google Service Account


This artifact depends on the App Engine SDK (appengine-api-1.0-sdk) and should be used only by applications running on App Engine. The AppEngineCredentials class allows to authorize your App Engine application given an instance of AppIdentityService.

You can install the App Engine SDK from Maven Central:

$ mvn dependency:get

You can find all available versions on Maven Central.


Application Default Credentials

This artifact contains a wide variety of credentials as well as utility methods to create them and to get Application Default Credentials. Credentials classes contained in this artifact are:

  • CloudShellCredentials: credentials for Google Cloud Shell built-in service account
  • CloudShellCredentials: credentials for Google Compute Engine built-in service account
  • OAuth2Credentials: base class for OAuth2-based credentials
  • ServiceAccountCredentials: credentials for a Service Account - use a JSON Web Token (JWT) to get access tokens
  • ServiceAccountJwtAccessCredentials: credentials for a Service Account - use JSON Web Token (JWT) directly in the request metadata to provide authorization
  • UserCredentials: credentials for a user identity and consent

To get Application Default Credentials use GoogleCredentials.getApplicationDefault() or GoogleCredentials.getApplicationDefault(HttpTransportFactory). These methods return the Application Default Credentials which are used to identify and authorize the whole application. The following are searched (in order) to find the Application Default Credentials:

  1. Credentials file pointed to by the GOOGLE_APPLICATION_CREDENTIALS environment variable
  2. Credentials provided by the Google Cloud SDK gcloud auth application-default login command
  3. Google App Engine built-in credentials
  4. Google Cloud Shell built-in credentials
  5. Google Compute Engine built-in credentials
    • Skip this check by setting the environment variable NO_GCE_CHECK=true
    • Customize the GCE metadata server address by setting the environment variable GCE_METADATA_HOST=<hostname>

Explicit Credential Loading

To get Credentials from a Service Account JSON key use GoogleCredentials.fromStream(InputStream) or GoogleCredentials.fromStream(InputStream, HttpTransportFactory). Note that the credentials must be refreshed before the access token is available.

GoogleCredentials credentials = GoogleCredentials.fromStream(new FileInputStream("/path/to/credentials.json"));
AccessToken token = credentials.getAccessToken();
// OR
AccessToken token = credentials.refreshAccessToken();


Allows a credentials issued to a user or service account to impersonate another. The source project using ImpersonaedCredentials must enable the "IAMCredentials" API. Also, the target service account must grant the orginating principal the "Service Account Token Creator" IAM role.

String credPath = "/path/to/svc_account.json";
ServiceAccountCredentials sourceCredentials = ServiceAccountCredentials
     .fromStream(new FileInputStream(credPath));
sourceCredentials = (ServiceAccountCredentials) sourceCredentials

ImpersonatedCredentials targetCredentials = ImpersonatedCredentials.create(sourceCredentials,
    "", null,
    Arrays.asList(""), 300);

Storage storage_service = StorageOptions.newBuilder().setProjectId("project-id")

for (Bucket b : storage_service.list().iterateAll())

CI Status

Java Version Status
Java 7 Kokoro CI
Java 8 Kokoro CI
Java 8 OSX Kokoro CI
Java 8 Windows Kokoro CI
Java 10 Kokoro CI


Contributions to this library are always welcome and highly encouraged.

See CONTRIBUTING documentation for more information on how to get started.

Please note that this project is released with a Contributor Code of Conduct. By participating in this project you agree to abide by its terms. See Code of Conduct for more information.

Running the Tests

To run the tests you will need:

$ mvn install -DskipTests=true
$ mvn test


BSD 3-Clause - See LICENSE for more information.