go auth_error ai_generated true

rpc错误:代码=未认证 描述=缺少或无效的身份验证令牌

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

ID: go/grpc-unauthenticated-missing-token

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

版本兼容性

版本状态引入弃用备注
1.22 active
1.23 active

根因分析

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

English

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

generic

解决方案

  1. 95% 成功率 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% 成功率 Use a per-RPC credential with grpc.WithPerRPCCredentials.
    conn, _ := grpc.Dial(target, grpc.WithPerRPCCredentials(oauth.NewTokenSource(tokenSource)))

无效尝试

常见但无效的做法:

  1. Hardcoding a static token that is expired. 90% 失败

    Server rejects expired tokens; need a fresh one.

  2. Omitting the token entirely. 100% 失败

    Server requires authentication; missing token always fails.