TikTok-Api icon indicating copy to clipboard operation
TikTok-Api copied to clipboard

[FEATURE_REQUEST] - Getting Your Following List (With a detailed plan)

Open Bengejd opened this issue 3 years ago • 6 comments

So, after some digging in the network tab, it looks like the logged-in user's following list can be retrieved via these URLS (on browser at least):

Initial List

For whatever reason, when you first access the web version, you will send off a call some (count varries) of your top accounts:

https://m.tiktok.com/api/user/list/?aid=1988&app_name=tiktok_web&device_platform=web&referer=&root_referer=&user_agent=Mozilla%2F5.0+(Windows+NT+10.0%3B+Win64%3B+x64)+AppleWebKit%2F537.36+(KHTML,+like+Gecko)+Chrome%2F87.0.4280.101+Safari%2F537.36&cookie_enabled=true&screen_width=1920&screen_height=1080&browser_language=en-US&browser_platform=Win32&browser_name=Mozilla&browser_version=5.0+(Windows+NT+10.0%3B+Win64%3B+x64)+AppleWebKit%2F537.36+(KHTML,+like+Gecko)+Chrome%2F87.0.4280.101+Safari%2F537.36&browser_online=true&ac=4g&timezone_name=America%2FChicago&page_referer=https:%2F%2Fwww.tiktok.com%2F%3Flang%3Den&priority_region=US&verifyFp=verify_kjelnia7_4eIWnvvy_Wovn_4u1X_9Uzr_iMOGlf4PnCxL&appId=1233&region=US&appType=m&isAndroid=false&isMobile=false&isIOS=false&OS=windows&did=6912857383401637381&tt-web-region=US&uid=6714635015340491781&scene=20&count=10&maxCursor=1609538059253&minCursor=0&_signature=_02B4Z6wo00d01jVg8jwAAIDDlfTnmpaQRII1YfaAANLE1a

The notable sections of this call are the last 5 parameters of the call though, which i'll explain more indepth in a minute:

    scene=20
    count=10
    maxCursor=1609538059253
    minCursor=0
    _signature=_02B4Z6wo00d01jVg8jwAAIDDlfTnmpaQRII1YfaAANLE1a

Secondary Initial List

followed by another call for the next set of initial accounts

https://m.tiktok.com/api/user/list/?aid=1988&app_name=tiktok_web&device_platform=web&referer=&root_referer=&user_agent=Mozilla%2F5.0+(Windows+NT+10.0%3B+Win64%3B+x64)+AppleWebKit%2F537.36+(KHTML,+like+Gecko)+Chrome%2F87.0.4280.101+Safari%2F537.36&cookie_enabled=true&screen_width=1920&screen_height=1080&browser_language=en-US&browser_platform=Win32&browser_name=Mozilla&browser_version=5.0+(Windows+NT+10.0%3B+Win64%3B+x64)+AppleWebKit%2F537.36+(KHTML,+like+Gecko)+Chrome%2F87.0.4280.101+Safari%2F537.36&browser_online=true&ac=4g&timezone_name=America%2FChicago&page_referer=https:%2F%2Fwww.tiktok.com%2F%3Flang%3Den&priority_region=US&verifyFp=verify_kjelnia7_4eIWnvvy_Wovn_4u1X_9Uzr_iMOGlf4PnCxL&appId=1233&region=US&appType=m&isAndroid=false&isMobile=false&isIOS=false&OS=windows&did=6912857383401637381&tt-web-region=US&uid=6714635015340491781&scene=21&count=10&maxCursor=0&minCursor=0&_signature=_02B4Z6wo00d01jVg8jwAAIDDlfTnmpaSS441YfaAANLE1b

This is very similar to the first call, with some notable differences:

    scene=21
    count=10
    maxCursor=0
    minCursor=0
    _signature=_02B4Z6wo00d01jVg8jwAAIDDlfTnmpaSS441YfaAANLE1b

How this is useful

So following the initial call for the first few accounts, you'll see that scene changed from 20 to 21 in the second call. It will stay 21 for the subsequent calls, for whatever reason. During testing, I found that 'count' was limited to a max of 199 results for the request. Any more and the request fails, so you can make this a variable in the call, just limited to 199 results at a time, or you can keep it to 10, up to you... So, the two important sections of the call are maxCursor and minCursor, which despite being a weird way to pull up the next list, is what we have to work with. So how do we get this information, you may be asking yourself? Well, you're in luck, because the second call, thankfully passes the subsequent call's min and max cursor back with it.

So the workflow comes out to be:

  1. initial call for first set of accounts
  2. Next 10 accounts call: Changing scene to 21, and setting min/max cursor to 0.
  3. Retrieve the next call's min/max values from the response (either in network tab, or by scraping the json page that it brings up).
  4. Change min/max cursor from 0 to the values retrieved in step 3, and then repeat steps 3/4 until you retrieve all of the accounts that you are interested in.

The issue I found though, is that this request requires a login instance to be attached to the browser... I tried to do more indepth testing using the requests library, but it would return an error: {"statusCode":10102,"statusMsg":"not login"}, which it also does, if you try to make the call incognito. Not entirely sure the workaround there, but I'm positive there is a way, considering everything else that this library can do.

You will eventually exhaust this list, and will be returned with a value of:

maxCursor: -1
minCursor: -1
statusCode: 0

Letting you know that you've reached the end, and to shut off future calls. You could also keep track of how many results you've gotten, but this seems to be a simpler method, in my opinion.

And there you have it. Any thoughts?

Bengejd avatar Jan 02 '21 07:01 Bengejd

Issue-Label Bot is automatically applying the label feature_request to this issue, with a confidence of 0.78. Please mark this comment with :thumbsup: or :thumbsdown: to give our bot feedback!

Links: app homepage, dashboard and code for this bot.

issue-label-bot[bot] avatar Jan 02 '21 07:01 issue-label-bot[bot]

Also worth noting: The calls return a detailed user profile list, including just about everything you'd expect to get from using the regular api.getUser method.

Bengejd avatar Jan 02 '21 07:01 Bengejd

After investigating this a bit further, it looks like if you include sessionid_ss in get_cookies() the request will go through, without a hitch.

Bengejd avatar Jan 02 '21 18:01 Bengejd

I'll submit a PR request here later tonight, after I make some adjustments to make it more scalable & pretty to look at 😂

Bengejd avatar Jan 02 '21 19:01 Bengejd

One thing I've noticed during testing is that this will not in fact get you your full following list. My assumption for the cause of this is because TikTok doesn't automatically unfollow accounts that are banned (as they still show up in the mobile app list). So if you are following 4,000 accounts, and it only brings back 3950 in the results, I am assuming that it's not showing you the banned accounts, but more testing will have to be done to verify that.

Bengejd avatar Jan 03 '21 00:01 Bengejd

Hi, anyone know how to get following list of a specified ID/username tiktok?

0902135xxx avatar May 26 '21 15:05 0902135xxx