pythonpython-3.xtiktoktiktok-api

How to fetch tiktok ads data using python


I need to fetch data from tiktok for Campaign, Ad Group and Ad, this is what I tried

import requests

url = "https://business-api.tiktok.com/open_api/v1.3/campaign/get/"

access_token = "your_access_token"

headers = {
    "Authorization": f"Bearer {access_token}"
}

response = requests.get(url, headers=headers)

if response.status_code == 200:
    print(response.json()) 
else:
    print(f"Failed to fetch ad campaigns. Status code: {response.status_code}")
    print(None)

But this is giving error as below

{'code': 40104, 'message': 'Access token is null, you should set it in http header with key Access-Token.', 'request_id': '', 'data': {}}

Solution

  • Your header is wrong (as it also says in the error), it has to be:

    headers = {
        "Access-Token": "your_access_token"
    }
    

    Official documentation: https://ads.tiktok.com/marketing_api/docs?id=1739315828649986