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

Pro*_*ill 1 python elasticsearch elasticsearch-5

我正在使用Elasticsearch数据库存储从网上提取的数据,但是当我尝试对数据库中的数据建立索引时,出现错误。

这是我用于创建和索引数据的代码:

es = Elasticsearch()

es.index(index='weather', doc_type='data', body=doc)
Run Code Online (Sandbox Code Playgroud)

但是,当我运行该程序时,其中的第二行会导致错误,这是完整的回溯:

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]')
Run Code Online (Sandbox Code Playgroud)

Gin*_*pin 8

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

\n
client = Elasticsearch(\n    hosts=[\'localhost:5000\'],\n    http_auth=(\'username\', \'password\'),\n)\ns = Search(using=client, index=\'something\')\n
Run Code Online (Sandbox Code Playgroud)\n

这假设您正在使用Urllib3HttpConnection具有参数的底层传输类http_auth

\n
\n
class elasticsearch.connection.Urllib3HttpConnection(host=\'localhost\', \n                                                     http_auth=None, \n                                                     ...,\n                                                     **kwargs) \n
Run Code Online (Sandbox Code Playgroud)\n

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

\n

参数

\n
    \n
  • http_auth \xe2\x80\x93 可选的 http 身份验证信息,作为 \xe2\x80\x98:\xe2\x80\x99 分隔的字符串或元组
  • \n
\n
\n

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

\n
from ssl import create_default_context\n\ncontext = create_default_context(cafile="path/to/cert.pem")\nes = Elasticsearch(\n    [\'localhost\', \'otherhost\'],\n    http_auth=(\'user\', \'secret\'),\n    scheme="https",\n    port=443,\n    ssl_context=context,\n)\n
Run Code Online (Sandbox Code Playgroud)\n


kie*_*lni 6

``缺少身份验证令牌''表示与该Elasticsearch实例进行对话之前需要进行身份验证。要为文档建立索引,用户必须具有写权限。您可以在如下所示的URL中包含用户名和密码:http:// user:password @ hostname:post

例如,在外壳中:

export ES_ENDPOINT="http://usernameWithWriteAccess:password@localhost:9200"
Run Code Online (Sandbox Code Playgroud)

然后在python中:

es = Elasticsearch(os.environ['ES_ENDPOINT'])
Run Code Online (Sandbox Code Playgroud)