# 换取访问令牌：client_credentials（client_id = 渠道号，client_secret = 口令）或 password（username = OMS 登录账号，password = OMS 密码）→ Bearer access_token

`POST /api/v1/auth/token` — 无需 token

认证

## 请求体（JSON）

| 字段 | 类型 | 说明 |
| --- | --- | --- |
| `grant_type` | string | OAuth2 授权类型：client_credentials（渠道号 + 口令）或 password（OMS 账号 + 密码）。可省略：给了 username 就按 password 处理，否则按 client_credentials。 |
| `client_id` | string | 渠道号（OAuth2 的 client_id），由对接人发放；client_credentials 方式用。想先试可以用 sandbox。 (最长 32 · 格式 ^[A-Za-z0-9_-]+$) |
| `client_secret` | string | 渠道口令（OAuth2 的 client_secret）；client_credentials 方式用。想先试可以用 sandbox（沙箱返回演示数据，不碰真库存）。 (最长 256) |
| `username` | string | OMS 网站的登录账号（password 方式用），比如 100470admin；建议给系统对接单独建一个子账号。想先试可以用 sandbox。 (最长 64) |
| `password` | string | OMS 登录密码（password 方式用）。只在换 token 那一刻经手，网关不保存、不记日志。想先试可以用 sandbox。 (最长 256) |

## 响应

响应信封只看 HTTP 状态码：成功是 2xx，正文 `{ data: … }`（列表另带 `page / page_size / has_more` 或 `total_count`）；失败是 4xx / 5xx，正文 `{ error: { code, message, hint, request_id, doc_url } }`。

```json
{
  "data": { "access_token": "eyJhbGciOi…", "token_type": "Bearer", "expires_in": 3600, "expires_at": "2026-09-03T06:27:28.000Z" }
}
```

成功响应里没有任何给人看的文字——没有 `code / result / msg`。`error.code` 是唯一该用程序判断的字段，`message` 跟随 `Accept-Language`。

## 示例

### curl

```bash
curl -X POST "https://connect.everugg.net.au/api/v1/auth/token" \
  -H "Content-Type: application/json" \
  -d '{ "grant_type": "client_credentials", "client_id": "<渠道号>", "client_secret": "<口令>" }'
```

### Python

```python
import requests

r = requests.post(
    "https://connect.everugg.net.au/api/v1/auth/token",
    json={
      "grant_type": "client_credentials",
      "client_id": "<渠道号>",
      "client_secret": "<口令>"
    },
)
data = r.json()
r.raise_for_status()  # 4xx/5xx: data["error"]["code"] / ["hint"]
```

### Node

```js
const res = await fetch("https://connect.everugg.net.au/api/v1/auth/token", {
  method: "POST",
  headers: { "Content-Type": "application/json" },
  body: JSON.stringify({
    "grant_type": "client_credentials",
    "client_id": "<渠道号>",
    "client_secret": "<口令>"
  }),
})
const data = await res.json()
if (!res.ok) throw new Error(`${data.error?.code}: ${data.error?.message}`)
```

### Java

```java
HttpRequest req = HttpRequest.newBuilder()
    .uri(URI.create("https://connect.everugg.net.au/api/v1/auth/token"))
    .header("Content-Type", "application/json")
    .POST(HttpRequest.BodyPublishers.ofString("{ \"grant_type\": \"client_credentials\", \"client_id\": \"<渠道号>\", \"client_secret\": \"<口令>\" }"))
    .build();
HttpResponse<String> res = HttpClient.newHttpClient().send(req, HttpResponse.BodyHandlers.ofString());
```

---
Markdown 源: https://connect.everugg.net.au/reference/post-api-v1-auth-token.md?lang=zh · 网页版: https://connect.everugg.net.au/reference?op=post-api-v1-auth-token&lang=zh
