go auth_error ai_generated true

rpc error: code = Unauthenticated desc = missing authorization metadata

ID: go/grpc-unauthenticated-missing-metadata

Also available as: JSON · Markdown · 中文
80%Fix Rate
88%Confidence
0Evidence
2024-12-01First Seen

Version Compatibility

VersionStatusIntroducedDeprecatedNotes
1.0+ active — — —

Root Cause

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

中文

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

Workarounds

  1. 93% success
    md := metadata.Pairs("authorization", "Bearer "+token)
    ctx := metadata.NewOutgoingContext(ctx, md)
    resp, err := client.Get(ctx, req)
  2. 90% success
    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...)
    }

Dead Ends

Common approaches that don't work:

  1. 90% fail

    Disables authentication entirely, exposing protected endpoints

  2. 60% fail

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