-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathquickstart.py
More file actions
70 lines (57 loc) · 2.68 KB
/
Copy pathquickstart.py
File metadata and controls
70 lines (57 loc) · 2.68 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
"""TokPortal quickstart with the official SDK (pip install tokportal).
create bundle -> upload -> configure account + video -> publish -> read account
pip install -r requirements.txt
TOKPORTAL_API_KEY=sk_... python quickstart.py ./launch.mp4
NOTE: creating a bundle debits credits immediately. Not run against a sandbox -
syntactically valid; endpoints and fields match https://developers.tokportal.com/openapi.json.
"""
import datetime as dt
import os
import sys
from tokportal import TokPortal, TokPortalApiError
api_key = os.environ.get("TOKPORTAL_API_KEY")
if not api_key:
raise SystemExit("Set TOKPORTAL_API_KEY (https://app.tokportal.com/developer/api-keys)")
video_file = sys.argv[1] if len(sys.argv) > 1 else "./launch.mp4"
publish_date = (dt.date.today() + dt.timedelta(days=7)).isoformat()
client = TokPortal(api_key=api_key)
try:
print("1) authenticated as", client.me()["data"]["email"])
bundle = client.bundles.create({
"bundle_type": "account_and_videos",
"platform": "tiktok",
"country": "US",
"title": "US launch",
"videos_quantity": 1,
})
bundle_id = bundle["data"]["id"]
print("2) bundle created", bundle_id, "credits charged:", bundle.get("credits_charged"))
# Direct multipart upload from disk (one call). Presigned flow: see quickstart_requests.py
upload = client.uploads.video_direct(video_file, bundle_id, content_type="video/mp4")
public_url = upload["data"]["public_url"]
print("3) uploaded", public_url)
client.bundles.configure_account(bundle_id, {
"username": "mybrand.us",
"visible_name": "My Brand",
"biography": "Official account",
})
client.bundles.configure_video(bundle_id, 1, {
"video_type": "video",
"video_url": public_url,
"description": "Day 1 - launching in the US #launch",
"target_publish_date": publish_date,
})
print("4) account + video configured")
print("5) readiness", client.bundles.readiness(bundle_id)["data"])
client.bundles.publish(bundle_id)
print(" published")
current = client.bundles.get(bundle_id)["data"]
print("6) bundle status", current["status"], "saved_account_id", current.get("saved_account_id"))
if current.get("saved_account_id"):
account = client.accounts.get(current["saved_account_id"])["data"]
print(" account", account["username"], account["profile_url"])
else:
print(" account not delivered yet - subscribe to account.in_review / account.finalized webhooks or poll bundles.get()")
except TokPortalApiError as error:
print("API error", error.status_code, error.code, error.details, "request_id:", error.request_id)
raise SystemExit(1)