go auth_error ai_generated true

rpc error: code = Unauthenticated desc = missing or invalid authentication token

ID: go/grpc-unauthenticated-missing-token

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

Version Compatibility

VersionStatusIntroducedDeprecatedNotes
1.22 active
1.23 active

Root Cause

The gRPC client did not provide a valid authentication token, or the token has expired/was rejected by the server's interceptor.

generic

中文

gRPC客户端未提供有效的身份验证令牌,或令牌已过期/被服务器的拦截器拒绝。

Workarounds

  1. 95% success Obtain a valid token from the auth service and pass it in metadata.
    token := getAuthToken()
    md := metadata.Pairs("authorization", "Bearer "+token)
    ctx := metadata.NewOutgoingContext(context.Background(), md)
    resp, err := client.SomeRPC(ctx, req)
  2. 90% success Use a per-RPC credential with grpc.WithPerRPCCredentials.
    conn, _ := grpc.Dial(target, grpc.WithPerRPCCredentials(oauth.NewTokenSource(tokenSource)))

Dead Ends

Common approaches that don't work:

  1. Hardcoding a static token that is expired. 90% fail

    Server rejects expired tokens; need a fresh one.

  2. Omitting the token entirely. 100% fail

    Server requires authentication; missing token always fails.