go auth_error ai_generated true

rpc error: code = Unauthenticated desc = 缺少授权元数据

rpc error: code = Unauthenticated desc = missing authorization metadata

ID: go/grpc-unauthenticated-missing-metadata

其他格式: JSON · Markdown 中文 · English
80%修复率
88%置信度
0证据数
2024-12-01首次发现

版本兼容性

版本状态引入弃用备注
1.0+ active — — —

根因分析

服务端的鉴权拦截器要求传入元数据中包含授权令牌,但客户端未附加(或使用了错误的键)。

English

The server's auth interceptor requires an authorization token in incoming metadata, but the client did not attach one (or attached it under the wrong key).

generic

解决方案

  1. 93% 成功率
    md := metadata.Pairs("authorization", "Bearer "+token)
    ctx := metadata.NewOutgoingContext(ctx, md)
    resp, err := client.Get(ctx, req)
  2. 90% 成功率
    func authUnary(ctx context.Context, method string, req, reply interface{}, cc *grpc.ClientConn, inv grpc.UnaryInvoker, opts ...grpc.CallOption) error {
      ctx = metadata.AppendToOutgoingContext(ctx, "authorization", "Bearer "+getToken())
      return inv(ctx, method, req, reply, cc, opts...)
    }

无效尝试

常见但无效的做法:

  1. 90% 失败

    Disables authentication entirely, exposing protected endpoints

  2. 60% 失败

    May still fail if the interceptor expects a specific metadata key like "authorization" with a "Bearer " prefix