go auth_error ai_generated true

rpc 错误:代码 = PermissionDenied 描述 = 无效的身份验证令牌

rpc error: code = PermissionDenied desc = invalid auth token

ID: go/grpc-permission-denied-auth-token

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

版本兼容性

版本状态引入弃用备注
1.56.x active
1.57.x active

根因分析

gRPC 元数据中提供的身份验证令牌无效或已过期。

English

The authentication token provided in the gRPC metadata is invalid or expired.

generic

解决方案

  1. 90% 成功率
    token, err := refreshToken(); if err != nil { log.Fatal(err) }; md := metadata.Pairs("authorization", "Bearer "+token)
  2. 85% 成功率
    interceptor := func(ctx context.Context, method string, req, reply interface{}, cc *grpc.ClientConn, invoker grpc.UnaryInvoker, opts ...grpc.CallOption) error { token := getValidToken(); ctx = metadata.AppendToOutgoingContext(ctx, "authorization", "Bearer "+token); return invoker(ctx, method, req, reply, cc, opts...) }
  3. 95% 成功率
    if token.Expiry.Before(time.Now()) { token, err = refreshToken() }

无效尝试

常见但无效的做法:

  1. Continuing to use the same expired token. 100% 失败

    Token is still invalid.

  2. Using a static test token in production. 80% 失败

    Security risk and may not be valid in production.

  3. Removing the token from metadata. 90% 失败

    Server requires authentication.