# Class Credential (1.39.0)

<button class="button button-with-icon cloud-select-dropdown__button " aria-haspopup="true">Version latest</button>

- [1.39.0 (latest)](https://docs.cloud.google.com/java/docs/reference/google-oauth-client/latest/com.google.api.client.auth.oauth2.Credential)
- [1.38.2](https://docs.cloud.google.com/java/docs/reference/google-oauth-client/1.38.2/com.google.api.client.auth.oauth2.Credential)
- [1.37.0](https://docs.cloud.google.com/java/docs/reference/google-oauth-client/1.37.0/com.google.api.client.auth.oauth2.Credential)
- [1.36.0](https://docs.cloud.google.com/java/docs/reference/google-oauth-client/1.36.0/com.google.api.client.auth.oauth2.Credential)
- [1.34.1](https://docs.cloud.google.com/java/docs/reference/google-oauth-client/1.34.1/com.google.api.client.auth.oauth2.Credential)
- [1.33.3](https://docs.cloud.google.com/java/docs/reference/google-oauth-client/1.33.3/com.google.api.client.auth.oauth2.Credential)
- [1.32.1](https://docs.cloud.google.com/java/docs/reference/google-oauth-client/1.32.1/com.google.api.client.auth.oauth2.Credential)

    public class Credential implements HttpExecuteInterceptor, HttpRequestInitializer, HttpUnsuccessfulResponseHandler

Thread-safe OAuth 2.0 helper for accessing protected resources using an access token, as well as
optionally refreshing the access token when it expires using a refresh token.

Sample usage:


     public static Credential createCredentialWithAccessTokenOnly(
     HttpTransport transport, JsonFactory jsonFactory, TokenResponse tokenResponse) {
     return new Credential(BearerToken.authorizationHeaderAccessMethod()).setFromTokenResponse(
     tokenResponse);
     }

     public static Credential createCredentialWithRefreshToken(
     HttpTransport transport, JsonFactory jsonFactory, TokenResponse tokenResponse) {
     return new Credential.Builder(BearerToken.authorizationHeaderAccessMethod()).setTransport(
     transport)
     .setJsonFactory(jsonFactory)
     .setTokenServerUrl(
     new GenericUrl("https://server.example.com/token"))
     .setClientAuthentication(new BasicAuthentication("s6BhdRkqt3", "7Fjfp0ZBr1KtDRbnfVdmIw"))
     .build()
     .setFromTokenResponse(tokenResponse);
     }
     
If you need to persist the access token in a data store, use DataStoreFactory and
Builder#addRefreshListener(CredentialRefreshListener) with [DataStoreCredentialRefreshListener](https://docs.cloud.google.com/java/docs/reference/google-oauth-client/latest/com.google.api.client.auth.oauth2.DataStoreCredentialRefreshListener).

If you have a custom request initializer, request execute interceptor, or unsuccessful
response handler, take a look at the sample usage for HttpExecuteInterceptor and HttpUnsuccessfulResponseHandler, which are interfaces that this class also implements.

## Inheritance

[java.lang.Object](https://docs.oracle.com/javase/8/docs/api/java/lang/Object.html) \> Credential

## Implements

com.google.api.client.http.HttpExecuteInterceptor, com.google.api.client.http.HttpRequestInitializer, com.google.api.client.http.HttpUnsuccessfulResponseHandler

## Inherited Members

[Object.clone()](https://docs.oracle.com/javase/8/docs/api/java/lang/Object.html#clone--) [Object.equals(Object)](https://docs.oracle.com/javase/8/docs/api/java/lang/Object.html#equals-java.lang.Object-) [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.hashCode()](https://docs.oracle.com/javase/8/docs/api/java/lang/Object.html#hashCode--) [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.toString()](https://docs.oracle.com/javase/8/docs/api/java/lang/Object.html#toString--) [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-)

## Constructors

### Credential(Credential.AccessMethod method)

    public Credential(Credential.AccessMethod method)

Constructor with the ability to access protected resources, but not refresh tokens.

To use with the ability to refresh tokens, use Builder.

| **Parameter** ||
|---|---|
| **Name** | **Description** |
| `method` | `https://docs.cloud.google.com/java/docs/reference/google-oauth-client/latest/com.google.api.client.auth.oauth2.Credential.AccessMethod` method of presenting the access token to the resource server (for example BearerToken.AuthorizationHeaderAccessMethod) |

### Credential(Credential.Builder builder)

    protected Credential(Credential.Builder builder)

| **Parameter** ||
|---|---|
| **Name** | **Description** |
| `builder` | `https://docs.cloud.google.com/java/docs/reference/google-oauth-client/latest/com.google.api.client.auth.oauth2.Credential.Builder` credential builder |

## Methods

### executeRefreshToken()

    protected TokenResponse executeRefreshToken()

Executes a request for new credentials from the token server.

The default implementation calls RefreshTokenRequest#execute() using the [#getTransport()](https://docs.cloud.google.com/java/docs/reference/google-oauth-client/latest/com.google.api.client.auth.oauth2.Credential#com_google_api_client_auth_oauth2_Credential_getTransport__), [#getJsonFactory()](https://docs.cloud.google.com/java/docs/reference/google-oauth-client/latest/com.google.api.client.auth.oauth2.Credential#com_google_api_client_auth_oauth2_Credential_getJsonFactory__), [#getRequestInitializer()](https://docs.cloud.google.com/java/docs/reference/google-oauth-client/latest/com.google.api.client.auth.oauth2.Credential#com_google_api_client_auth_oauth2_Credential_getRequestInitializer__), [#getTokenServerEncodedUrl()](https://docs.cloud.google.com/java/docs/reference/google-oauth-client/latest/com.google.api.client.auth.oauth2.Credential#com_google_api_client_auth_oauth2_Credential_getTokenServerEncodedUrl__), [#getRefreshToken()](https://docs.cloud.google.com/java/docs/reference/google-oauth-client/latest/com.google.api.client.auth.oauth2.Credential#com_google_api_client_auth_oauth2_Credential_getRefreshToken__), and the [#getClientAuthentication()](https://docs.cloud.google.com/java/docs/reference/google-oauth-client/latest/com.google.api.client.auth.oauth2.Credential#com_google_api_client_auth_oauth2_Credential_getClientAuthentication__). If [#getRefreshToken()](https://docs.cloud.google.com/java/docs/reference/google-oauth-client/latest/com.google.api.client.auth.oauth2.Credential#com_google_api_client_auth_oauth2_Credential_getRefreshToken__) is `null`, it instead returns
`null`.

Subclasses may override for a different implementation. Implementations can assume proper
thread synchronization is already taken care of inside [#refreshToken()](https://docs.cloud.google.com/java/docs/reference/google-oauth-client/latest/com.google.api.client.auth.oauth2.Credential#com_google_api_client_auth_oauth2_Credential_refreshToken__).

| **Returns** ||
|---|---|
| **Type** | **Description** |
| `https://docs.cloud.google.com/java/docs/reference/google-oauth-client/latest/com.google.api.client.auth.oauth2.TokenResponse` | successful response from the token server or `null` if it is not possible to refresh the access token |

| **Exceptions** ||
|---|---|
| **Type** | **Description** |
| `https://docs.oracle.com/javase/8/docs/api/java/io/IOException.html` | if an error response was received from the token server |

### getAccessToken()

    public final String getAccessToken()

Returns the access token or `null` for none. If `null` the token needs to be
refreshed using refreshToken().

| **Returns** ||
|---|---|
| **Type** | **Description** |
| `https://docs.oracle.com/javase/8/docs/api/java/lang/String.html` |   |

### getClientAuthentication()

    public final HttpExecuteInterceptor getClientAuthentication()

Returns the client authentication or `null` for none.

| **Returns** ||
|---|---|
| **Type** | **Description** |
| `com.google.api.client.http.HttpExecuteInterceptor` |   |

### getClock()

    public final Clock getClock()

Returns the clock used for expiration checks by this Credential. Mostly used for unit-testing.

| **Returns** ||
|---|---|
| **Type** | **Description** |
| `com.google.api.client.util.Clock` |   |

### getExpirationTimeMilliseconds()

    public final Long getExpirationTimeMilliseconds()

Expected expiration time in milliseconds relative to the Java epoch, or `null` for none.

| **Returns** ||
|---|---|
| **Type** | **Description** |
| `https://docs.oracle.com/javase/8/docs/api/java/lang/Long.html` |   |

### getExpiresInSeconds()

    public final Long getExpiresInSeconds()

Returns the remaining lifetime in seconds of the access token (for example 3600 for an hour
from now, or -3600 if expired an hour ago) or `null` if unknown.

| **Returns** ||
|---|---|
| **Type** | **Description** |
| `https://docs.oracle.com/javase/8/docs/api/java/lang/Long.html` |   |

### getJsonFactory()

    public final JsonFactory getJsonFactory()

Returns the JSON factory to use for parsing response for refresh token request or `null`
for none.

| **Returns** ||
|---|---|
| **Type** | **Description** |
| `com.google.api.client.json.JsonFactory` |   |

### getMethod()

    public final Credential.AccessMethod getMethod()

Return the method of presenting the access token to the resource server (for example BearerToken.AuthorizationHeaderAccessMethod).

| **Returns** ||
|---|---|
| **Type** | **Description** |
| `https://docs.cloud.google.com/java/docs/reference/google-oauth-client/latest/com.google.api.client.auth.oauth2.Credential.AccessMethod` |   |

### getRefreshListeners()

    public final Collection<CredentialRefreshListener> getRefreshListeners()

Returns the unmodifiable collection of listeners for refresh token results.

| **Returns** ||
|---|---|
| **Type** | **Description** |
| `https://docs.oracle.com/javase/8/docs/api/java/util/Collection.html<https://docs.cloud.google.com/java/docs/reference/google-oauth-client/latest/com.google.api.client.auth.oauth2.CredentialRefreshListener>` |   |

### getRefreshToken()

    public final String getRefreshToken()

Returns the refresh token associated with the access token to be refreshed or `null` for
none.

| **Returns** ||
|---|---|
| **Type** | **Description** |
| `https://docs.oracle.com/javase/8/docs/api/java/lang/String.html` |   |

### getRequestInitializer()

    public final HttpRequestInitializer getRequestInitializer()

Returns the HTTP request initializer for refresh token requests to the token server or `
null` for none.

| **Returns** ||
|---|---|
| **Type** | **Description** |
| `com.google.api.client.http.HttpRequestInitializer` |   |

### getTokenServerEncodedUrl()

    public final String getTokenServerEncodedUrl()

Returns the encoded authorization server URL or `null` for none.

| **Returns** ||
|---|---|
| **Type** | **Description** |
| `https://docs.oracle.com/javase/8/docs/api/java/lang/String.html` |   |

### getTransport()

    public final HttpTransport getTransport()

Return the HTTP transport for executing refresh token request or `null` for none.

| **Returns** ||
|---|---|
| **Type** | **Description** |
| `com.google.api.client.http.HttpTransport` |   |

### handleResponse(HttpRequest request, HttpResponse response, boolean supportsRetry)

    public boolean handleResponse(HttpRequest request, HttpResponse response, boolean supportsRetry)

Default implementation checks if `WWW-Authenticate` exists and contains a "Bearer"
value (see [rfc6750 section 3.1](http://tools.ietf.org/html/rfc6750#section-3.1) for
more details). If so, it calls [#refreshToken](https://docs.cloud.google.com/java/docs/reference/google-oauth-client/latest/com.google.api.client.auth.oauth2.Credential#com_google_api_client_auth_oauth2_Credential_refreshToken_) in case the error code contains `
invalid_token`. If there is no "Bearer" in `WWW-Authenticate` and the status code is
HttpStatusCodes#STATUS_CODE_UNAUTHORIZED it calls [#refreshToken](https://docs.cloud.google.com/java/docs/reference/google-oauth-client/latest/com.google.api.client.auth.oauth2.Credential#com_google_api_client_auth_oauth2_Credential_refreshToken_). If [#executeRefreshToken()](https://docs.cloud.google.com/java/docs/reference/google-oauth-client/latest/com.google.api.client.auth.oauth2.Credential#com_google_api_client_auth_oauth2_Credential_executeRefreshToken__) throws an I/O exception, this implementation will log the exception and
return `false`. Subclasses may override.

| **Parameters** ||
|---|---|
| **Name** | **Description** |
| `request` | `com.google.api.client.http.HttpRequest` |
| `response` | `com.google.api.client.http.HttpResponse` |
| `supportsRetry` | `https://docs.oracle.com/javase/tutorial/java/nutsandbolts/datatypes.html` |

| **Returns** ||
|---|---|
| **Type** | **Description** |
| `https://docs.oracle.com/javase/tutorial/java/nutsandbolts/datatypes.html` |   |

### initialize(HttpRequest request)

    public void initialize(HttpRequest request)

| **Parameter** ||
|---|---|
| **Name** | **Description** |
| `request` | `com.google.api.client.http.HttpRequest` |

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

### intercept(HttpRequest request)

    public void intercept(HttpRequest request)

Default implementation is to try to refresh the access token if there is no access token or
if we are 1 minute away from expiration. If token server is unavailable, it will try to use the
access token even if has expired. If a 4xx error is encountered while refreshing the token,
[TokenResponseException](https://docs.cloud.google.com/java/docs/reference/google-oauth-client/latest/com.google.api.client.auth.oauth2.TokenResponseException) is thrown. If successful, it will call [#getMethod()](https://docs.cloud.google.com/java/docs/reference/google-oauth-client/latest/com.google.api.client.auth.oauth2.Credential#com_google_api_client_auth_oauth2_Credential_getMethod__) and
AccessMethod#intercept.

Subclasses may override.

| **Parameter** ||
|---|---|
| **Name** | **Description** |
| `request` | `com.google.api.client.http.HttpRequest` |

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

### refreshToken()

    public final boolean refreshToken()

Request a new access token from the authorization endpoint.

On success, it will call [#setFromTokenResponse(TokenResponse)](https://docs.cloud.google.com/java/docs/reference/google-oauth-client/latest/com.google.api.client.auth.oauth2.Credential#com_google_api_client_auth_oauth2_Credential_setFromTokenResponse_com_google_api_client_auth_oauth2_TokenResponse_), call [CredentialRefreshListener#onTokenResponse](https://docs.cloud.google.com/java/docs/reference/google-oauth-client/latest/com.google.api.client.auth.oauth2.CredentialRefreshListener#com_google_api_client_auth_oauth2_CredentialRefreshListener_onTokenResponse_) with the token response, and return `true`. On
error, it will call [#setAccessToken(String)](https://docs.cloud.google.com/java/docs/reference/google-oauth-client/latest/com.google.api.client.auth.oauth2.Credential#com_google_api_client_auth_oauth2_Credential_setAccessToken_java_lang_String_) and [#setExpiresInSeconds(Long)](https://docs.cloud.google.com/java/docs/reference/google-oauth-client/latest/com.google.api.client.auth.oauth2.Credential#com_google_api_client_auth_oauth2_Credential_setExpiresInSeconds_java_lang_Long_) with
`null`, call [CredentialRefreshListener#onTokenErrorResponse](https://docs.cloud.google.com/java/docs/reference/google-oauth-client/latest/com.google.api.client.auth.oauth2.CredentialRefreshListener#com_google_api_client_auth_oauth2_CredentialRefreshListener_onTokenErrorResponse_) with the token error
response, and return `false`. If a 4xx error is encountered while refreshing the token,
[TokenResponseException](https://docs.cloud.google.com/java/docs/reference/google-oauth-client/latest/com.google.api.client.auth.oauth2.TokenResponseException) is thrown.

If there is no refresh token, it will quietly return `false`.

| **Returns** ||
|---|---|
| **Type** | **Description** |
| `https://docs.oracle.com/javase/tutorial/java/nutsandbolts/datatypes.html` | whether a new access token was successfully retrieved |

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

### setAccessToken(String accessToken)

    public Credential setAccessToken(String accessToken)

Sets the access token.

Overriding is only supported for the purpose of calling the super implementation and
changing the return type, but nothing else.

| **Parameter** ||
|---|---|
| **Name** | **Description** |
| `accessToken` | `https://docs.oracle.com/javase/8/docs/api/java/lang/String.html` access token or `null` for none |

| **Returns** ||
|---|---|
| **Type** | **Description** |
| `https://docs.cloud.google.com/java/docs/reference/google-oauth-client/latest/com.google.api.client.auth.oauth2.Credential` |   |

### setExpirationTimeMilliseconds(Long expirationTimeMilliseconds)

    public Credential setExpirationTimeMilliseconds(Long expirationTimeMilliseconds)

Sets the expected expiration time in milliseconds relative to the Java epoch, or `null` for none.

Overriding is only supported for the purpose of calling the super implementation and
changing the return type, but nothing else.

| **Parameter** ||
|---|---|
| **Name** | **Description** |
| `expirationTimeMilliseconds` | `https://docs.oracle.com/javase/8/docs/api/java/lang/Long.html` |

| **Returns** ||
|---|---|
| **Type** | **Description** |
| `https://docs.cloud.google.com/java/docs/reference/google-oauth-client/latest/com.google.api.client.auth.oauth2.Credential` |   |

### setExpiresInSeconds(Long expiresIn)

    public Credential setExpiresInSeconds(Long expiresIn)

Sets the lifetime in seconds of the access token (for example 3600 for an hour from now) or
`null` for none.

Overriding is only supported for the purpose of calling the super implementation and
changing the return type, but nothing else.

| **Parameter** ||
|---|---|
| **Name** | **Description** |
| `expiresIn` | `https://docs.oracle.com/javase/8/docs/api/java/lang/Long.html` lifetime in seconds of the access token (for example 3600 for an hour from now) or `null` for none |

| **Returns** ||
|---|---|
| **Type** | **Description** |
| `https://docs.cloud.google.com/java/docs/reference/google-oauth-client/latest/com.google.api.client.auth.oauth2.Credential` |   |

### setFromTokenResponse(TokenResponse tokenResponse)

    public Credential setFromTokenResponse(TokenResponse tokenResponse)

Sets the [access token](https://docs.cloud.google.com/java/docs/reference/google-oauth-client/latest/com.google.api.client.auth.oauth2.Credential#com_google_api_client_auth_oauth2_Credential_setAccessToken_), [refresh token](https://docs.cloud.google.com/java/docs/reference/google-oauth-client/latest/com.google.api.client.auth.oauth2.Credential#com_google_api_client_auth_oauth2_Credential_setRefreshToken_) (if
available), and [expires-in time](https://docs.cloud.google.com/java/docs/reference/google-oauth-client/latest/com.google.api.client.auth.oauth2.Credential#com_google_api_client_auth_oauth2_Credential_setExpiresInSeconds_) based on the values from the token
response.

It does not call the refresh listeners.

Overriding is only supported for the purpose of calling the super implementation and
changing the return type, but nothing else.

| **Parameter** ||
|---|---|
| **Name** | **Description** |
| `tokenResponse` | `https://docs.cloud.google.com/java/docs/reference/google-oauth-client/latest/com.google.api.client.auth.oauth2.TokenResponse` successful token response |

| **Returns** ||
|---|---|
| **Type** | **Description** |
| `https://docs.cloud.google.com/java/docs/reference/google-oauth-client/latest/com.google.api.client.auth.oauth2.Credential` |   |

### setRefreshToken(String refreshToken)

    public Credential setRefreshToken(String refreshToken)

Sets the refresh token.

Overriding is only supported for the purpose of calling the super implementation and
changing the return type, but nothing else.

| **Parameter** ||
|---|---|
| **Name** | **Description** |
| `refreshToken` | `https://docs.oracle.com/javase/8/docs/api/java/lang/String.html` refresh token or `null` for none |

| **Returns** ||
|---|---|
| **Type** | **Description** |
| `https://docs.cloud.google.com/java/docs/reference/google-oauth-client/latest/com.google.api.client.auth.oauth2.Credential` |   |