# Token based authentication(oauth2) using RestHighLevelClient

**URL:** https://discuss.elastic.co/t/token-based-authentication-oauth2-using-resthighlevelclient/181574
**Category:** Elasticsearch
**Tags:** elastic-stack-security
**Created:** [May 17, 2019, 11:11am UTC](https://discuss.elastic.co/t/token-based-authentication-oauth2-using-resthighlevelclient/181574 "2019-05-17T11:11:09Z")
**Posts on this page:** 3
**Page:** 1

<div class="post-metadata">

### Author: ![dhrchatt](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/dhrchatt/32/46374_2.png) [@dhrchatt](https://discuss.elastic.co/u/dhrchatt)
#### Post date: [May 17, 2019, 11:11am UTC](https://discuss.elastic.co/t/token-based-authentication-oauth2-using-resthighlevelclient/181574/1 "2019-05-17T11:11:09Z")

</div>

I have ES 7.0.0 with x-pack and an application using java RestHighLevelClient to connect to it. I am looking for way to setup token based authentication(oauth2) with this client but not able to find any API to set the token in the request after getting it. Can you please give an example (sample code) on how this can be achieved using RestHighLevelClient API.

---

<div class="post-metadata">

### Author: ![ikakavas](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/ikakavas/32/34430_2.png) [@ikakavas](https://discuss.elastic.co/u/ikakavas)
#### Post date: [May 20, 2019, 6:32am UTC](https://discuss.elastic.co/t/token-based-authentication-oauth2-using-resthighlevelclient/181574/2 "2019-05-20T06:32:41Z")

</div>

Hi there,

You can use [`RequestOptions`](https://www.elastic.co/guide/en/elasticsearch/client/java-rest/7.0/java-rest-high-getting-started-request-options.html) to set any headers in the request (see [here](https://www.elastic.co/guide/en/elasticsearch/client/java-rest/7.0/java-rest-low-usage-requests.html#java-rest-low-usage-request-options) for an example that actually sets the `Authorization` header with `Bearer <token_value>` which is exactly what you want )

---

<div class="post-metadata">

### Author: ![system](https://us1.discourse-cdn.com/elastic/original/3X/1/a/1ac57faf039f6b580b3f104ef42a2a89e41014de.png) [@system](https://discuss.elastic.co/u/system)
#### Post date: [June 17, 2019, 6:32am UTC](https://discuss.elastic.co/t/token-based-authentication-oauth2-using-resthighlevelclient/181574/3 "2019-06-17T06:32:43Z")

</div>

This topic was automatically closed 28 days after the last reply. New replies are no longer allowed.
