Documentation
¶
Overview ¶
Package tiktok is a pure-Go, dependency-free, best-effort read client for public TikTok content, talking to TikTok's undocumented web JSON endpoints.
Best-effort and fragile by nature ¶
TikTok does not publish or support a stable public web API. The endpoints used here are the ones its own website calls, and TikTok actively defends them: requests frequently require a valid msToken query parameter/cookie, a signed "X-Bogus"/"_signature" parameter, and often a logged-in sessionid cookie. TikTok also returns anti-bot responses (HTTP 403/429, or a 200 with an empty "{}" body) when it decides a request looks automated.
Consequently this client is BEST-EFFORT: it builds correct requests and parses correct responses, but it can and will break without notice when TikTok changes its web API or tightens its bot defenses. Use it accordingly, respect TikTok's Terms of Service, and do not rely on it for anything critical. Supplying msToken via WithMSToken and a sessionid via WithSessionID improves — but does not guarantee — success.
Index ¶
Constants ¶
const DefaultBaseURL = "https://www.tiktok.com"
DefaultBaseURL is the default TikTok web origin.
const DefaultUserAgent = "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) " +
"AppleWebKit/537.36 (KHTML, like Gecko) Chrome/124.0.0.0 Safari/537.36"
DefaultUserAgent is a plausible desktop browser User-Agent. TikTok inspects this header; an empty or obviously automated value is more likely blocked.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Client ¶
type Client struct {
// BaseURL is the TikTok web origin (default https://www.tiktok.com).
BaseURL string
// HTTPClient performs requests (default http.DefaultClient).
HTTPClient *http.Client
// UserAgent is sent as the User-Agent header.
UserAgent string
// MSToken is TikTok's msToken, sent as both a query param and a cookie
// when non-empty.
MSToken string
// SessionID is the sessionid cookie for authenticated reads, sent when
// non-empty.
SessionID string
}
Client is a best-effort read client for public TikTok content.
A zero Client is not ready for use; construct one with New.
func (*Client) UserPosts ¶
func (c *Client) UserPosts(ctx context.Context, secUid string, count int, cursor string) (*UserFeed, error)
UserPosts fetches a user's recent videos via TikTok's web item_list API:
GET {BaseURL}/api/post/item_list/?secUid=<secUid>&count=<n>&cursor=<c>&...
The secUid is TikTok's opaque secondary user id; the caller obtains it once (for example from a profile page's embedded JSON) and passes it here. count is the requested page size and cursor is the pagination cursor ("0" or "" for the first page). Headers (User-Agent, Referer) and cookies (sessionid, msToken) are set when configured.
An empty result (no videos, HasMore=false) is returned without error. A non-2xx status, an empty/anti-bot body, or malformed JSON returns an error.
type Option ¶
type Option func(*Client)
Option configures a Client.
func WithBaseURL ¶
WithBaseURL overrides the TikTok web origin (useful for testing).
func WithHTTPClient ¶
WithHTTPClient sets the underlying http.Client.
func WithMSToken ¶
WithMSToken sets the msToken query param / cookie.
func WithSessionID ¶
WithSessionID sets the sessionid cookie for authenticated reads.
func WithUserAgent ¶
WithUserAgent overrides the User-Agent header.
type Video ¶
type Video struct {
ID string
Description string
Author string // unique_id / username
Permalink string // https://www.tiktok.com/@<author>/video/<id>
CoverURL string // thumbnail
PlayURL string // video URL (often expiring)
Likes int
Comments int
Plays int
CreateTime time.Time
}
Video is a single public TikTok video.