WARNING: Deprecated in 7.15.0.
The Java REST Client is deprecated in favor of the Java API Client.
Create Token API
editCreate Token API
editRequest
editThe CreateTokenRequest
supports three different OAuth2 grant types:
Password Grants
editfinal char[] password = new char[]{'t', 'e', 's', 't', '-', 'u', 's', 'e', 'r', '-', 'p', 'a', 's', 's', 'w', 'o', 'r', 'd'}; CreateTokenRequest createTokenRequest = CreateTokenRequest.passwordGrant("token_user", password);
Refresh Token Grants
editcreateTokenRequest = CreateTokenRequest.refreshTokenGrant(refreshToken);
Client Credential Grants
editCreateTokenRequest createTokenRequest = CreateTokenRequest.clientCredentialsGrant();
Execution
editCreating a OAuth2 security token can be performed by passing the appropriate request to the
security().createToken()
method:
CreateTokenResponse createTokenResponse = client.security().createToken(createTokenRequest, RequestOptions.DEFAULT);
Response
editThe returned CreateTokenResponse
contains the following properties:
-
accessToken
- This is the newly created access token. It can be used to authenticate to the Elasticsearch cluster.
-
type
-
The type of the token, this is always
"Bearer"
. -
expiresIn
- The length of time until the token will expire. The token will be considered invalid after that time.
-
scope
-
The scope of the token. May be
null
. -
refreshToken
-
A secondary "refresh" token that may be used to extend
the life of an access token. May be
null
. -
authentication
- This is the authentication object for the newly created token. See also authenticate response for details.
Asynchronous Execution
editThis request can be executed asynchronously using the security().createTokenAsync()
method:
The asynchronous method does not block and returns immediately. Once the request
has completed the ActionListener
is called back using the onResponse
method
if the execution successfully completed or using the onFailure
method if
it failed.
A typical listener for a CreateTokenResponse
looks like: