# rpc 错误：代码 = Unauthenticated 描述 = 缺少凭据

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

## 根因

客户端未在请求元数据中提供任何身份验证凭据。

## 版本兼容性

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

## 解决方案

1. **** (100% 成功率)
   ```
   md := metadata.Pairs("authorization", "Bearer mytoken"); ctx := metadata.NewOutgoingContext(context.Background(), md)
   ```
2. **** (90% 成功率)
   ```
   creds := credentials.NewClientTLSFromCert(nil, ""); conn, err := grpc.Dial(address, grpc.WithTransportCredentials(creds))
   ```
3. **** (85% 成功率)
   ```
   interceptor := func(ctx context.Context, method string, req, reply interface{}, cc *grpc.ClientConn, invoker grpc.UnaryInvoker, opts ...grpc.CallOption) error { ctx = metadata.AppendToOutgoingContext(ctx, "authorization", "Bearer mytoken"); return invoker(ctx, method, req, reply, cc, opts...) }
   ```

## 无效尝试

- **Sending an empty token or user/pass.** — Server still sees no valid credentials. (90% 失败率)
- **Removing auth checks.** — Security risk. (80% 失败率)
- **Not adding any metadata.** — Same error will occur. (100% 失败率)
