# Class DownscopedCredentials (1.46.0)

1.46.0 (latest) 1.43.0 1.42.1 1.41.0 1.40.0 1.39.1 1.38.0 1.37.1 1.36.0 1.35.0 1.34.0 1.33.1 1.32.1 1.31.0 1.30.1 1.29.1-SNAPSHOT 1.28.0 1.27.0 1.26.0 1.25.0 1.24.1 1.20.0 1.19.0 1.18.0 1.7.0 1.6.1 1.4.0 1.3.0

    public final class DownscopedCredentials extends OAuth2Credentials

DownscopedCredentials enables the ability to downscope, or restrict, the Identity and Access
Management (IAM) permissions that a short-lived credential can use for Cloud Storage.

This class provides a server-side approach for generating downscoped tokens, suitable for
situations where Credential Access Boundary rules change infrequently or a single downscoped
credential is reused many times. For scenarios where rules change frequently, or you need to
generate many unique downscoped tokens, the client-side approach using `
com.google.auth.credentialaccessboundary.ClientSideCredentialAccessBoundaryFactory` is more
efficient.

To downscope permissions you must define a [CredentialAccessBoundary](https://docs.cloud.google.com/java/docs/reference/google-auth-library/latest/com.google.auth.oauth2.CredentialAccessBoundary) which specifies
the upper bound of permissions that the credential can access. You must also provide a source
credential which will be used to acquire the downscoped credential.

See [for more
information.](https://cloud.google.com/iam/docs/downscoping-short-lived-credentials)

Usage:


     GoogleCredentials sourceCredentials = GoogleCredentials.getApplicationDefault()
        .createScoped("https://www.googleapis.com/auth/cloud-platform");

     CredentialAccessBoundary.AccessBoundaryRule rule =
         CredentialAccessBoundary.AccessBoundaryRule.newBuilder()
             .setAvailableResource(
                 "//storage.googleapis.com/projects/_/buckets/bucket")
             .addAvailablePermission("inRole:roles/storage.objectViewer")
             .build();

     DownscopedCredentials downscopedCredentials =
         DownscopedCredentials.newBuilder()
             .setSourceCredential(sourceCredentials)
             .setCredentialAccessBoundary(
                 CredentialAccessBoundary.newBuilder().addRule(rule).build())
             .build();

     AccessToken accessToken = downscopedCredentials.refreshAccessToken();

     OAuth2Credentials credentials = OAuth2Credentials.create(accessToken);

     Storage storage =
     StorageOptions.newBuilder().setCredentials(credentials).build().getService();

     Blob blob = storage.get(BlobId.of("bucket", "object"));
     System.out.printf("Blob %s retrieved.", blob.getBlobId());
     
Note that [OAuth2CredentialsWithRefresh](https://docs.cloud.google.com/java/docs/reference/google-auth-library/latest/com.google.auth.oauth2.OAuth2CredentialsWithRefresh) can instead be used to consume the downscoped
token, allowing for automatic token refreshes by providing a [OAuth2CredentialsWithRefresh.OAuth2RefreshHandler](https://docs.cloud.google.com/java/docs/reference/google-auth-library/latest/com.google.auth.oauth2.OAuth2CredentialsWithRefresh.OAuth2RefreshHandler).

## Inheritance

[java.lang.Object](https://docs.oracle.com/javase/8/docs/api/java/lang/Object.html) \> [Credentials](https://docs.cloud.google.com/java/docs/reference/google-auth-library/latest/com.google.auth.Credentials) \> [OAuth2Credentials](https://docs.cloud.google.com/java/docs/reference/google-auth-library/latest/com.google.auth.oauth2.OAuth2Credentials) \> DownscopedCredentials

## Inherited Members

[Credentials.blockingGetToCallback(URI,RequestMetadataCallback)](https://docs.cloud.google.com/java/docs/reference/google-auth-library/latest/com.google.auth.Credentials#com_google_auth_Credentials_blockingGetToCallback_java_net_URI_com_google_auth_RequestMetadataCallback_) [Credentials.getMetricsCredentialType()](https://docs.cloud.google.com/java/docs/reference/google-auth-library/latest/com.google.auth.Credentials#com_google_auth_Credentials_getMetricsCredentialType__) [Credentials.getRequestMetadata()](https://docs.cloud.google.com/java/docs/reference/google-auth-library/latest/com.google.auth.Credentials#com_google_auth_Credentials_getRequestMetadata__) [Credentials.getUniverseDomain()](https://docs.cloud.google.com/java/docs/reference/google-auth-library/latest/com.google.auth.Credentials#com_google_auth_Credentials_getUniverseDomain__) [OAuth2Credentials.\<T\>getFromServiceLoader(Class\<? extends T\>,T)](https://docs.cloud.google.com/java/docs/reference/google-auth-library/latest/com.google.auth.oauth2.OAuth2Credentials#com_google_auth_oauth2_OAuth2Credentials__T_getFromServiceLoader_java_lang_Class___extends_T__T_) [OAuth2Credentials.addChangeListener(OAuth2Credentials.CredentialsChangedListener)](https://docs.cloud.google.com/java/docs/reference/google-auth-library/latest/com.google.auth.oauth2.OAuth2Credentials#com_google_auth_oauth2_OAuth2Credentials_addChangeListener_com_google_auth_oauth2_OAuth2Credentials_CredentialsChangedListener_) [OAuth2Credentials.create(AccessToken)](https://docs.cloud.google.com/java/docs/reference/google-auth-library/latest/com.google.auth.oauth2.OAuth2Credentials#com_google_auth_oauth2_OAuth2Credentials_create_com_google_auth_oauth2_AccessToken_) [OAuth2Credentials.equals(Object)](https://docs.cloud.google.com/java/docs/reference/google-auth-library/latest/com.google.auth.oauth2.OAuth2Credentials#com_google_auth_oauth2_OAuth2Credentials_equals_java_lang_Object_) [OAuth2Credentials.getAccessToken()](https://docs.cloud.google.com/java/docs/reference/google-auth-library/latest/com.google.auth.oauth2.OAuth2Credentials#com_google_auth_oauth2_OAuth2Credentials_getAccessToken__) [OAuth2Credentials.getAdditionalHeaders()](https://docs.cloud.google.com/java/docs/reference/google-auth-library/latest/com.google.auth.oauth2.OAuth2Credentials#com_google_auth_oauth2_OAuth2Credentials_getAdditionalHeaders__) [OAuth2Credentials.getAuthenticationType()](https://docs.cloud.google.com/java/docs/reference/google-auth-library/latest/com.google.auth.oauth2.OAuth2Credentials#com_google_auth_oauth2_OAuth2Credentials_getAuthenticationType__) [OAuth2Credentials.getRequestMetadata(URI)](https://docs.cloud.google.com/java/docs/reference/google-auth-library/latest/com.google.auth.oauth2.OAuth2Credentials#com_google_auth_oauth2_OAuth2Credentials_getRequestMetadata_java_net_URI_) [OAuth2Credentials.getRequestMetadata(URI,Executor,RequestMetadataCallback)](https://docs.cloud.google.com/java/docs/reference/google-auth-library/latest/com.google.auth.oauth2.OAuth2Credentials#com_google_auth_oauth2_OAuth2Credentials_getRequestMetadata_java_net_URI_java_util_concurrent_Executor_com_google_auth_RequestMetadataCallback_) [OAuth2Credentials.getRequestMetadataInternal()](https://docs.cloud.google.com/java/docs/reference/google-auth-library/latest/com.google.auth.oauth2.OAuth2Credentials#com_google_auth_oauth2_OAuth2Credentials_getRequestMetadataInternal__) [OAuth2Credentials.hasRequestMetadata()](https://docs.cloud.google.com/java/docs/reference/google-auth-library/latest/com.google.auth.oauth2.OAuth2Credentials#com_google_auth_oauth2_OAuth2Credentials_hasRequestMetadata__) [OAuth2Credentials.hasRequestMetadataOnly()](https://docs.cloud.google.com/java/docs/reference/google-auth-library/latest/com.google.auth.oauth2.OAuth2Credentials#com_google_auth_oauth2_OAuth2Credentials_hasRequestMetadataOnly__) [OAuth2Credentials.hashCode()](https://docs.cloud.google.com/java/docs/reference/google-auth-library/latest/com.google.auth.oauth2.OAuth2Credentials#com_google_auth_oauth2_OAuth2Credentials_hashCode__) [OAuth2Credentials.newBuilder()](https://docs.cloud.google.com/java/docs/reference/google-auth-library/latest/com.google.auth.oauth2.OAuth2Credentials#com_google_auth_oauth2_OAuth2Credentials_newBuilder__) [OAuth2Credentials.newInstance(String)](https://docs.cloud.google.com/java/docs/reference/google-auth-library/latest/com.google.auth.oauth2.OAuth2Credentials#com_google_auth_oauth2_OAuth2Credentials_newInstance_java_lang_String_) [OAuth2Credentials.refresh()](https://docs.cloud.google.com/java/docs/reference/google-auth-library/latest/com.google.auth.oauth2.OAuth2Credentials#com_google_auth_oauth2_OAuth2Credentials_refresh__) [OAuth2Credentials.refreshAccessToken()](https://docs.cloud.google.com/java/docs/reference/google-auth-library/latest/com.google.auth.oauth2.OAuth2Credentials#com_google_auth_oauth2_OAuth2Credentials_refreshAccessToken__) [OAuth2Credentials.refreshIfExpired()](https://docs.cloud.google.com/java/docs/reference/google-auth-library/latest/com.google.auth.oauth2.OAuth2Credentials#com_google_auth_oauth2_OAuth2Credentials_refreshIfExpired__) [OAuth2Credentials.removeChangeListener(OAuth2Credentials.CredentialsChangedListener)](https://docs.cloud.google.com/java/docs/reference/google-auth-library/latest/com.google.auth.oauth2.OAuth2Credentials#com_google_auth_oauth2_OAuth2Credentials_removeChangeListener_com_google_auth_oauth2_OAuth2Credentials_CredentialsChangedListener_) [OAuth2Credentials.toBuilder()](https://docs.cloud.google.com/java/docs/reference/google-auth-library/latest/com.google.auth.oauth2.OAuth2Credentials#com_google_auth_oauth2_OAuth2Credentials_toBuilder__) [OAuth2Credentials.toString()](https://docs.cloud.google.com/java/docs/reference/google-auth-library/latest/com.google.auth.oauth2.OAuth2Credentials#com_google_auth_oauth2_OAuth2Credentials_toString__) [Object.clone()](https://docs.oracle.com/javase/8/docs/api/java/lang/Object.html#clone--) [Object.finalize()](https://docs.oracle.com/javase/8/docs/api/java/lang/Object.html#finalize--) [Object.getClass()](https://docs.oracle.com/javase/8/docs/api/java/lang/Object.html#getClass--) [Object.notify()](https://docs.oracle.com/javase/8/docs/api/java/lang/Object.html#notify--) [Object.notifyAll()](https://docs.oracle.com/javase/8/docs/api/java/lang/Object.html#notifyAll--) [Object.wait()](https://docs.oracle.com/javase/8/docs/api/java/lang/Object.html#wait--) [Object.wait(long)](https://docs.oracle.com/javase/8/docs/api/java/lang/Object.html#wait-long-) [Object.wait(long,int)](https://docs.oracle.com/javase/8/docs/api/java/lang/Object.html#wait-long-int-)

## Static Methods

### newBuilder()

    public static DownscopedCredentials.Builder newBuilder()

| **Returns** ||
|---|---|
| **Type** | **Description** |
| `https://docs.cloud.google.com/java/docs/reference/google-auth-library/latest/com.google.auth.oauth2.DownscopedCredentials.Builder` |   |

## Methods

### getCredentialAccessBoundary()

    public CredentialAccessBoundary getCredentialAccessBoundary()

| **Returns** ||
|---|---|
| **Type** | **Description** |
| `https://docs.cloud.google.com/java/docs/reference/google-auth-library/latest/com.google.auth.oauth2.CredentialAccessBoundary` |   |

### getSourceCredentials()

    public GoogleCredentials getSourceCredentials()

| **Returns** ||
|---|---|
| **Type** | **Description** |
| `https://docs.cloud.google.com/java/docs/reference/google-auth-library/latest/com.google.auth.oauth2.GoogleCredentials` |   |

### getUniverseDomain()

    public String getUniverseDomain()

Returns the universe domain for the credential.

| **Returns** ||
|---|---|
| **Type** | **Description** |
| `https://docs.oracle.com/javase/8/docs/api/java/lang/String.html` | An explicit universe domain if it was explicitly provided, otherwise the default Google universe will be returned. |

**Overrides** [Credentials.getUniverseDomain()](https://docs.cloud.google.com/java/docs/reference/google-auth-library/latest/com.google.auth.Credentials#com_google_auth_Credentials_getUniverseDomain__)

### refreshAccessToken()

    public AccessToken refreshAccessToken()

Method to refresh the access token according to the specific type of credentials.

Throws IllegalStateException if not overridden since direct use of OAuth2Credentials is only
for temporary or non-refreshing access tokens.

| **Returns** ||
|---|---|
| **Type** | **Description** |
| `https://docs.cloud.google.com/java/docs/reference/google-auth-library/latest/com.google.auth.oauth2.AccessToken` |   |

**Overrides** [OAuth2Credentials.refreshAccessToken()](https://docs.cloud.google.com/java/docs/reference/google-auth-library/latest/com.google.auth.oauth2.OAuth2Credentials#com_google_auth_oauth2_OAuth2Credentials_refreshAccessToken__)

| **Exceptions** ||
|---|---|
| **Type** | **Description** |
| `https://docs.oracle.com/javase/8/docs/api/java/io/IOException.html` |   |