How to specify authorization in python client?

Hi,
I am looking for a way to specify the authorization token in python requests for elasticsearch.

For example, es=Elasticsearch(['http://localhost:8080'],http_auth=('user', 'pass'))

in above example just like you specify http_auth, I want to specify the Bearer token.

Thanks.

from elasticsearch import Elasticsearch
from elasticsearch import helpers
import requests
from requests.auth import AuthBase

requests.packages.urllib3.disable_warnings()

class TokenAuth(AuthBase):

    def __init__(self, token):
        self.token = token

    def __call__(self, r):
        r.headers['Authorization :Bearer'] = f'{self.token}'  
        return r

es = Elasticsearch(['https://localhost:9200/user/type'],ca_certs=False,verify_certs=False,auth=TokenAuth('h8HeBtVlpBkblQ6RzBq'))

res = helpers.bulk(es, "ld.json", chunk_size=100, request_timeout=200)

i have this above code, for bulk indexing the data according to chunks, but when i pass the access token to the function, it throws me this error.

*Traceback (most recent call last):
File "bulk_index.py", line 20, in
res = helpers.bulk(es, "ldif2.json", chunk_size=1, request_timeout=200)
File "C:\Users\mkumaru\AppData\Local\Programs\Python\Python37\lib\site-packages\elasticsearch\helpers\actions.py", line 300, in bulk
for ok, item in streaming_bulk(client, actions, *args, **kwargs):
File "C:\Users\mkumaru\AppData\Local\Programs\Python\Python37\lib\site-packages\elasticsearch\helpers\actions.py", line 230, in streaming_bulk
**kwargs
File "C:\Users\mkumaru\AppData\Local\Programs\Python\Python37\lib\site-packages\elasticsearch\helpers\actions.py", line 116, in _process_bulk_chunk
raise e
File "C:\Users\mkumaru\AppData\Local\Programs\Python\Python37\lib\site-packages\elasticsearch\helpers\actions.py", line 112, in _process_bulk_chunk
resp = client.bulk("\n".join(bulk_actions) + "\n", *args, *kwargs)
File "C:\Users\mkumaru\AppData\Local\Programs\Python\Python37\lib\site-packages\elasticsearch\client\utils.py", line 84, in wrapped
return func(*args, params=params, **kwargs)
File "C:\Users\mkumaru\AppData\Local\Programs\Python\Python37\lib\site-packages\elasticsearch\client_init
.py", line 1498, in bulk
headers={"content-type": "application/x-ndjson"},
File "C:\Users\mkumaru\AppData\Local\Programs\Python\Python37\lib\site-packages\elasticsearch\transport.py", line 353, in perform_request
timeout=timeout,
File "C:\Users\mkumaru\AppData\Local\Programs\Python\Python37\lib\site-packages\elasticsearch\connection\http_urllib3.py", line 239, in perform_request
self._raise_error(response.status, raw_data)
File "C:\Users\mkumaru\AppData\Local\Programs\Python\Python37\lib\site-packages\elasticsearch\connection\base.py", line 168, in _raise_error
status_code, error_message, additional_info
elasticsearch.exceptions.AuthenticationException: AuthenticationException(401, 'Access denied')

from elasticsearch import Elasticsearch
from elasticsearch.helpers import bulk
from requests.auth import AuthBase
#requests.packages.urllib3.disable_warnings()
es = Elasticsearch('https://localhost:9200/user',ca_certs=False,verify_certs=False,headers={'Authorization' :'Bearer 09f03U7qJVrEj9l6kz07oM2c'})
res = bulk(es, "ldif2.json", chunk_size=2, request_timeout=200,doc_type="json")

The above code throws me following error,

elasticsearch.helpers.errors.BulkIndexError: ('2 document(s) failed to index.', [{'index': {'_index': 'user', '_type': 'json', '_id': 'u5wWu2oB44h7_wMvjq38', 'status': 400, 'error': {'type': 'mapper_parsing_exception', 'reason': 'failed to parse', 'caused_by': {'type': 'not_x_content_exception', 'reason': 'Compressor detection can only be called on some xcontent bytes or compressed xcontent bytes'}}, 'data': 'l'}}, {'index': {'_index': 'user', '_type': 'json', '_id': 'vJwWu2oB44h7_wMvjq38', 'status': 400, 'error': {'type': 'mapper_parsing_exception', 'reason': 'failed to parse', 'caused_by': {'type': 'not_x_content_exception', 'reason': 'Compressor detection can only be called on some xcontent bytes or compressed xcontent bytes'}}, 'data': 'd'}}])

can you tell me whats wrong with the code

Any body knows whats wrong with above code

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