TikTok Open API Client
This Python library provides a convenient way to interact with the TikTok Open API for authorization and publishing video and photo content. It handles the OAuth 2.0 authorization flow with PKCE and offers methods for posting videos (from file or URL) and photos.
Table of Contents
- Features
- Installation
- Getting Started
- Class Overview
- Available Scopes
- Error Handling
- Contributing
- License
Features
- OAuth 2.0 Authorization: Implements PKCE (Proof Key for Code Exchange) flow for secure authentication
- Token Management: Handles authorization code exchange, token refresh, and revocation
- Content Publishing: Supports both video and photo posts
- Creator Information: Retrieves details about the authenticated TikTok creator
- Upload Status: Tracks video upload progress
- Error Handling: Includes custom exceptions for various error scenarios
TikTok Documentation
Installation
pip install tiktok-api-client
Getting Started
Prerequisites
You'll need these credentials from your TikTok developer portal:
- Client Key
- Client Secret
- Redirect URI (must be registered in your developer portal)
Basic Usage
Available Scopes
SCOPES = [
"user.info.basic",
"video.list",
"video.upload",
"video.publish",
"user.info.profile",
"user.info.stats",
]
Pass a subset of these scopes to the scopes parameter when initializing the client.
Initialize the client and get authorization URL:
from tiktok_api_client import TikTok
# Initialize client
tik = TikTok(
client_key="your_client_key",
client_secret="your_client_secret",
redirect_uri="https://your-app.com/callback", use ngrok for testing
state={"user_id": "user1"}, # Optional tracking state
scopes=["video.publish", "user.info.basic"]
)
# Get authorization URL
auth_url = tik.get_authorization_url()
print(f"Authorize URL: {auth_url}")
https://www.tiktok.com/v2/auth/authorize/?client_key=sbaw0y8rutbd9qx3i1&response_type=code&scope=video.publish&redirect_uri=https%3A%2F%2Fyour-app-url.comp%2Fcallback&code_challenge=zzq1--trJ8aXxqsddsdsdsdrWCaatTBx4&state=%7B%27user_id%27%3A+%mami%27%2C+%name%27%3A+%27hello%27%7D&code_challenge_method=S256
# visit the endpoint to authentiicate Tiktok on the browser
Authorization Flow Notes
- The authorization process generates a
code_verifierstored intik.code_verifier - You must store this code verifier along with your state parameters for later verification
- After user authorization, TikTok redirects to your callback URL with an authorization code
Example callback response:
{
"code": "9xmQgPZMUTvILmosta-4eC-PXLGFRgjdzXspg2ybTc2AKGDt...",
"scopes": "user.info.basic,video.publish",
"state": "{'user_id': user1'}
}
Exchanging Authorization Code for Tokens
# Exchange code for tokens
# You can track your save info from the state returned earlier then, retrive code_verifier
# reinitialize tiktok in session or continue with previous instance if testing on terminal
tik = TikTok(
client_key="your_client_key",
client_secret="your_client_secret",
redirect_uri="https://your-app.com/callback",
)
tik.code_verifier = code_verifier #saved earlier
token_data = tik.exchange_code_for_token(code=code)
print("Access Token:", token_data["access_token"])
print("Refresh Token:", token_data["refresh_token"])
Example token response:
{
"access_token": "act.UD0znPSOyqFgRJVvF9Tr2Xc5bJYjOnRiGPpsvNxb1TX...",
"expires_in": 86400,
"open_id": "-000bMhnTFeW4SZCPZkPWZppArDnsFgvOa_f",
"refresh_expires_in": 31536000,
"refresh_token": "rft.BDkTqoVTZZm9kLAtll3Rf2JQq5vwlvy9XR3KvbQEIM...",
"scope": "user.info.basic,video.publish",
"token_type": "Bearer"
}
Token Management
-
Store both access and refresh tokens securely
-
Refresh tokens hourly using a cron job:
tik.refresh_access_token() # Uses stored refresh token # Or specify manually: tik.refresh_access_token(refresh_token="your_refresh_token")
-
Revoke tokens when needed:
tik.revoke_access_token()
Getting Creator Information
creator_info = tik.get_creator_info()
Example response:
{
"data": {
"duet_disabled": true,
"max_video_post_duration_sec": 3600,
"privacy_level_options": [
"FOLLOWER_OF_CREATOR",
"MUTUAL_FOLLOW_FRIENDS",
"SELF_ONLY"
],
"stitch_disabled": true,
"comment_disabled": false,
"creator_avatar_url": "https://p16-pu-sign-no.tiktokcdn-eu.com/...",
"creator_nickname": "username",
"creator_username": "userhandle"
},
"error": {
"code": "ok",
"message": "",
"log_id": "2025041606EF40DB44655DE1236366E029"
}
}
Content Publishing
Video Upload Options
There are two upload methods:
- Direct Post: Publishes immediately to public view
- Upload: Saves to draft folder
Required parameters:
titlesource(eitherFILE_UPLOAD,PULL_FROM_URL)upload_type(e.g.,POST_VIDEO_FILE,UPLOAD_VIDEO_FILE,POST_VIDEO_URLorUPLOAD_VIDEO_URL)
Posting a Video
# From local file, FOR DIRECT UPLOAD
response = tik.create_video(
title="Hello World",
source="FILE_UPLOAD",
upload_type="POST_VIDEO_FILE",
privacy_level="SELF_ONLY",
video_path="/path/to/video.mp4", #FROM LOCAL FILE SYSTEM
# OPTIONAL PARAMETERS
disable_comment=False,
disable_duet=False,
disable_comment=False,
disable_stitch=False,
video_cover_timestamp_ms=1000
)
Example response:
{"initial_response": {"data": {"publish_id": "v_pub_file~v2-1.7493784938978805793",
"upload_url": "https://open-upload-i18n.tiktokapis.com/upload?upload_id=7497888937493822177&upload_token=4534354-c51c-b4ef-0d67-svsdvsdvs"},
"error": {"code": "ok",
"message": "",
"log_id": "20250415DB4C60556301E49832D8623D21"}},
"final_response": None}
# From URL
response = tik.create_video(
title="Remote Video",
source="PULL_FROM_URL",
upload_type="POST_VIDEO_URL",
privacy_level="SELF_ONLY",
video_url="https://your-url.com/video.mp4"
)
Example response:
{
"data": {
"publish_id": "v_pub_url~v2-1.7493775763519964616"
},
"error": {
"code": "ok",
"message": "",
"log_id": "20228DFF6051832504CD7615187FE36718"
}
}
These methoss requires your app verified by tiktok before use:
tik.create_video(title='hello', source='PULL_FROM_URL', upload_type="UPLOAD_VIDEO_URL", privacy_level="SELF_ONLY", video_url='https://your-url.com/files/video.mp4')
tik.create_video(title='hello', source='FILE_UPLOAD', upload_type="UPLOAD_VIDEO_FILE", privacy_level="SELF_ONLY", video_path='/path/to/video.mp4')
# "unaudited_client_can_only_post_to_private_accounts","message":"Please review our integration guidelines at https://developers.tiktok.com/doc/content-sharing-guidelines/
Photo Upload
# Direct post
response = tik.create_photo(
title="My Photos",
post_mode="DIRECT_POST",
privacy_level="SELF_ONLY",
photo_images=[
"https://example.com/photo1.png",
"https://example.com/photo2.png"
]
)
Example response:
{"photo_upload_response": {"data": {"publish_id": "p_pub_url~v2.43534534563534534"},
"error": {"code": "ok",
"message": "",
"log_id": "7CTFGVO483OOOOOOOOOOOO4"}}}
# Save to draft
response = tik.create_photo(
title="Draft Photos",
post_mode="MEDIA_UPLOAD",
privacy_level="SELF_ONLY",
photo_images=[
"https://example.com/photo1.png"
]
)
# "unaudited_client_can_only_post_to_private_accounts","message":"Please review our integration guidelines at https://developers.tiktok.com/doc/content-sharing-guidelines/
Checking Upload Status
status = tik.check_upload_status(publish_id="your_publish_id")
Class Reference
TikTok(client_key, client_secret, redirect_uri, state="", scopes=None)
Constructor parameters:
client_key: Your application's client keyclient_secret: Your application's client secretredirect_uri: Registered callback URIstate: Optional CSRF protection string/dictscopes: List of requested API scopes
Methods
get_authorization_url() -> strexchange_code_for_token(code: str) -> dictrefresh_access_token(refresh_token: str = None) -> dictrevoke_access_token(access_token: str = None) -> dictget_creator_info() -> dictcreate_video() -> dictcreate_photo() -> dictcheck_upload_status() -> dict
Available Scopes
[
"user.info.basic",
"video.list",
"video.upload",
"video.publish",
"user.info.profile",
"user.info.stats"
]
Error Handling
The library throws these exceptions:
TimeoutErrorfor request timeoutsHTTPErrorfor HTTP-related issues- General
Exceptionfor OAuth errors
Always use try-catch blocks for reliability.
Contributing
Contributions are welcome! Please:
- Fork the repository
- Create a feature branch
- Commit your changes
- Push to your fork
- Submit a pull request
License
MIT License. See MIT License for details.
Metadata
Release files for tiktok-api-client 0.0.15
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | Actions |
|---|---|---|---|---|
| tiktok_api_client-0.0.15.tar.gz | 14.9 kB | With provenance | Details for tiktok_api_client-0.0.15.tar.gz |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| tiktok_api_client-0.0.15-py3-none-any.whl | Python 3 | none | any | Details for tiktok_api_client-0.0.15-py3-none-any.whl |
Total release size: 26.7 kB
Release files / tiktok_api_client-0.0.15.tar.gz
| Download URL | tiktok_api_client-0.0.15.tar.gz |
|---|---|
| Size | 14.9 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
4387089fb4464ffb1b43f4af75892d91c008aa5543eebeed74af3b12847fb8ab
|
|
BLAKE2b-256 checksum How to use checksums |
2043ea6cb5014e123071b491d345474cf57a7eafacccbec75d35609e8313564a
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
twine/6.1.0 CPython/3.12.9
|
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
PyPI Publish Attestation
PyPI verified that this artifact, at this checksum, originated from the publisher listed below.
Signed by GitHub Actions, verified by PyPI on Apr 20, 2025.
Transparency logRelease files / tiktok_api_client-0.0.15-py3-none-any.whl
| Download URL | tiktok_api_client-0.0.15-py3-none-any.whl |
|---|---|
| Size | 11.8 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
0f8251c9440bada8d30e578bce23bdebf452d50975a5717f39dcdc476b98760d
|
|
BLAKE2b-256 checksum How to use checksums |
e1e054edd78f1a3ecc30bd0e26696faef3beda2854718007b9002f3ef4975af0
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
twine/6.1.0 CPython/3.12.9
|
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
PyPI Publish Attestation
PyPI verified that this artifact, at this checksum, originated from the publisher listed below.
Signed by GitHub Actions, verified by PyPI on Apr 20, 2025.
Transparency logSecurity for tiktok-api-client 0.0.15
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
Full provenance
Both files in this release have been uploaded with provenance metadata.
Attestation for tiktok_api_client-0.0.15.tar.gz
PyPI Publish Attestation
PyPI verified that this artifact, at this checksum, originated from the publisher listed below.
Signed by GitHub Actions, verified by PyPI on Apr 20, 2025.
Transparency logA note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.
To learn more about protecting yourself from malicious actors, visit our package security documentation.