elasticsearch-java icon indicating copy to clipboard operation
elasticsearch-java copied to clipboard

Elasticsearch token service demo code not working

Open liu-xiao-guo opened this issue 2 years ago • 1 comments

https://www.elastic.co/guide/en/elasticsearch/client/java-api-client/current/_other_authentication_methods.html. The following code:

RestClientBuilder builder = RestClient.builder( new HttpHost("localhost", 9200, "http")); Header[] defaultHeaders = new Header[]{new BasicHeader("Authorization", "Bearer u6iuAxZ0RG1Kcm5jVFI4eU4tZU9aVFEwT2F3")}; builder.setDefaultHeaders(defaultHeaders);

It should be:

RestClientBuilder builder = RestClient.builder( new HttpHost("localhost", 9200, "http")); Header[] defaultHeaders = new Header[]{new BasicHeader("Authorization", "ApiKey u6iuAxZ0RG1Kcm5jVFI4eU4tZU9aVFEwT2F3")}; builder.setDefaultHeaders(defaultHeaders);

The "Bearer" is wrong according to the following code on the same page:

String apiKeyId = "uqlEyn8B_gQ_jlvwDIvM"; String apiKeySecret = "HxHWk2m4RN-V_qg9cDpuX"; String apiKeyAuth = Base64.getEncoder().encodeToString( (apiKeyId + ":" + apiKeySecret) .getBytes(StandardCharsets.UTF_8)); RestClientBuilder builder = RestClient.builder( new HttpHost("localhost", 9200, "http")); Header[] defaultHeaders = new Header[]{new BasicHeader("Authorization", "ApiKey " + apiKeyAuth)}; builder.setDefaultHeaders(defaultHeaders);

It should be ApiKey.

Thanks & best regards, Xiaoguo image

liu-xiao-guo avatar Apr 18 '22 03:04 liu-xiao-guo

Thanks, @liu-xiao-guo . I'm going to transfer this to the Java client library for the developers to take a look at.

gtback avatar Apr 18 '22 13:04 gtback