我正在使用一个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]')发布于 2017-07-16 00:47:05
“缺少身份验证令牌”意味着您需要先进行身份验证,然后才能与此Elasticsearch实例通信。要为文档编制索引,用户必须具有写访问权限。您可以在URL中包含用户名和密码,如下所示:http://user:password@hostname:port
例如,在shell中:
export ES_ENDPOINT="http://usernameWithWriteAccess:password@localhost:9200"然后在python中:
es = Elasticsearch(os.environ['ES_ENDPOINT'])发布于 2020-08-05 15:06:46
此外,如果您使用Postman工具执行此操作,例如:
单击elasticsearch-setup-passwords.bat,转至Authorization选项卡,Basic Auth,在此处写入您收到的用户名和密码

发布于 2020-11-30 11:20:28
在创建ElasticSearch客户端时,可以将HTTP basic auth传递给http_auth参数:
client = Elasticsearch(
hosts=['localhost:5000'],
http_auth=('username', 'password'),
)
s = Search(using=client, index='something')这里假设您使用的是具有http_auth参数的底层Urllib3HttpConnection传输类。
elasticsearch.connection.Urllib3HttpConnection(host='localhost',
类http_auth=None,...,**kwargs)
使用urllib3库和http协议的默认连接类。
参数:
有关SSL和其他身份验证参数,请参阅文档的SSL and Authentication部分:
from ssl import create_default_context
context = create_default_context(cafile="path/to/cert.pem")
es = Elasticsearch(
['localhost', 'otherhost'],
http_auth=('user', 'secret'),
scheme="https",
port=443,
ssl_context=context,
)https://stackoverflow.com/questions/45110802
复制相似问题