← 返回目录
📁 API Reference › Authentication

获取长期访问令牌

您可以使用/oauth2/access_token/或/oauth/token/生成长期访问令牌。

长期访问令牌是从广告账户、商务中心账户或 TikTok One Creator Marketplace 账户获取授权后生成的访问令牌类型。该令牌不会过期。若您想撤销长期访问令牌,可使用/oauth2/revoke_token/。

注意
/oauth2/access_token/仅支持Content-Type 为 application/json,而/oauth/token/支持 Content-Type设置为application/json或 application/x-www-form-urlencoded。

/oauth2/access_token/

您可以使用本接口获得长期有效的访问令牌。

仅支持将Content-Type设置为application/json。若您想生成访问令牌时将 Content-Type设置为 application/x-www-form-urlencoded,请查看下文的/oauth/token/小节。

v1.2与v1.3版本对比

以下表格概括了 v1.2与v1.3接口的变化。 ``xtable |变化内容{30%}|v1.2{35%}|v1.3{35%}| |---|---|---| |接口路径|/v1.2/oauth2/access_token/|/v1.3/oauth2/access_token/| |请求参数类型 |app_id : number|app_id: string | |新增请求参数|/|return_advertiser_ids| |返回参数类型|advertiser_ids: number[]|advertiser_ids`: string[]|


### 请求
**地址** https://business-api.tiktok.com/open_api/v1.3/oauth2/access_token/

**方法** POST

**Header**
```xtable
|字段{30%}|类型{15%}|描述{55%}|
|---|---|---|
|Content-Type {必填}|string|请求消息类型。允许值: `"application/json"`。  |

参数

