go auth_error ai_generated true

rpc error: code = PermissionDenied desc = missing or invalid authorization metadata

ID: go/grpc-permission-denied-metadata

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

Version Compatibility

VersionStatusIntroducedDeprecatedNotes
google.golang.org/grpc v1.55+ active — — —

Root Cause

The server-side auth interceptor rejected the request because the authorization metadata (bearer token, API key) was missing or failed validation.

generic

中文

服务器端认证拦截器拒绝了请求,因为授权元数据(bearer token、API key)缺失或验证失败。

Workarounds

  1. 95% success
    md := metadata.Pairs("authorization", "Bearer "+token)
    ctx = metadata.NewOutgoingContext(ctx, md)
    resp, err := client.Get(ctx, req)
  2. 93% success
    func authUnary(token string) grpc.UnaryClientInterceptor {
        return func(ctx context.Context, method string, req, reply any, cc *grpc.ClientConn, inv grpc.UnaryInvoker, opts ...grpc.CallOption) error {
            ctx = metadata.AppendToOutgoingContext(ctx, "authorization", "Bearer "+token)
            return inv(ctx, method, req, reply, cc, opts...)
        }
    }
    conn, _ := grpc.NewClient(addr, grpc.WithUnaryInterceptor(authUnary(token)))

Dead Ends

Common approaches that don't work:

  1. 98% fail

    gRPC method names are fixed; query strings are not transmitted. Token never reaches the server.

  2. 85% fail

    Leaks the token into logs and business payloads; bypasses standard interceptor checks.