I am trying to call API from Tiktok Open API. But I’m stuck at creating a signature (actually, I don’t really know what signature is). Below is my work flow, step by step.
I have a partner account and this is what I have done:
- First, I create a request by API Testing Tool in this web:
- When the request is successfully executed, a cURL will appear in the top left box, I copy and paste it to Postman so I can know what fields I need to fill.
- After that, I use this python code to generate a new signature:
import requests
import json
import time
import hashlib
import hmac
import urllib.parse
# API credentials and details
API_KEY = '6cjes8d*****'
ACCESS_TOKEN = 'TTP_XbqVOAAAAACPp7FUiaJiu9mqEw6ioT2rr_y37DAu38i7q5-YsGbonCb_J7lnNoWBPXNOBxHNGYCtzNxU4A0LdNveGAuzSaKL__IbmwM9ON-o5vU-VC0Qib1qSawqpdO9uyV0eD4uc_lEacZfr4OAiAkegBqsUvzW2TwEtCgGoDz4LBhhn*****' # change each time executing
SHOP_CIPHER = 'TTP_rg43SgAAAAAV8MA1WpxVEE8pacn*****'
SHOP_ID = '7495785205308492700'
API_SECRET = 'dc3027d983949c6091203ec04892c469c07*****'
VERSION = '202309'
BASE_URL = 'https://open-api.tiktokglobalshop.com/event/202309/webhooks'
def generate_signature(params, api_secret):
# Sort the parameters by key
sorted_params = sorted(params.items())
# Create the base string
base_string = api_secret + ''.join(f'{k}{v}' for k, v in sorted_params) + api_secret
# Generate HMAC-SHA256 signature
signature = hmac.new(api_secret.encode('utf-8'), base_string.encode('utf-8'), hashlib.sha256).hexdigest()
return signature
# Generate the current timestamp
timestamp = time.time()
# Prepare the parameters
params = {
'access_token': ACCESS_TOKEN,
'app_key': API_KEY,
'shop_cipher': SHOP_CIPHER,
'shop_id': SHOP_ID,
'timestamp': timestamp,
'version': VERSION,
'page_size': 10
}
# Generate the signature
signature = generate_signature(params, API_SECRET)
params['sign'] = signature
# URL with parameters
query_string = urllib.parse.urlencode(params)
full_url = f'{BASE_URL}?{query_string}'
# Headers
headers = {
'Content-Type': 'application/json',
'x-tts-access-token': ACCESS_TOKEN
}
# Data (Empty JSON object)
data = {}
# Make the POST request
response = requests.put(full_url, headers=headers, data=json.dumps(data), verify=True)
# Print the response
if response.status_code == 200:
print(response.json())
else:
print('Failed to fetch orders:', response.text)
But it always return error “signature is invalid”. Can anyone help me to solve this problem? Thank you!
Here is the document I read, I think it may helpful:
- https://partner.tiktokshop.com/docv2/page/6632a7c850b2bd02d91d21b4