Authenticating with OAuth2 in Python - TikTok API for business developers

248 views Asked by At

I'm trying to make a Python script that connects with TikTok's API, for which I've made an app on https://business-api.tiktok.com/. However, I'm completely unable to get an access token out of their API. I've followed the instructions from this documentation:

https://developers.tiktok.com/doc/oauth-user-access-token-management

Running the following code:

    import requests
    
    headers = {"Content-Type": "application/x-www-form-urlencoded"}
    token_url = "https://ads.tiktok.com/open_api/oauth2/access_token_v2/"    
    data = {
            "client_key": app_id, 
            "client_secret": app_secret, 
            "code": my_authorization_code, 
            "grant_type": "authorization_code", 
            "redirect_uri": "https://www.mysamplewebsite.co.uk/"
           }
    r = requests.post(token_url, 
                      data=data, 
                      headers={"Content-Type": "application/x-www-form-urlencoded"}
                      )

just leads to the response:

"{"code": 40006, "message": "Be advised that API versions 1.1 and earlier are now deprecated. Please migrate to v1.3 as soon as possible to avoid any disruptions.", "request_id": "asdsadfjklahf123123", "data": {}}"

Which is bizarre since I'm following the latest API documentation from their own website.

I've also tried several Oauth2 modules but none have worked at all, e.g.:

    from requests_oauthlib import OAuth2Session
    oauth = OAuth2Session(app_id, redirect_uri=redirect_url, scope=[])
    
   
    token = oauth.fetch_token(
        token_url,
        authorization_response=authorization_code_url,
        client_secret=secret_key,
        auth=HTTPBasicAuth(app_id, secret_key)
    )
    

just leads to the error "MissingTokenError: (missing_token) Missing access token parameter."

The authorization code I'm using is the code found in the URL by following the advertiser authorization URL on the app info & authorizing with my login. The token URL is https://open.tiktokapis.com/v2/oauth/token/.

Can anyone see what I'm doing on here? Or is it something about TikTok's API that I'm missing? I can't find any useful instructions on how to do this online.

0

There are 0 answers