# 通过 HTTP 调用模板

## 1. 获取授权（OAuth2 令牌）

在调用任何功能性 API 之前，您必须先使用您的凭据交换 **JWT 访问令牌**。

* **端点：** `POST https://api.morelogin.com/oauth2/token`
* **官方文档：** [授权路径](https://guide.morelogin.com/api-reference/open-api/open-api/authorization/paths/~1oauth2~1token/post)


### 请求（cURL）

```bash
curl -i -X POST \
  https://api.morelogin.com/oauth2/token \
  -H 'Content-Type: application/json' \
  -d '{
    "client_id": 1672940217990530,
    "client_secret": "your_client_secret",
    "grant_type": "client_credentials"
  }'
```### 回复示例

```json
{
    "code": 0,
    "data": {
        "access_token": "eyJhbGciOiJIUzI1NiIsIn...",
        "token_type": "Bearer",
        "expires_in": 3600
    }
}
```---

## 2. 执行云手机模板

使用步骤 1 中获取的 access_token 来触发特定的 RPA 模板。

* **端点：** `POST https://api.morelogin.com/cloudphone/rpa/onceTask/save`

* **官方文档：** [执行计划路径](https://guide.morelogin.com/api-reference/open-api/open-api/cloud-phoneschedules-management/paths/~1cloudphone~1rpa~1oncetask~1save/post)

### 请求 (cURL)

```bash
curl -i -X POST \
  https://api.morelogin.com/cloudphone/rpa/onceTask/save \
  -H 'Authorization: Bearer <YOUR_JWT_HERE>' \
  -H 'Content-Type: application/json' \
  -d '{
    "cloudPhoneId": 16783319661123,
    "scheduleName": "test_automation",
    "templateId": 1678347487160256,
    "templateParameter": "{\"Video Caption\": \"Hello World\", \"AI Label\": true}",
    "description": "schedule description"
  }'
```---

## 3. 参数详解

### 如何获取 ID？

* **Authorization：** 通过 `/oauth2/token` 端点获取。

* **cloudPhoneId：** 在 MoreLogin 云手机控制面板中找到（数字 ID）。

* **templateId：** 在自动化模板市场或个人模板列表中找到。

### 理解 templateParameter

这是一个字符串化的 JSON 对象。您必须根据模板的要求定义键，然后将其转义为字符串。

**模板定义示例：**

```json
{
    "Video Caption": "string",
    "AI Label": "boolean",
    "Product Id": "number",
    "Get Leads": "boolean",
    "Comment": "string"
}
```

**如何在请求中映射：**

要为上述定义传递值，您的 templateParameter 值应如下所示：

```json
{
    "templateParameter": "{\"Video Caption\": \"My Title\", \"AI Label\": true, \"Product Id\": 12345}"
}
```

## 4. 错误处理

| 状态 | 可能原因 |

| ---------------- | ------------------------------------------------------------ |

| 401 未授权 | 令牌过期或客户端凭据无效。 |

| 400 错误请求 | cloudPhoneId 无效或 templateParameter 中的 JSON 格式错误。 |

| 404 未找到 | templateId 不存在。 |

## Python 快速入门

```python
import requests
import json

def run_morelogin_task():
    # 1. Auth
    auth_res = requests.post(
        "https://api.morelogin.com/oauth2/token",
        json={
            "client_id": 1672940217990530,
            "client_secret": "your_secret",
            "grant_type": "client_credentials"
        }
    )
    token = auth_res.json()['data']['access_token']

    # 2. Execute
    task_payload = {
        "cloudPhoneId": 16783319661123,
        "templateId": 1678347487160256,
        "templateParameter": json.dumps({
            "Video Caption": "My Post",
            "AI Label": True
        })
    }
    
    headers = {"Authorization": f"Bearer {token}"}
    response = requests.post(
        "https://api.morelogin.com/cloudphone/rpa/onceTask/save",
        json=task_payload,
        headers=headers
    )
    return response.json()

print(run_morelogin_task())
```