API 调用快速体验

更新时间:
复制 MD 格式

5 分钟内使用 Aidge API 完成一次文本翻译。本教程提供完整的 Python 示例和 cURL 示例。

前提条件

  • 已开通 Aidge 文本翻译功能的阿里云账号。请参见准备工作

  • AccessKey 对(AccessKey ID 和 AccessKey Secret)。请参见准备工作 - 步骤四

  • Python 3.6+ 及 requests 库,或 cURL。

Python 示例

import os
import time
import hashlib
import hmac
import json
import requests

# 从环境变量读取凭证
ACCESS_KEY_NAME = os.environ["AIDGE_ACCESS_KEY_NAME"]
ACCESS_KEY_SECRET = os.environ["AIDGE_ACCESS_KEY_SECRET"]

# API 端点
# 中国站:"cn-api.aidc-ai.com"
# 国际站:"api.aidc-ai.com"
API_DOMAIN = "cn-api.aidc-ai.com"


def call_aidge_api(api_name: str, payload: dict, use_trial: bool = True) -> dict:
    """使用阿里云 AccessKey (AK/SK) 调用 Aidge API。"""
    timestamp = str(int(time.time() * 1000))

    # 用 HMAC-SHA256 计算 AK/SK 签名
    sign_string = ACCESS_KEY_SECRET + timestamp
    signature = hmac.new(
        ACCESS_KEY_SECRET.encode("utf-8"),
        sign_string.encode("utf-8"),
        hashlib.sha256,
    ).hexdigest().upper()

    url = (
        f"https://{API_DOMAIN}/rest{api_name}"
        f"?partner_id=aidge&sign_method=sha256&sign_ver=v2"
        f"&app_key={ACCESS_KEY_NAME}&timestamp={timestamp}&sign={signature}"
    )

    headers = {
        "Content-Type": "application/json",
        "x-iop-trial": str(use_trial).lower(),
    }

    response = requests.post(url, data=json.dumps(payload), headers=headers)
    return response.json()


# --- 翻译文本 ---
result = call_aidge_api(
    api_name="/ai/text/marco/translator",
    payload={
        "SourceLanguage": "zh",
        "TargetLanguage": "en",
        "text": "跨境电商智能翻译解决方案",
    },
)

print(json.dumps(result, indent=2, ensure_ascii=False))

运行示例

将上述代码保存为 translate.py,然后执行以下命令:

export AIDGE_ACCESS_KEY_NAME="your-access-key-name"
export AIDGE_ACCESS_KEY_SECRET="your-access-key-secret"
pip install requests
python translate.py

cURL 示例

# 设置凭证
AK_NAME="your-access-key-name"
AK_SECRET="your-access-key-secret"
TIMESTAMP=$(date +%s000)

# 用 HMAC-SHA256 计算 AK/SK 签名
SIGN=$(echo -n "${AK_SECRET}${TIMESTAMP}" \
  | openssl dgst -sha256 -hmac "${AK_SECRET}" \
  | awk '{print toupper($2)}')

# 调用文本翻译 API
curl -X POST \
  "https://cn-api.aidc-ai.com/rest/ai/text/marco/translator?partner_id=aidge&sign_method=sha256&sign_ver=v2&app_key=${AK_NAME}&timestamp=${TIMESTAMP}&sign=${SIGN}" \
  -H "Content-Type: application/json" \
  -H "x-iop-trial: true" \
  -d '{
    "SourceLanguage": "zh",
    "TargetLanguage": "en",
    "text": "跨境电商智能翻译解决方案"
  }'

解析响应

成功响应的结构如下:

{
  "Code": "Success",
  "Message": "ok",
  "RequestId": "abc123...",
  "Data": {
    "TranslatedText": "Intelligent translation solution for cross-border e-commerce"
  }
}

通过响应体的 Code 字段判断调用结果。"Success" 表示成功;失败时 Code 为命名错误码(如 InvalidParameterFreeQuotaExhausted),详见错误码RequestId 是排查问题的关键标识。

使用试用额度

x-iop-trial 请求头设置为 "true" 即可使用免费试用额度。使用正式额度时,设置为 "false"

如果将 x-iop-trial 设置为 "false" 且无可用额度,API 返回以下错误:

Sorry, your calling resources have been exhausted.

常见错误

错误信息 原因 解决方法
Invalid signature AccessKey Secret 错误或时间戳偏差 核对 AccessKey Secret,确保系统时钟准确。
Calling resources exhausted 无可用额度(试用或正式) x-iop-trial 设为 "true" 使用试用额度,或确认账户已开通按量付费。
Invalid app_key AccessKey Name 错误 在 Aidge 控制台侧边栏管理分组下的额度与 API Key 页面核对 AccessKey Name。
Rate limit exceeded 达到 QPS 上限(默认 10 QPS) 降低请求频率,或联系支持团队提升上限。

后续步骤