无法使用golang和elasticsearch-v8连接到elasticsearch

问题描述 投票:0回答:1

我正在使用的方法:

func getESClient() (*elasticsearch.Client, error) {

    cfg := config.LoadEnv()
    cloudId := cfg.ElasticCloudId
    apiKey := cfg.ElasticSearchApiKey

    log.Println("Using CloudID:", cloudId)

    client, err := elasticsearch.NewClient(elasticsearch.Config{
        CloudID: string(cloudId),
        APIKey:  apiKey,
    })

    if err != nil {
        log.Fatal("Failed to connect to the Elasticsearch client:", err)
        return nil, err
    }
    log.Println(client.Info())
    log.Println("Client info")
    return client, nil
}

我遇到的错误:

2024/06/14 20:04:16 [401 Unauthorized] {"error":{"root_cause":[{"type":"security_exception","reason":"unable to authenticate with provided credentials and anonymous access is not allowed for this request","additional_unsuccessful_credentials":"API key: Illegal base64 character 3a","header":{"WWW-Authenticate":["Basic realm=\"security\" charset=\"UTF-8\"","Bearer realm=\"security\"","ApiKey"]}}],"type":"security_exception","reason":"unable to authenticate with provided credentials and anonymous access is not allowed for this request","additional_unsuccessful_credentials":"API key: Illegal base64 character 3a","header":{"WWW-Authenticate":["Basic realm=\"security\" charset=\"UTF-8\"","Bearer realm=\"security\"","ApiKey"]}}

ELASTICSEARCH_API_KEY=https://id.westus2.azure.elastic-cloud.com:443
ELASTICSEARCH_CLOUD_ID=elasticsearch:id==

使用

"github.com/elastic/go-elasticsearch/v8"

我无法直接访问elasticsearch节点,它变得更加复杂。有人有提示吗?

go elasticsearch
1个回答
0
投票

"API key: Illegal base64 character 3a"
意味着您为连接指定的密钥实际上并不是 Base64 格式。如果您查看 [Elasticsearch API 密钥文档][1],您需要提供
encoded
值。从您的问题来看,您似乎指定了 URL 而不是 API 密钥,因此请尝试更新它并查看它是否可以连接。

[1]:https://www.elastic.co/guide/en/elasticsearch/reference/current/security-api-create-api-key.html#:~:text=API%20key%20credentials%20which %20是,来自%20的%20响应编码的%20值%20)。

© www.soinside.com 2019 - 2024. All rights reserved.