认证指南
本指南逐步说明如何获取访问令牌以使用 CorpX PIX API。
概述
CorpX API 使用 OAuth 2.0 的 Client Credentials 流程进行认证。您需要使用您的凭据(client_id 和 client_secret)来获取有效的访问令牌。
前提条件
在开始之前,请确保您已具备以下信息:
- Client ID - 您的唯一客户端标识符
- Client Secret - 用于认证的密钥
- X-Tenant-Id - 您的租户标识符(例如:
tenant-suaempresa)
信息
如果您还没有凭据,请联系我们的支持团队。
环境
| 环境 | 认证 URL | API URL | 备注 |
|---|---|---|---|
| Sandbox(开发) | — | — | 暂时停用。 |
| 生产环境 | https://auth.corpxapi.com/oauth2/token | https://api.corpxapi.com |
第一步:请求访问令牌
请求
curl -X POST "https://auth.corpxapi.com/oauth2/token" \
-H "Content-Type: application/x-www-form-urlencoded" \
-d "grant_type=client_credentials" \
-d "client_id=YOUR_CLIENT_ID" \
-d "client_secret=YOUR_CLIENT_SECRET"
参数
| 参数 | 类型 | 必填 | 描述 |
|---|---|---|---|
grant_type | string | 是 | 固定为 client_credentials |
client_id | string | 是 | 您的客户端标识符 |
client_secret | string | 是 | 您的密钥 |
成功响应 (200 OK)
{
"access_token": "eyJraWQiOiJ0emwzZWVYWGx1eVlDWHFwQXdBTzJWYWJNQ0llMFMyMXVRWGV2Y281N2RRPSIsImFsZyI6IlJTMjU2In0...",
"expires_in": 3600,
"token_type": "Bearer"
}
| 字段 | 描述 |
|---|---|
access_token | 用于 API 调用认证的 JWT 令牌 |
expires_in | 有效期(秒),通常为 3600 = 1 小时 |
token_type | 令牌类型(固定为 Bearer) |
错误响应 (401 Unauthorized)
{
"error": "invalid_client",
"error_description": "Client authentication failed"
}
第二步:在请求中使用令牌
获取令牌后,在所有 API 请求的 Authorization 头中携带该令牌。
示例:查询余额
curl -X GET "https://api.corpxapi.com/v1/accounts/{accountId}/balance" \
-H "Authorization: Bearer eyJraWQiOiJ0emwzZWVYWGx1eVlDWHFwQXdBTzJWYWJNQ0llMFMyMXVRWGV2Y281N2RRPSIsImFsZyI6IlJTMjU2In0..." \
-H "X-Tenant-Id: tenant-suaempresa" \
-H "Content-Type: application/json"
必需的请求头
| 请求头 | 描述 |
|---|---|
Authorization | 格式为 Bearer {access_token} 的访问令牌 |
X-Tenant-Id | 您的租户标识符 |
Content-Type | 带有请求体的请求使用 application/json |
第三步:续期令牌
令牌会在 expires_in 指定的时间后过期。我们建议:
- 缓存令牌并记录其过期时间
- 在过期前续期 - 提前几分钟请求新令牌
- 处理 401 错误 - 如果收到
401 Unauthorized,重新获取令牌
自动续期示例 (Bash)
#!/bin/bash
# Variables
CLIENT_ID="your_client_id"
CLIENT_SECRET="your_client_secret"
AUTH_URL="https://auth.corpxapi.com/oauth2/token"
# Function to get token
get_token() {
response=$(curl -s -X POST "$AUTH_URL" \
-H "Content-Type: application/x-www-form-urlencoded" \
-d "grant_type=client_credentials" \
-d "client_id=$CLIENT_ID" \
-d "client_secret=$CLIENT_SECRET")
echo "$response" | jq -r '.access_token'
}
# Get token
TOKEN=$(get_token)
# Use the token
curl -X GET "https://api.corpxapi.com/v1/accounts/{accountId}/balance" \
-H "Authorization: Bearer $TOKEN" \
-H "X-Tenant-Id: tenant-suaempresa"
不同语言的完整示例
Python
import requests
# Credentials
CLIENT_ID = "your_client_id"
CLIENT_SECRET = "your_client_secret"
TENANT_ID = "tenant-suaempresa"
# Get token
auth_response = requests.post(
"https://auth.corpxapi.com/oauth2/token",
data={
"grant_type": "client_credentials",
"client_id": CLIENT_ID,
"client_secret": CLIENT_SECRET
}
)
token = auth_response.json()["access_token"]
# Use the token
headers = {
"Authorization": f"Bearer {token}",
"X-Tenant-Id": TENANT_ID,
"Content-Type": "application/json"
}
response = requests.get(
"https://api.corpxapi.com/v1/accounts/{accountId}/balance",
headers=headers
)
print(response.json())
Node.js
const axios = require('axios');
const CLIENT_ID = 'your_client_id';
const CLIENT_SECRET = 'your_client_secret';
const TENANT_ID = 'tenant-suaempresa';
async function getToken() {
const response = await axios.post(
'https://auth.corpxapi.com/oauth2/token',
new URLSearchParams({
grant_type: 'client_credentials',
client_id: CLIENT_ID,
client_secret: CLIENT_SECRET
}),
{ headers: { 'Content-Type': 'application/x-www-form-urlencoded' } }
);
return response.data.access_token;
}
async function getBalance(accountId) {
const token = await getToken();
const response = await axios.get(
`https://api.corpxapi.com/v1/accounts/${accountId}/balance`,
{
headers: {
'Authorization': `Bearer ${token}`,
'X-Tenant-Id': TENANT_ID,
'Content-Type': 'application/json'
}
}
);
return response.data;
}
常见错误
| 错误 | 原因 | 解决方案 |
|---|---|---|
invalid_client | Client ID 或 Secret 不正确 | 检查您的凭据 |
invalid_grant | 无效的 grant type | 使用 client_credentials |
401 Unauthorized | 令牌已过期或无效 | 重新获取令牌 |
403 Forbidden | 令牌有效但无权限 | 检查 X-Tenant-Id |
最佳实践
- 切勿将
client_secret暴露在客户端代码(前端)中 - 使用环境变量存储凭据
- 实施令牌缓存以避免不必要的请求
- 监控过期时间并主动续期令牌
- 使用 HTTPS 进行所有通信
后续步骤
现在您已了解如何进行认证,请探索其他指南:
- 动态 QR Code 指南 - 生成 PIX 收款码
- Cash Out 指南 - 发起 PIX 转账
- 退款指南 - 申请退款