brooklea
brooklea

Reputation: 1

How do I fix signature is invalid message when using tiktok shop api and python

I followed the "Sign your API request" page on the TikTok Shop partner website and wrote a function that generates the HMAC-SHA256 signature. I can generate the same signature as the guide if I use the example credentials, but when I pass my credentials through and make an API request, I get back

Failed to retrieve data. Status code: 401 {"code":106001,"message":"signature is invalid","request_id":"...

I have some experience with the eBay API but this TikTok one is a bit more challenging.

I believe my credentials are correct because when I use the TikTok API testing tool I get a success message.

import hashlib
import hmac
import json

with open('config.json', 'r') as config_file:
config = json.load(config_file)

app_key = '68xu9ks5p4i8' #Example from TikTok guide
shop_cipher = 'ROW_xkMbgAAAeVAQra0eZWebFQq5aIK' #Example from TikTok guide
timestamp = '1696909648' #Example from TikTok guide
app_secret = 'e59af819cc' #Example from TikTok guide
api_path = '/event/202309/webhooks' #Example from TikTok guide

address = 'https://partner.tiktokshop.com'
event_type = 'PACKAGE_UPDATE'

body = '{    "address":"https://partner.tiktokshop.com",    "event_type": "PACKAGE_UPDATE"}'

def generateSignature(app_key, shop_cipher, timestamp, app_secret, body, api_path):

    input = f'app_key{app_key}shop_cipher{shop_cipher}timestamp{timestamp}'
    print(input)
    
    input = f'{api_path}{input}'
    print(input)
    
    input = f'{input}{body}'
    print(input)
    
    input = f'{app_secret}{input}{app_secret}'
    print(input)
    
    input_bytes = input.encode('utf-8')
    secret_bytes = app_secret.encode('utf-8')
    
    h = hmac.new(secret_bytes, input_bytes, hashlib.sha256)
    
    hmac_digest = h.digest()
    
    print(hmac_digest.hex())
    return hmac_digest.hex()

#generateSignature(app_key, shop_cipher, timestamp, app_secret, body, api_path)

Upvotes: 0

Views: 890

Answers (0)

Related Questions