|字段{30%}|类型{15%}|描述{55%}|
|---|---|---|
|app_id {必填}|string| 开发者应用 ID。<br><br>若想获取<strong>应用 ID</strong>,可以导航至<strong>[应用管理](https://ads.tiktok.com/marketing_api/apps/)</strong> > <strong>App Detail</strong> > <strong>基本信息</strong>。 |
|secret {必填}|string| 开发者应用密钥。<br><br>若想获取<strong>密钥</strong>,可以导航至<strong>[应用管理](https://ads.tiktok.com/marketing_api/apps/)</strong> > <strong>App Detail</strong> > <strong>基本信息</strong>。|
|auth_code {必填}|string|您的<strong>广告主授权 URL </strong>生成的授权码(`auth_code`)。<br><br>授权码有效期为 1 小时且只能使用一次。<br><br>若想了解您使用的 API 类别对应的授权码获取方式,可查看[不同 API 的授权流程对比](https://business-api.tiktok.com/portal/docs?id=1781891256601602#item-link-%E4%B8%8D%E5%90%8C%20API%20%E7%9A%84%E6%8E%88%E6%9D%83%E6%B5%81%E7%A8%8B%E5%AF%B9%E6%AF%94)中的"<strong>OAuth 授权 URL</strong>"行。  |
|return_advertiser_ids|boolean|是否在返回中通过 `advertiser_ids` 返回生成的访问令牌可以访问的广告账号完整列表。<br><br>支持的值:`true`,`false`。<br><br>默认值:`true`。<br><br><p><span style="color:darkred"><b>注意</b></span>:如果您拥有数千个或更多广告账户的访问权限,将此字段设置为 `false` 可避免在获取所有广告主 ID 完整列表导致的超时。</p>|

示例

(code curl http)
curl --location -request POST 'https://business-api.tiktok.com/open_api/v1.3/oauth2/access_token/' \
--header 'Content-Type: application/json' \
--data '{
    "app_id": "{{app_id}}",
    "secret": "{{secret}}",
    "auth_code": "{{auth_code}}"
}'
(/code)

返回

|字段{30%}|类型{15%}|描述{55%}|
|---|---|---|
|code |number|返回码。完整返回码列表及描述,可查看[附录-返回码](https://ads.tiktok.com/marketing_api/docs?id=1737172488964097)。 |
|message |string|返回信息。更多信息可查看[附录-返回码](https://ads.tiktok.com/marketing_api/docs?id=1737172488964097)。 |
|request_id |string|请求的日志 ID,唯一标识一个请求。  |
|data |object|返回数据。  |
#|access_token |string|用于权限验证的长期访问令牌。|
#|advertiser_ids |string[]|该访问令牌可以访问的广告账号列表。<br><br>如果请求中的 `return_advertiser_ids` 为 `false`,则本字段返回空字符串数组 `[]`。 |
#|scope |number[]|该访问令牌所拥有的权限。<br>您可查看[权限范围](https://ads.tiktok.com/marketing_api/docs?id=1753986142651394)了解权限范围 ID(`scope`的值)对应的权限。  |

示例

(code Success-Response http)
HTTPS/1.1 200 OK
{
    "code": 0,
    "message": "OK",
    "request_id": "{{request_id}}",
    "data": {
        "access_token": "{{access_token}}",
        "advertiser_ids": [
            "{{advertiser_id}}",
            "{{advertiser_id}}"
        ],
        "scope": [
            18000000,
            1,
            2,
            3,
            4,
            5,
            6,
            7,
            8,
            9,
            7043626160646946818,
            7050363942434013185,
            7263200645266554881,
            7294180478502830081,
            7302558519668703234,
            15000000,
            10000,
            19000000,
            12000000,
            7336834475000610818,
            16000000,
            20000000,
            13000000,
            17000000,
            14000000
        ]
    }
}
(/code);

/oauth/token/

您可以使用本接口获得访问令牌。支持将Content-Type设置为"application/x-www-form-urlencoded" 或"application/json"。本接口是TikTok API for Business v1.3中的全新接口。

请求

地址 https://business-api.tiktok.com/open_api/v1.3/oauth/token/

方法 POST

Header

|字段{30%}|类型 {15%}|描述{55%}|
|---|---|---|
|Content-Type {必填}|string|请求消息类型。 <br>允许值: [`"application/x-www-form-urlencoded"`, `"application/json"`]。 |

参数

|字段{30%}|类型 {15%}|描述{55%}|
|---|---|---|
| client_id {必填} | string |开发者应用 ID。<br><br>若想获取<strong>应用 ID</strong>,可以导航至<strong>[应用管理](https://ads.tiktok.com/marketing_api/apps/)</strong> > <strong>App Detail</strong> > <strong>基本信息</strong>。 |
| client_secret {必填} | string | 开发者应用密钥。<br><br>若想获取<strong>密钥</strong>,可以导航至<strong>[应用管理](https://ads.tiktok.com/marketing_api/apps/)</strong> > <strong>App Detail</strong> > <strong>基本信息</strong>。|
| code {必填} | string | 您的<strong>广告主授权 URL </strong>生成的授权码(`auth_code`)。<br><br>授权码有效期为 1 小时且只能使用一次。<br><br>若想了解您使用的 API 类别对应的授权码获取方式,可查看[不同 API 的授权流程对比](https://business-api.tiktok.com/portal/docs?id=1781891256601602#item-link-%E4%B8%8D%E5%90%8C%20API%20%E7%9A%84%E6%8E%88%E6%9D%83%E6%B5%81%E7%A8%8B%E5%AF%B9%E6%AF%94)中的"<strong>OAuth 授权 URL</strong>"行。  |
| grant_type {必填} | string | 生成访问令牌的方式。<br><br>允许的枚举值:<ul><li>`authorization_code`:通过授权码生成访问令牌。</li></ul> |

示例

Content-Type为application/x-www-form-urlencoded
(code curl http)
curl --location --request POST 'https://business-api.tiktok.com/open_api/v1.3/oauth/token/' \
--header 'Content-Type: application/x-www-form-urlencoded' \
--data-urlencode 'grant_type=authorization_code' \
--data-urlencode 'client_id={{client_id}}' \
--data-urlencode 'client_secret={{client_secret}}' \
--data-urlencode 'code={{code}}'
(/code)
Content-Type为"application/json"
(code curl http)
curl --location --request POST 'https://business-api.tiktok.com/open_api/v1.3/oauth/token/' \
--header 'Content-Type: application/json' \
--data-raw '{
    "grant_type": "authorization_code",
    "client_id": "{{client_id}}",
    "client_secret": "{{client_secret}}",
    "code": "{{code}}"
}'
(/code)

返回

|字段{30%}|类型 {15%}|描述{55%}|
|---|---|---|
|code |number| 返回码。完整返回码列表及描述,可查看[附录 - 返回码](https://ads.tiktok.com/marketing_api/docs?id=1737172488964097). |
|message |string| 返回信息。更多信息可查看[附录 - 返回码](https://ads.tiktok.com/marketing_api/docs?id=1737172488964097). |
| request_id | string | 请求的日志 ID,唯一标识一个请求。 |
| access_token | string | 用于权限验证的长期访问令牌。 |
| token_type | string |令牌类型。枚举值:`Bearer` (不记名令牌,拥有该令牌后任何人均可访问相关资源)。|
| advertiser_ids | string[] |该令牌可以访问的广告账号列表。 |
| scope | number[] |该令牌可以访问的接口。您可查看[权限范围](https://ads.tiktok.com/marketing_api/docs?id=1753986142651394),了解权限范围ID(`scope`的值)对应的权限。 |

示例

(code Success-Response http)
HTTPS/1.1 200 OK
{
    "scope": [
        16000000,
        1,
        2,
        3,
        4,
        5,
        6,
        7,
        8,
        9,
        7043626160646946818,
        7050363942434013185,
        7134840184444616705,
        11000000,
        18000000,
        15000000,
        10000,
        19000000,
        12000000,
        20000000,
        13000000,
        17000000,
        14000000
    ],
    "access_token": "{{access_token}}",
    "token_type": "Bearer",
    "advertiser_ids": [
        {{advertiser_id}}
    ],
    "code": 0,
    "message": "OK",
    "request_id": "{{request_id}}"
} 
(/code);

内容来源:TikTok API for Business 官方文档 · 本地归档

在接这个接口时卡住了?
Stuck wiring this API into production? We build ad-delivery tooling on these platforms — campaign automation, reporting pipelines, error-handling and migration between API versions.
联系我们 · Talk to us 每日平台更新简报 →