# rpc错误：代码=未认证 描述=令牌已过期：当前时间2025-01-15T10:00:00Z晚于到期时间2025-01-15T09:00:00Z

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

## 根因

gRPC调用中使用的身份验证令牌已过期，需要新令牌。

## 版本兼容性

| 版本 | 状态 | 引入 | 弃用 |
|------|------|------|------|
| 1.0 | active | — | — |
| 1.1 | active | — | — |

## 解决方案

1. **Refresh the token before making the RPC call.** (90% 成功率)
   ```
   token, err := refreshToken()
if err != nil { log.Fatal(err) }
conn, err := grpc.Dial("localhost:8080", grpc.WithInsecure(), grpc.WithPerRPCCredentials(&tokenCreds{token: token}))
   ```
2. **Implement automatic token refresh in an interceptor.** (85% 成功率)
   ```
   func refreshInterceptor(ctx context.Context, method string, req, reply interface{}, cc *grpc.ClientConn, invoker grpc.UnaryInvoker, opts ...grpc.CallOption) error {
    token, err := getValidToken()
    if err != nil { return err }
    md := metadata.Pairs("authorization", "Bearer "+token)
    ctx = metadata.NewOutgoingContext(ctx, md)
    return invoker(ctx, method, req, reply, cc, opts...)
}
   ```

## 无效尝试

- **Use the same expired token and hope it works.** — The server checks expiration; expired tokens are always rejected. (100% 失败率)
- **Manually extend the token's expiration without re-authentication.** — Token expiration is enforced by the auth server; manual changes are invalid. (95% 失败率)
