用户登录
接口说明
用户登录获取访问令牌。
请求信息
- Method:
POST - Path:
/v1/auth/login - 认证: 不需要
请求参数
json
{
"username": "string",
"password": "string"
}响应示例
json
{
"code": 0,
"message": "success",
"data": {
"access_token": "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9...",
"refresh_token": "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9...",
"token_type": "Bearer",
"expires_in": 86400 // 秒
}
}错误情况
- 401: 用户名或密码错误
- 400: 参数验证失败
示例代码
bash
curl -X POST http://localhost:8081/v1/auth/login \
-H "Content-Type: application/json" \
-d '{
"username": "testuser",
"password": "testpass123"
}'javascript
const response = await fetch('http://localhost:8081/v1/auth/login', {
method: 'POST',
headers: {
'Content-Type': 'application/json'
},
body: JSON.stringify({
username: 'testuser',
password: 'testpass123'
})
});
const data = await response.json();
const accessToken = data.data.access_token;python
import requests
response = requests.post(
'http://localhost:8081/v1/auth/login',
json={
'username': 'testuser',
'password': 'testpass123'
}
)
data = response.json()
access_token = data['data']['access_token']