1. TikTokApi
  2. msToken

Failed to get msToken

TikTokApi puts TikTok's msToken cookie on every call it signs. When a session never gets one, the calls still go out, and they usually fail.

The messages#

TikTokApi 7.3.3 has two, from two places in tiktok.py:

1Failed to get msToken on session index 0, you should consider specifying ms_tokens2Failed to get msToken from cookies, trying to make the request anyway (probably will fail)

The first is logged at INFO while a session is created, so the default level of logging.WARN hides it. The second is a WARNING from make_request, just before a call that goes out with the literal text msToken=None in its query string.

Where the token comes from#

create_sessions(ms_tokens=[...]) sets each token as the msToken cookie on its session's browser context and keeps a copy on the session. With no list, a session opens tiktok.com, waits sleep_after seconds (one, by default) and reads whatever cookie TikTok set by then. The docstring is frank about that route: "If you don't provide any, the sessions will try to get them themselves, but this is not guaranteed to work."

Every signed call then adds the session's token to its query. user.info(), hashtag.info(), sound.info() and playlist.info() also take an ms_token= keyword that wins over the session's.

How to tell which one it is#

You seeWhat happened
The INFO line during create_sessionsTikTok set no cookie within sleep_after
The WARNING before a callThat session still has no token, and its cookies have none either
EmptyResponseException right after the WARNINGTikTok refused the call that carried no token
User search returns nothing with your tokenThe search docstring: "Your ms_token needs to have done a search before for this to work."

To see the INFO line, create the client with TikTokApi(logging_level=logging.INFO).

How to fix it#

1. Give the cookie longer to arrive. The README's example uses sleep_after=3, and every session takes that long to open.

2. Pass a token from your browser. Open tiktok.com, find the msToken cookie in the developer tools (Application, then Cookies in Chrome) and hand it over. The README's comment says just that: "get your own ms_token from your cookies on tiktok.com". The value belongs to that browser's session, and a user in issue #1109 reports it changing often, so fetch a fresh one when calls start failing again.

1import os2from TikTokApi import TikTokApi34ms_token = os.environ.get("ms_token")5async with TikTokApi() as api:6    await api.create_sessions(ms_tokens=[ms_token], num_sessions=1, sleep_after=3)

3. Know how tokens are shared. Each session picks one entry from ms_tokens at random, so five sessions and a single token all carry that one token.

4. For user search, use a token that has searched. Run one search for a person on tiktok.com in the browser the token came from, as the search docstring asks.

No token to look after#

The SocialScrape API asks for no msToken and no cookie from a browser: your API key is the one credential. GET /v1/tiktok/search/users takes a query, and GET /v1/tiktok/profile takes a handle.

Either one is 1 credit per answer and free when it fails, and new accounts begin with 100 credits to spend on them.

Questions

What is msToken?

A cookie that tiktok.com sets in the browser. TikTokApi copies it into each call's query string, next to the X-Bogus signature it computes inside the page.

How long does an msToken last?

TikTokApi's docs do not say. Users in #1109 report both a value that changed quickly and one that kept working unchanged, so treat it as something to refresh.

Do I need a TikTok account for the token?

The library only asks for the cookie tiktok.com sets in the browser; its docs mention no account for it.

Why is the first message missing from my console?

It is logged at INFO, one step below the WARN level TikTokApi uses unless you pass logging_level.

100 free credits. No card needed.

Start pulling social data.