Class OAuth2Credentials (1.25.0)

public class OAuth2Credentials extends Credentials

Base type for Credentials using OAuth2.

Inheritance

Object > Credentials > OAuth2Credentials

Static Methods

<T>getFromServiceLoader(Class<? extends T> clazz, T defaultInstance)

protected static T <T>getFromServiceLoader(Class<? extends T> clazz, T defaultInstance)
Parameters
Name Description
clazz Class<? extends T>
defaultInstance T
Returns
Type Description
T

<T>newInstance(String className)

protected static T <T>newInstance(String className)
Parameter
Name Description
className String
Returns
Type Description
T
Exceptions
Type Description
IOException
ClassNotFoundException

create(AccessToken accessToken)

public static OAuth2Credentials create(AccessToken accessToken)

Returns the credentials instance from the given access token.

Parameter
Name Description
accessToken AccessToken

the access token

Returns
Type Description
OAuth2Credentials

the credentials instance

newBuilder()

public static OAuth2Credentials.Builder newBuilder()
Returns
Type Description
OAuth2Credentials.Builder

Constructors

OAuth2Credentials()

protected OAuth2Credentials()

Default constructor.

OAuth2Credentials(AccessToken accessToken)

protected OAuth2Credentials(AccessToken accessToken)

Constructor with explicit access token.

Parameter
Name Description
accessToken AccessToken

initial or temporary access token

OAuth2Credentials(AccessToken accessToken, Duration refreshMargin, Duration expirationMargin)

protected OAuth2Credentials(AccessToken accessToken, Duration refreshMargin, Duration expirationMargin)
Parameters
Name Description
accessToken AccessToken
refreshMargin Duration
expirationMargin Duration

Methods

addChangeListener(OAuth2Credentials.CredentialsChangedListener listener)

public final void addChangeListener(OAuth2Credentials.CredentialsChangedListener listener)

Adds a listener that is notified when the Credentials data changes.

This is called when token content changes, such as when the access token is refreshed. This is typically used by code caching the access token.

Parameter
Name Description
listener OAuth2Credentials.CredentialsChangedListener

the listener to be added

equals(Object obj)

public boolean equals(Object obj)
Parameter
Name Description
obj Object
Returns
Type Description
boolean
Overrides

getAccessToken()

public final AccessToken getAccessToken()

Returns the cached access token.

If not set, you should call #refresh() to fetch and cache an access token.

Returns
Type Description
AccessToken

The cached access token.

getAdditionalHeaders()

protected Map<String,List<String>> getAdditionalHeaders()

Provide additional headers to return as request metadata.

Returns
Type Description
Map<String,List<String>>

additional headers

getAuthenticationType()

public String getAuthenticationType()

A constant string name describing the authentication technology.

E.g. “OAuth2”, “SSL”. For use by the transport layer to determine whether it supports the type of authentication in the case where Credentials#hasRequestMetadataOnly is false. Also serves as a debugging helper.

Returns
Type Description
String
Overrides

getRequestMetadata(URI uri)

public Map<String,List<String>> getRequestMetadata(URI uri)

Provide the request metadata by ensuring there is a current access token and providing it as an authorization bearer token.

Parameter
Name Description
uri URI
Returns
Type Description
Map<String,List<String>>
Overrides
Exceptions
Type Description
IOException

getRequestMetadata(URI uri, Executor executor, RequestMetadataCallback callback)

public void getRequestMetadata(URI uri, Executor executor, RequestMetadataCallback callback)

Get the current request metadata without blocking.

This should be called by the transport layer on each request, and the data should be populated in headers or other context. The implementation can either call the callback inline or asynchronously. Either way it should never block in this method. The executor is provided for tasks that may block.

The default implementation will just call #getRequestMetadata(URI) then the callback from the given executor.

The convention for handling binary data is for the key in the returned map to end with "-bin" and for the corresponding values to be base64 encoded.

Parameters
Name Description
uri URI
executor Executor
callback RequestMetadataCallback
Overrides

getRequestMetadataInternal()

protected Map<String,List<String>> getRequestMetadataInternal()
Returns
Type Description
Map<String,List<String>>

hasRequestMetadata()

public boolean hasRequestMetadata()

Whether the credentials have metadata entries that should be added to each request.

This should be called by the transport layer to see if Credentials#getRequestMetadata should be used for each request.

Returns
Type Description
boolean
Overrides

hasRequestMetadataOnly()

public boolean hasRequestMetadataOnly()

Indicates whether or not the Auth mechanism works purely by including request metadata.

This is meant for the transport layer. If this is true a transport does not need to take actions other than including the request metadata. If this is false, a transport must specifically know about the authentication technology to support it, and should fail to accept the credentials otherwise.

Returns
Type Description
boolean
Overrides

hashCode()

public int hashCode()
Returns
Type Description
int
Overrides

refresh()

public void refresh()

Request a new token regardless of the current token state. If the current token is not expired, it will still be returned during the refresh.

Overrides
Exceptions
Type Description
IOException

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
AccessToken

never

Exceptions
Type Description
IOException

always. OAuth2Credentials does not support refreshing the access token. An instance with a new access token or a derived type that supports refreshing should be used instead.

refreshIfExpired()

public void refreshIfExpired()

Refresh these credentials only if they have expired or are expiring imminently.

Exceptions
Type Description
IOException

during token refresh.

removeChangeListener(OAuth2Credentials.CredentialsChangedListener listener)

public final void removeChangeListener(OAuth2Credentials.CredentialsChangedListener listener)

Removes a listener that was added previously.

Parameter
Name Description
listener OAuth2Credentials.CredentialsChangedListener

The listener to be removed.

toBuilder()

public OAuth2Credentials.Builder toBuilder()
Returns
Type Description
OAuth2Credentials.Builder

toString()

public String toString()
Returns
Type Description
String
Overrides