Elasticsearch缺少REST请求的身份验证令牌

时间:2017-07-14 20:04:26

标签: python elasticsearch elasticsearch-5

我正在使用elasticsearch数据库存储我从在线提取的数据,但是当我尝试索引数据库中的数据时,我收到错误。

以下是我创建和索引数据的代码:

es = Elasticsearch()

es.index(index='weather', doc_type='data', body=doc)

然而,当我运行这个程序时,这些行中的第二行会导致错误,这里是完整的回溯:

Traceback (most recent call last):
File "weatherScraper.py", line 79, in <module>
  main()
File "weatherScraper.py", line 73, in main
  es.index(index='weather', doc_type='data', body=doc)
File "/home/alec/.local/lib/python2.7/site-packages/elasticsearch/client/utils.py", line 73, in _wrapped
  return func(*args, params=params, **kwargs)
File "/home/alec/.local/lib/python2.7/site-packages/elasticsearch/client/__init__.py", line 298, in index
  _make_path(index, doc_type, id), params=params, body=body)
File "/home/alec/.local/lib/python2.7/site-packages/elasticsearch/transport.py", line 312, in perform_request
  status, headers, data = connection.perform_request(method, url, params, body, ignore=ignore, timeout=timeout)
File "/home/alec/.local/lib/python2.7/site-packages/elasticsearch/connection/http_urllib3.py", line 128, in perform_request
  self._raise_error(response.status, raw_data)
File "/home/alec/.local/lib/python2.7/site-packages/elasticsearch/connection/base.py", line 125, in _raise_error
  raise HTTP_EXCEPTIONS.get(status_code, TransportError)(status_code, error_message, additional_info)
elasticsearch.exceptions.AuthenticationException: TransportError(401, u'security_exception', u'missing authentication token for REST request [/weather/data]')

3 个答案:

答案 0 :(得分:4)

&#39;&#39;&#39;缺少身份验证令牌&#39;意味着您需要在与此Elasticsearch实例通信之前进行身份验证。要索引文档,用户必须具有写访问权限。您可以在以下网址中添加用户名和密码:http://user:password@hostname:post

例如,在shell中:

export ES_ENDPOINT="http://usernameWithWriteAccess:password@localhost:9200"

然后在python:

es = Elasticsearch(os.environ['ES_ENDPOINT'])

答案 1 :(得分:0)

此外,如果您是通过Postman工具执行的操作,例如:

转到“授权”标签的“基本身份验证”,在此处输入通过单击elasticsearch-setup-passwords.bat收到的用户名和密码

enter image description here

答案 2 :(得分:0)

在创建ElasticSearch客户端时,可以将HTTP基本身份验证传递给item.imageUrl参数:

http_auth

这假定您正在使用具有client = Elasticsearch( hosts=['localhost:5000'], http_auth=('username', 'password'), ) s = Search(using=client, index='something') 参数的基础Urllib3HttpConnection传输类。

http_auth

使用urllib3库和http的默认连接类 协议。

参数

  • http_auth –可选的http身份验证信息,可以是“:”分隔的字符串或元组

有关SSL和其他用于身份验证的参数,请参阅文档的SSL and Authentication部分:

class elasticsearch.connection.Urllib3HttpConnection(host='localhost', 
                                                     http_auth=None, 
                                                     ...,
                                                     **kwargs)