Class OAuth2Credentials (1.18.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
NameDescription
clazzClass<? extends T>
defaultInstanceT
Returns
TypeDescription
T

<T>newInstance(String className)

protected static T <T>newInstance(String className)
Parameter
NameDescription
classNameString
Returns
TypeDescription
T
Exceptions
TypeDescription
IOException
ClassNotFoundException

create(AccessToken accessToken)

public static OAuth2Credentials create(AccessToken accessToken)

Returns the credentials instance from the given access token.

Parameter
NameDescription
accessTokenAccessToken

the access token

Returns
TypeDescription
OAuth2Credentials

the credentials instance

newBuilder()

public static OAuth2Credentials.Builder newBuilder()
Returns
TypeDescription
OAuth2Credentials.Builder

Constructors

OAuth2Credentials()

protected OAuth2Credentials()

Default constructor.

OAuth2Credentials(AccessToken accessToken)

protected OAuth2Credentials(AccessToken accessToken)

Constructor with explicit access token.

Parameter
NameDescription
accessTokenAccessToken

initial or temporary access token

OAuth2Credentials(AccessToken accessToken, Duration refreshMargin, Duration expirationMargin)

protected OAuth2Credentials(AccessToken accessToken, Duration refreshMargin, Duration expirationMargin)
Parameters
NameDescription
accessTokenAccessToken
refreshMarginDuration
expirationMarginDuration

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
NameDescription
listenerOAuth2Credentials.CredentialsChangedListener

the listener to be added

equals(Object obj)

public boolean equals(Object obj)
Parameter
NameDescription
objObject
Returns
TypeDescription
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
TypeDescription
AccessToken

The cached access token.

getAdditionalHeaders()

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

Provide additional headers to return as request metadata.

Returns
TypeDescription
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
TypeDescription
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
NameDescription
uriURI
Returns
TypeDescription
Map<String,List<String>>
Overrides
Exceptions
TypeDescription
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
NameDescription
uriURI
executorExecutor
callbackRequestMetadataCallback
Overrides

getRequestMetadataInternal()

protected Map<String,List<String>> getRequestMetadataInternal()
Returns
TypeDescription
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
TypeDescription
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
TypeDescription
boolean
Overrides

hashCode()

public int hashCode()
Returns
TypeDescription
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
TypeDescription
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
TypeDescription
AccessToken

never

Exceptions
TypeDescription
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
TypeDescription
IOException

during token refresh.

removeChangeListener(OAuth2Credentials.CredentialsChangedListener listener)

public final void removeChangeListener(OAuth2Credentials.CredentialsChangedListener listener)

Removes a listener that was added previously.

Parameter
NameDescription
listenerOAuth2Credentials.CredentialsChangedListener

The listener to be removed.

toBuilder()

public OAuth2Credentials.Builder toBuilder()
Returns
TypeDescription
OAuth2Credentials.Builder

toString()

public String toString()
Returns
TypeDescription
String
Overrides