Skip to content

[Bug]:对接self-hosted的mem0的时候,如果提供API key,应该传递header,X-API-Key,而不是Authorization #1032

@lichen782

Description

@lichen782

Describe the bug
在测试自建的mem0的时候遇到问题,提供了api key的情况下无法认证通过。

在 Mem0Client.java 第 138-141 行:

     1 // Add Authorization header only if apiKey is provided
     2 if (apiKey != null && !apiKey.isEmpty()) {
     3     requestBuilder.addHeader("Authorization", "Token " + apiKey);
     4 }

问题:
- 无论 Mem0ApiType 是 PLATFORM 还是 SELF_HOSTED,都使用 Authorization: Token 头
- 但 Mem0 self-hosted 实际需要的是 X-API-Key: 头

正确的实现应该是:

     1 // Add Authorization header only if apiKey is provided
     2 if (apiKey != null && !apiKey.isEmpty()) {
     3     if (apiType == Mem0ApiType.SELF_HOSTED) {
     4         requestBuilder.addHeader("X-API-Key", apiKey);
     5     } else {
     6         requestBuilder.addHeader("Authorization", "Token " + apiKey);
     7     }
     8 }

To Reproduce
连接self hosted的mem0服务,发起请求的时候发现认证不通过:
15:11:05.140 [boundedElastic-2] WARN i.a.c.m.StaticLongTermMemoryHook - Failed to record to long-term memory: Mem0 API add request f ailed with status 401: {"detail":"需要认证"

客户端:

 try {
            return Mem0LongTermMemory.builder()
                    .agentName(agentName)
                    .userId(userId)
                    .apiBaseUrl(apiBaseUrl.trim())
                    .apiKey(apiKey != null && !apiKey.trim().isEmpty() ? apiKey.trim() : null)
                    .apiType(Mem0ApiType.SELF_HOSTED)
                    .build();
        } catch (Exception e) {
            log.warn("Failed to create Mem0LongTermMemory: {}", e.getMessage());
            return null;
        }

Expected behavior
无论是self hosted还是platform,AgentScope对应用层应该使用统一的参数。

Error messages
15:11:05.140 [boundedElastic-2] WARN i.a.c.m.StaticLongTermMemoryHook - Failed to record to long-term memory: Mem0 API add request f ailed with status 401: {"detail":"需要认证"

Environment (please complete the following information):

  • AgentScope-Java Version:1.0.10 ]
  • Java Version: 21
  • OS: macOS

Additional context
None

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Type

    No type

    Projects

    Status

    Backlog

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions