应用授权码
您可以使用本接口应用授权码。从视频主获得授权码后,授权并不会立即生效。您需要使用授权码将广告账户与Spark Ads帖子相连接。
对于拼接或者合拍帖子或者源视频提及了其他视频,广告主在推广帖子前还需要获得源视频的授权。在本接口中,你需要使用auth_code来传入当前视频的授权码,并用original_post_auth_code来传入源视频的授权码。
使用前需知
确保已从创作者(TikTok 帖子所有者)那里获得授权码。
你可以通过以下步骤,请创作者生成授权码:
1. 打开 TikTok 应用,点击想分享的帖子上的三点状图标。
2. 在分享菜单中点击“广告设置”。
3. 开启“广告授权”。
4. 选择授权时长。
5. 生成并复制授权码。
v1.2与v1.3版本对比
以下表格概括了 v1.2与v1.3接口的变化。
``xtable
|变化内容{30%}|v1.2{35%}|v1.3{35%}|
|---|---|---|
|接口路径| /v1.2/tt_video/authorize/| /v1.3/tt_video/authorize/|
|请求参数名称|from_auth_code|original_post_auth_code|
|请求参数类型 |advertiser_id: number|advertiser_id`: string|
## 请求
**地址**
https://business-api.tiktok.com/open_api/v1.3/tt_video/authorize/
**方法** POST
**Header**
``` xtable
|字段|类型|描述|
|---|---|---|
|Access-Token {必填}|string|授权访问令牌,可以通过[获取访问令牌](https://ads.tiktok.com/marketing_api/docs?id=1738373164380162)接口获取。 |
|Content-Type {必填}|string|请求消息类型。<br>允许值: `"application/json"`。 |
参数
|字段|类型|描述|
|---|---|---|
|advertiser_id {必填}|string| 广告主ID。 |
|auth_code {必填}|string| Spark Ads 帖子的授权码。 <br><br><p><span style="color:darkred"><b>注意</b></span>:将授权码中的“+”替换为“%2B”。否则,可能会出现授权码无效的报错提示。例如,你可以替换授权码“#XtjblzbEuOGK/BvHj4V/K0i71JL3Wu2fgRtOJrZSSf4SrPAedegtn+IH+UXr3f4=” 中的 “+”,传入 “#XtjblzbEuOGK/BvHj4V/K0i71JL3Wu2fgRtOJrZSSf4SrPAedegtn%2BIH%2BUXr3f4=”。|
|original_post_auth_code | string|如果本帖子是一个拼接或者合拍帖或者提及了其他视频,你还需要获得源视频的授权,并使用本字段传入源视频的授权码。<br>注意:目前,帖子只能提及一个视频。|
示例
(code Java java)
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import okhttp3.*;
import org.apache.http.client.utils.URIBuilder;
import java.io.IOException;
import java.net.URI;
import java.net.URISyntaxException;
import java.net.URL;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
public class Demo {
private static final String ACCESS_TOKEN = "xxx";
private static final String PATH = "/open_api/v1.3/tt_video/authorize/";
private static final ObjectMapper mapper = new ObjectMapper();
/**
* Build request URL
*
* @param path Request path
* @return Request URL
*/
private static String buildUrl(String path) throws URISyntaxException {
URI uri = new URI("https", "business-api.tiktok.com", path, "", "");
return uri.toString();
}
/**
* Send POST request
*
* @param jsonStr Args in JSON format
* @return Response in JSON format
*/
private static String post(String jsonStr) throws IOException, URISyntaxException {
OkHttpClient client = new OkHttpClient().newBuilder().build();
String url = buildUrl(PATH);
RequestBody body = RequestBody.create(MediaType.parse("application/json"), jsonStr);
Request request = new Request.Builder()
.url(url)
.method("POST", body)
.addHeader("Content-Type", "application/json")
.addHeader("Access-Token", ACCESS_TOKEN)
.build();
Response response = client.newCall(request).execute();
return response.body().string();
}
public static void main(String[] args) throws IOException, URISyntaxException {
String advertiser_id = ADVERTISER_ID;
String auth_code = AUTH_CODE;
// Args in JSON format
String myArgs = String.format("{\"advertiser_id\": \"%s\", \"auth_code\": \"%s\"}",advertiser_id, auth_code);
System.out.println(post(myArgs));
}
}
(/code)
(code Python python)
# coding=utf-8
import json
import requests
from six import string_types
from six.moves.urllib.parse import urlencode, urlunparse # noqa
ACCESS_TOKEN = "xxx"
PATH = "/open_api/v1.3/tt_video/authorize/"
def build_url(path, query=""):
# type: (str, str) -> str
"""
Build request URL
:param path: Request path
:param query: Querystring
:return: Request URL
"""
scheme, netloc = "https", "business-api.tiktok.com"
return urlunparse((scheme, netloc, path, "", query, ""))
def post(json_str):
# type: (str) -> dict
"""
Send POST request
:param json_str: Args in JSON format
:return: Response in JSON format
"""
url = build_url(PATH)
args = json.loads(json_str)
headers = {
"Access-Token": ACCESS_TOKEN,
"Content-Type": "application/json",
}
rsp = requests.post(url, headers=headers, json=args)
return rsp.json()
if __name__ == '__main__':
advertiser_id = ADVERTISER_ID
auth_code = AUTH_CODE
# Args in JSON format
my_args = "{\"advertiser_id\": \"%s\", \"auth_code\": \"%s\"}" % (advertiser_id, auth_code)
print(post(my_args))
(/code)
(code PHP php)
$ACCESS_TOKEN = "xxx";
$PATH = "/open_api/v1.3/tt_video/authorize/";
/**
* Build request URL
* @param $path : Request path
* @return string
*/
function build_url($path)
{
return "https://business-api.tiktok.com" . $path;
}
/**
* Send POST request
* @param $json_str : Args in JSON format
* @return bool|string : Response in JSON format
*/
function post($json_str)
{
global $ACCESS_TOKEN, $PATH;
$curl = curl_init();
$url = build_url($PATH);
curl_setopt_array($curl, array(
CURLOPT_URL => $url,
CURLOPT_RETURNTRANSFER => true,
CURLOPT_ENCODING => "",
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 0,
CURLOPT_FOLLOWLOCATION => true,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => "POST",
CURLOPT_POSTFIELDS => $json_str,
CURLOPT_HTTPHEADER => array(
"Content-Type: application/json",
"Access-Token: " . $ACCESS_TOKEN,
),
));
$response = curl_exec($curl);
curl_close($curl);
return $response;
}
$advertiser_id = ADVERTISER_ID;
$auth_code = AUTH_CODE;
/* Args in JSON format */
$my_args = sprintf("{\"advertiser_id\": \"%s\", \"auth_code\": \"%s\"}", $advertiser_id, $auth_code);
echo post($my_args);
(/code)
(code curl http)
curl -H "Access-Token:xxx" -H "Content-Type:application/json" -X POST \
-d '{
"advertiser_id": "ADVERTISER_ID",
"auth_code": "AUTH_CODE"
}' \
https://business-api.tiktok.com/open_api/v1.3/tt_video/authorize/
(/code)
返回
|字段|类型|描述|
|---|---|---|
|code |number|返回码。完整返回码列表及描述,可查看[附录-返回码](https://ads.tiktok.com/marketing_api/docs?id=1737172488964097)。 |
|message |string|返回信息。更多信息可查看[附录-返回码](https://ads.tiktok.com/marketing_api/docs?id=1737172488964097)。 |
|data |object|返回数据。 |
|request_id |string|请求的日志ID,唯一标识一个请求。 |
示例
(code Success-Response http)
HTTPS/1.1 200 OK
{
"message": "OK",
"code": 0,
"data": {}
}
(/code);