# How to specify authorization in python client?

**URL:** <https://discuss.elastic.co/t/how-to-specify-authorization-in-python-client/181123>\
**Category:** Elasticsearch\
**Created:** [May 15, 2019, 8:35am UTC](https://discuss.elastic.co/t/how-to-specify-authorization-in-python-client/181123 "2019-05-15T08:35:58Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![MKU](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/mku/32/48327_2.png) [@MKU](https://discuss.elastic.co/u/MKU)\
**Post date:** [May 15, 2019, 8:35am UTC](https://discuss.elastic.co/t/how-to-specify-authorization-in-python-client/181123/1 "2019-05-15T08:35:58Z")

</div>

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://localhost:8080/)'],http\_auth=('user', 'pass'))**

in above example just like you specify **http\_auth,** I want to specify the **Bearer token.**

Thanks.

---

<div class="post-metadata">

**Author:** ![MKU](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/mku/32/48327_2.png) [@MKU](https://discuss.elastic.co/u/MKU)\
**Post date:** [May 15, 2019, 9:26am UTC](https://discuss.elastic.co/t/how-to-specify-authorization-in-python-client/181123/2 "2019-05-15T09:26:54Z")

</div>

```
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')_

---

<div class="post-metadata">

**Author:** ![MKU](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/mku/32/48327_2.png) [@MKU](https://discuss.elastic.co/u/MKU)\
**Post date:** [May 15, 2019, 10:51am UTC](https://discuss.elastic.co/t/how-to-specify-authorization-in-python-client/181123/3 "2019-05-15T10:51:07Z")

</div>

```auto
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

---

<div class="post-metadata">

**Author:** ![MKU](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/mku/32/48327_2.png) [@MKU](https://discuss.elastic.co/u/MKU)\
**Post date:** [May 16, 2019, 5:35am UTC](https://discuss.elastic.co/t/how-to-specify-authorization-in-python-client/181123/4 "2019-05-16T05:35:57Z")

</div>

Any body knows whats wrong with above code

---

<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 13, 2019, 5:36am UTC](https://discuss.elastic.co/t/how-to-specify-authorization-in-python-client/181123/5 "2019-06-13T05:36:02Z")

</div>

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