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

- **ID:** `go/grpc-permission-denied-auth-token`
- **领域:** go
- **类别:** auth_error
- **验证级别:** ai_generated
- **修复率:** 80%

## 根因

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

## 版本兼容性

| 版本 | 状态 | 引入 | 弃用 |
|------|------|------|------|
| 1.56.x | active | — | — |
| 1.57.x | active | — | — |

## 解决方案

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() }
   ```

## 无效尝试

- **Continuing to use the same expired token.** — Token is still invalid. (100% 失败率)
- **Using a static test token in production.** — Security risk and may not be valid in production. (80% 失败率)
- **Removing the token from metadata.** — Server requires authentication. (90% 失败率)
