| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586 | [role="xpack"][[java-rest-high-security-create-token]]=== Create Token API[[java-rest-high-security-create-token-request]]==== RequestThe `CreateTokenRequest` supports three different OAuth2 _grant types_:===== Password Grants["source","java",subs="attributes,callouts,macros"]--------------------------------------------------include-tagged::{doc-tests}/SecurityDocumentationIT.java[create-token-password-request]--------------------------------------------------===== Refresh Token Grants["source","java",subs="attributes,callouts,macros"]--------------------------------------------------include-tagged::{doc-tests}/SecurityDocumentationIT.java[create-token-refresh-request]--------------------------------------------------===== Client Credential Grants["source","java",subs="attributes,callouts,macros"]--------------------------------------------------include-tagged::{doc-tests}/SecurityDocumentationIT.java[create-token-client-credentials-request]--------------------------------------------------[[java-rest-high-security-create-token-execution]]==== ExecutionCreating a OAuth2 security token can be performed by passing the appropriate request to the `security().createToken()` method:["source","java",subs="attributes,callouts,macros"]--------------------------------------------------include-tagged::{doc-tests}/SecurityDocumentationIT.java[create-token-execute]--------------------------------------------------[[java-rest-high-security-create-token-response]]==== ResponseThe 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`.["source","java",subs="attributes,callouts,macros"]--------------------------------------------------include-tagged::{doc-tests}/SecurityDocumentationIT.java[create-token-response]--------------------------------------------------<1> The `accessToken` can be used to authentication to Elasticsearch.<2> The `refreshToken` can be used in to create a new `CreateTokenRequest` with a `refresh_token` grant.[[java-rest-high-security-create-token-async]]==== Asynchronous ExecutionThis request can be executed asynchronously using the `security().createTokenAsync()`method:["source","java",subs="attributes,callouts,macros"]--------------------------------------------------include-tagged::{doc-tests}/SecurityDocumentationIT.java[create-token-execute-async]--------------------------------------------------<1> The `CreateTokenRequest` to execute and the `ActionListener` to use whenthe execution completesThe asynchronous method does not block and returns immediately. Once the requesthas completed the `ActionListener` is called back using the `onResponse` methodif the execution successfully completed or using the `onFailure` method ifit failed.A typical listener for a `CreateTokenResponse` looks like:["source","java",subs="attributes,callouts,macros"]--------------------------------------------------include-tagged::{doc-tests}/SecurityDocumentationIT.java[create-token-execute-listener]--------------------------------------------------<1> Called when the execution is successfully completed. The response isprovided as an argument<2> Called in case of failure. The raised exception is provided as an argument
 |