【问题标题】:How to fix "elasticsearch.exceptions.AuthenticationException: ... missing authentication token for REST request"?如何修复“elasticsearch.exceptions.AuthenticationException:...缺少 REST 请求的身份验证令牌”?
【发布时间】:2017-12-20 00:24:58
【问题描述】:

我正在使用 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]')

【问题讨论】:

    标签: python elasticsearch elasticsearch-5


    【解决方案1】:

    ''missing authentication token' 表示您需要先进行身份验证,然后才能与此 Elasticsearch 实例通信。要索引文档,用户必须具有写入权限。您可以在 URL 中包含用户名和密码,如下所示:http://user:password@hostname:port

    例如,在外壳中:

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

    然后在python中:

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

    【讨论】:

      【解决方案2】:

      另外,如果您使用 Postman 工具执行此操作,例如:

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

      【讨论】:

        【解决方案3】:

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

        client = Elasticsearch(
            hosts=['localhost:5000'],
            http_auth=('username', 'password'),
        )
        s = Search(using=client, index='something')
        

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

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

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

        参数

        • http_auth – 可选的 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,
        )
        

        【讨论】:

          猜你喜欢
          • 2017-04-05
          • 1970-01-01
          • 1970-01-01
          • 2023-03-20
          • 1970-01-01
          • 2019-02-21
          • 2020-09-12
          • 2016-09-04
          • 2018-08-03
          相关资源
          最近更新 更多