I'm working on integrating the TikTok API in a Node.js application and facing an issue with the "Get Order Settlements" endpoint. Despite following the API documentation and successfully implementing other endpoints, this particular call keeps returning a "signature is invalid" error.
Environment:
Node.js
Express.js
MongoDB
Problematic Endpoint: /api/finance/order/settlements
Issue: The API call to get order settlements consistently fails with the error message: "Error: signature is invalid". I'm using a custom function generateSignature for creating HMAC-SHA256 signatures.
Here's my generateSignature function:
function generateSignature(path, params, secret) {
const sortedKeys = Object.keys(params).sort();
let input = path;
sortedKeys.forEach(key => {
input += key + params[key];
});
input = secret + input + secret;
const hmac = crypto.createHmac('sha256', secret);
hmac.update(input);
return hmac.digest('hex');
}
Express.js Controller Function:
// Simplified version for brevity
exports.getTikTokOrderSettlements = async (req, res) => {
// ... [setup and validation code]
const queryParams = {
app_key: appKey,
timestamp: timestamp,
access_token: accessToken,
shop_id: shopId,
order_id: order_id,
};
const sign = generateSignature(path, queryParams, secret);
queryParams.sign = sign;
const queryString = new URLSearchParams(queryParams).toString();
const tiktokApiUrl = `https://open-api.tiktokglobalshop.com${path}?${queryString}`;
// ... [API call using fetch]
};
This method has worked for other endpoints (e.g., getting authorized shops), but not for the order settlements endpoint. The error persists even after double-checking the API key, secret, and other parameters.
What I've Tried:
- Ensuring all relevant query parameters are included in the signature.
- Checking parameter order and encoding.
- Synchronizing timestamps to GMT (UTC+00:00).
- Using hardcoded values from a known working example (from TikTok's API testing tool).
I'm at a loss as to why this specific endpoint is failing. Is there something I'm missing in the signature generation process, or is there a known issue with this TikTok API endpoint?
Any insights or suggestions would be greatly appreciated.