深度揭秘:Nano Banana Tasks API 的整合与应用

0 阅读3分钟

深度揭秘:Nano Banana Tasks API 的整合与应用

您是否曾因任务状态查询而感到困惑?无论是在数据处理、图片生成还是其他任务中,掌握任务执行状态至关重要。今天,我们将一起探讨如何高效使用 Nano Banana Tasks API 进行任务状态查询,助您轻松掌握进度动态。

API 的强大能力

Nano Banana Tasks API 的核心功能在于通过任务 ID 查询任务执行状态。该 API 与 Nano Banana Images API 紧密结合,使开发者能够实时获取任务信息。

接下来,我们将通过一个示例向您展示如何实现这一功能。

请求示例

假设我们已经通过 Nano Banana Images API 获得了一个任务 ID:4d320ead-4af4-4a55-8f3e-f2afebdf4fd0。现在,让我们展示如何利用该 ID 进行状态查询。

请求头与请求体设置

在调用 API 时,必须设置请求头与请求体:

请求头包括:

  • accept: 指定响应格式为 JSON,需设置为 application/json
  • authorization: 用于调用 API 的密钥,申请后即可获取。

请求体包括:

  • id: 上传的任务 ID。
  • action: 指定执行的操作。

设置示例如下:

请求示例

代码示例

您可以使用多种编程语言来调用此 API。以下是两种常用语言的示例代码。

CURL
curl -X POST 'https://api.acedata.cloud/nano-banana/tasks' \
-H 'accept: application/json' \
-H 'authorization: Bearer {token}' \
-H 'content-type: application/json' \
-d '{
  "id": "4d320ead-4af4-4a55-8f3e-f2afebdf4fd0",
  "action": "retrieve"
}'
Python
import requests

url = "https://api.acedata.cloud/nano-banana/tasks"

headers = {
    "accept": "application/json",
    "authorization": "Bearer {token}",
    "content-type": "application/json"
}

payload = {
    "id": "4d320ead-4af4-4a55-8f3e-f2afebdf4fd0",
    "action": "retrieve"
}

response = requests.post(url, json=payload, headers=headers)
print(response.text)

响应示例

成功请求后,API 将返回任务的详细信息,例如:

{
  "_id": "68bc7c3c550a4144a53d0e24",
  "id": "4d320ead-4af4-4a55-8f3e-f2afebdf4fd0",
  "response": {
    "success": true,
    "task_id": "4d320ead-4af4-4a55-8f3e-f2afebdf4fd0",
    "data": [
      {
        "prompt": "a white siamese cat",
        "image_url": "https://platform.cdn.acedata.cloud/nanobanana/7e7bd000-698a-4e14-bb2d-3db61237e4bb.png"
      }
    ]
  }
}

从这个响应中,您可以清晰地看到请求与响应的详细信息,包括生成的任务 ID 和返回的数据。

批量查询操作

如果您需要查询多个任务的详细信息,可以使用批量查询功能。在请求体中,将 action 设置为 retrieve_batch

例如:

curl -X POST 'https://api.acedata.cloud/nano-banana/tasks' \
-H 'accept: application/json' \
-H 'authorization: Bearer {token}' \
-H 'content-type: application/json' \
-d '{
  "ids": ["1ebe4f2b-59ba-4385-a4ea-0ce8a3fe12ed","1ebe4f2b-59ba-4385-a4ea-0ce8a3fe12ed"],
  "action": "retrieve_batch"
}'

批量查询响应示例

成功请求后,API 将返回所有批量任务的详细信息:

{
  "items": [
    {
      "id": "4d320ead-4af4-4a55-8f3e-f2afebdf4fd0",
      "response": {
        "success": true,
        "data": [
          {
            "prompt": "a white siamese cat",
            "image_url": "https://platform.cdn.acedata.cloud/nanobanana/7e7bd000-698a-4e14-bb2d-3db61237e4bb.png"
          }
        ]
      }
    }
  ],
  "count": 1
}

错误处理

在调用 API 的过程中,如果发生错误,API 将返回相应的错误代码与信息。例如:

  • 400 token_mismatched: 请求错误,可能因参数缺失或无效。
  • 401 invalid_token: 未授权,授权令牌无效或缺失。
  • 429 too_many_requests: 请求过多,超出速率限制。

错误响应示例

{
  "success": false,
  "error": {
    "code": "api_error",
    "message": "fetch failed"
  }
}

总结

通过本文,您应该对如何使用 Nano Banana Tasks API 查询单个或批量任务的详细信息有了明确的认识。希望这些信息能助您更好地整合与应用这个强大的 API。