# 建立 agent 登入請求

Canonical URL: https://sosoapi.ai/zh-hant/docs/api/identity-api-agent-auth-request

Contract: sha256:563d2d581da0ad29

開始 agent 登入請求，由使用者喺瀏覽器登入及選擇授權；此 endpoint 唔屬於 api post allowlist。唔好記錄或公開登入 session 資料。

POST /identity-api/agent-auth/request

## 驗證及權限

- **接受嘅憑證類型:** delegated_oauth

- **委派權限:** oauth-bootstrap

- **最低工作空間角色:** 此 contract 冇指定工作空間最低角色；仍需符合擁有權及授權檢查。

- **存取範圍:** 已登入使用者及獲授權資源範圍

- **需要所有工作空間同意:** 唔係

- **會改動狀態:** 唔係

- **完成依據:** not_agent_callable

憑證唔能夠擴大持有人權限。實際操作仍會檢查工作空間成員資格、資源擁有權、已選工作空間及能力授權。Tool profile 只改變可見工具，唔會授權。

呢個係驗證協定 endpoint，唔係可直接呼叫嘅產品 route。請用受支援嘅瀏覽器登入流程，唔好公開或記錄登入 session 憑證。

## 請求欄位

```json
{
  "$schema": "https://json-schema.org/draft/2020-12/schema",
  "type": "object",
  "properties": {
    "client_name": {
      "type": "string",
      "minLength": 1,
      "maxLength": 80
    },
    "device_name": {
      "type": "string",
      "minLength": 1,
      "maxLength": 120
    },
    "callback_uri": {
      "anyOf": [
        {
          "type": "string",
          "const": "http://127.0.0.1:8789/grant/callback"
        },
        {
          "type": "string",
          "maxLength": 2048,
          "format": "uri",
          "pattern": "^https:\\/\\/.*"
        }
      ]
    },
    "callback_state": {
      "type": "string",
      "minLength": 32,
      "maxLength": 256
    }
  },
  "required": [
    "client_name",
    "device_name",
    "callback_uri",
    "callback_state"
  ],
  "additionalProperties": false
}
```

## 回應及完成條件

成功 HTTP 狀態碼: 200

HTTP 成功只表示呢次操作嘅回應。背景工作要保留回傳 ID，讀取同一操作直到文件指定嘅終態。接受請求、排程成功或客戶端逾時，都唔代表已發佈。

```json
{
  "$schema": "https://json-schema.org/draft/2020-12/schema",
  "type": "object",
  "properties": {
    "request_id": {
      "type": "string",
      "format": "uuid",
      "pattern": "^([0-9a-fA-F]{8}-[0-9a-fA-F]{4}-[1-8][0-9a-fA-F]{3}-[89abAB][0-9a-fA-F]{3}-[0-9a-fA-F]{12}|00000000-0000-0000-0000-000000000000|ffffffff-ffff-ffff-ffff-ffffffffffff)$"
    },
    "approval_url": {
      "type": "string",
      "format": "uri"
    },
    "expires_in": {
      "type": "integer",
      "exclusiveMinimum": 0,
      "maximum": 9007199254740991
    }
  },
  "required": [
    "request_id",
    "approval_url",
    "expires_in"
  ]
}
```

## 錯誤及安全重試

檢查 HTTP 狀態及實際回應格式。驗證400可能回傳 ZodError；handler 錯誤可能係 error／message／code envelope。保留可用嘅 request_id 或 x-request-id，唔好記錄憑證。401／403先修正存取問題；429遵守 Retry-After。寫入結果未明或5xx時，先讀原有結果，再決定重試；只喺操作支援時重用 idempotency key，並冇通用重試或 exactly-once 保證。

## 範例

以下虛構值已按 contract 驗證，唔係真實帳戶 ID、憑證或發佈紀錄。請用已授權範圍回傳嘅 ID、已準備好嘅素材及已審核排程取代，寫入前核對平台資格。此頁唔會呼叫 API。

喺伺服器環境設定 SOSOAPI_ACCESS_TOKEN，使用此操作接受嘅憑證。唔好放入瀏覽器程式。一般 agent 建議透過 sosoapi auth login 用瀏覽器 OAuth，由 CLI 管理憑證更新。

request.json

```json
{
  "client_name": "example",
  "device_name": "example",
  "callback_uri": "http://127.0.0.1:8789/grant/callback",
  "callback_state": "exampleaaaaaaaaaaaaaaaaaaaaaaaaa"
}
```

cURL

```bash
curl --request POST \
  "https://gqeshlawtcexsapabaui.supabase.co/functions/v1/identity-api/agent-auth/request" \
  -H "Authorization: Bearer $SOSOAPI_ACCESS_TOKEN" \
  -H "Content-Type: application/json" \
  --data-binary @request.json
```

JavaScript

```js
// Server-side JavaScript. Do not print tokens or credential-bearing responses.
const token = process.env.SOSOAPI_ACCESS_TOKEN;
if (!token) throw new Error("Set an accepted credential for this operation");
const response = await fetch("https://gqeshlawtcexsapabaui.supabase.co/functions/v1/identity-api/agent-auth/request", {
  method: "POST",
  headers: { "Content-Type": "application/json", Authorization: "Bearer " + token },
  body: JSON.stringify({
  "client_name": "example",
  "device_name": "example",
  "callback_uri": "http://127.0.0.1:8789/grant/callback",
  "callback_state": "exampleaaaaaaaaaaaaaaaaaaaaaaaaa"
})
});
const result = await response.json();
// Check response.ok, the operation-specific body and completion state before continuing.
```

虛構回應範例

```json
{
  "request_id": "11111111-1111-4111-8111-111111111111",
  "approval_url": "https://example.com/media.png",
  "expires_in": 1
}
```

## 相關工具及指南

[API reference](https://sosoapi.ai/zh-hant/docs/api)
