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

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

## 根因

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

## 版本兼容性

| 版本 | 状态 | 引入 | 弃用 |
|------|------|------|------|
| 1.22 | active | — | — |
| 1.23 | active | — | — |

## 解决方案

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

## 无效尝试

- **Hardcoding a static token that is expired.** — Server rejects expired tokens; need a fresh one. (90% 失败率)
- **Omitting the token entirely.** — Server requires authentication; missing token always fails. (100% 失败率)
