tiktok-shop
Advanced tools
Comparing version 1.0.4 to 1.0.5
@@ -41,3 +41,3 @@ const crypto = require('crypto'); | ||
static getBaseUrl(url = '') { | ||
const parts = url.split("?"); | ||
const parts = url.split('?'); | ||
return { | ||
@@ -49,8 +49,8 @@ baseUrl: parts[0]+'?', | ||
static getPath(url = '') { | ||
const indexOfDotCom = url.indexOf(".com"); | ||
const indexOfQuestionMark = url.indexOf("?", indexOfDotCom); | ||
const indexOfDotCom = url.indexOf('.com'); | ||
const indexOfQuestionMark = url.indexOf('?', indexOfDotCom); | ||
return url.substring(indexOfDotCom, indexOfQuestionMark); | ||
} | ||
static sortKeyObject(pathObj = {}) { | ||
const declareKeyObj = ["app_secret", "token"] | ||
const declareKeyObj = ['app_secret', 'token', 'access_token', 'sign'] | ||
const keys = Object.keys(pathObj).filter((k) => !declareKeyObj.includes(k)); | ||
@@ -95,2 +95,51 @@ return keys.sort((a, b) => a.localeCompare(b)); | ||
} | ||
static signatureByAppSecret(params = {}, path = '', appSecret = '') { | ||
let input = ''; | ||
let timestamp = this.timestamp(); | ||
if (params.timestamp) { | ||
timestamp = params.timestamp; | ||
} | ||
const modParams = { ...params, timestamp }; | ||
const key = this.sortKeyObject(modParams); | ||
for(let index = 0; index < key.length; index += 1) { | ||
input+=key[index]+modParams[key[index]]; | ||
}; | ||
const plainText = appSecret+path+input+appSecret; | ||
const signature = this.sha256Decoded(plainText, appSecret); | ||
return { | ||
signature: signature, | ||
timestamp, | ||
} | ||
} | ||
static signByUrl(url = '', appSecret = '') { | ||
const { path, query } = this.getPathQueryFromUrl(decodeURIComponent(url)); | ||
const params = this.parseQueryString(query); | ||
return this.signatureByAppSecret(params, path, appSecret); | ||
} | ||
static getPathQueryFromUrl(url = '') { | ||
const parts = url.split('?'); | ||
const match = url.match(/\.com(.*?)\?/); | ||
return { | ||
path: match[1], | ||
query: parts[1], | ||
}; | ||
} | ||
static parseQueryString(queryString) { | ||
const obj = {}; | ||
queryString.split('&').forEach((keyValue) => { | ||
const [key, value] = keyValue.split('='); | ||
obj[key] = value; | ||
}); | ||
return obj; | ||
} | ||
static checkUrl(url, appSecret) { | ||
let error = ''; | ||
if (!url) { | ||
error = `url is required`; | ||
} | ||
if (!appSecret) { | ||
error = `appSecret is required`; | ||
} | ||
return error; | ||
} | ||
} |
@@ -60,2 +60,9 @@ const axios = require('axios'); | ||
} | ||
function signByUrl(url = '', appSecret = '') { | ||
const error = Common.checkUrl(url, appSecret); | ||
if (error) { | ||
return new Error(error); | ||
} | ||
return Common.signByUrl(url, appSecret); | ||
} | ||
module.exports = { | ||
@@ -65,2 +72,3 @@ signature, | ||
generateToken, | ||
signByUrl, | ||
} |
{ | ||
"name": "tiktok-shop", | ||
"version": "1.0.4", | ||
"version": "1.0.5", | ||
"description": "Generate \"signature\" and \"token\" for Tiktok Shop.", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
@@ -17,5 +17,6 @@ [![Tiktok-shop Logo](https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgWnWWI1yW_T2VRAYVgkByltWqH41X-Qfslgr2qHM3j64VVksDjz9CxzwbQ8M1vYlaB7QIN5pg0BUcxGP05kIcfXSNusvmeCkxVIQYkYyC12bHwuW__r9krtMPXN8yPhaXrcapdhDD70RE5vzjLb26D3d60STB5GFypF3OsNTnYhIrAtowx7eC54qJsrKGk/s1600/Untitled-2.png)](https://github.com/tudinhacoustic/tiktok-shop) | ||
3. [Community](#community) | ||
4. [Generate Signature](#generate-signature) | ||
5. [Generate Token using Auth Code](#generate-token-using-auth-code) | ||
6. [Generate Token using Refresh Token](#generate-token-using-refresh-token) | ||
4. [Generate Signature using Url](#generate-signature-using-url) | ||
5. [Generate Signature using Config](#generate-signature-using-config) | ||
6. [Generate Token using Auth Code](#generate-token-using-auth-code) | ||
7. [Generate Token using Refresh Token](#generate-token-using-refresh-token) | ||
@@ -47,6 +48,28 @@ ## Installation | ||
## Generate Signature | ||
## Generate Signature using Url | ||
```js | ||
const tiktokShop = require('tiktok-shop') | ||
// Example Url. | ||
// The package helps reorder the parameters and remove keys as Tiktok Shop's instructions. | ||
const url = 'https://open-api.tiktokglobalshop.com/order/202309/orders?access_token=ROW_CBxxx&app_key=6a6xxx&ids=5779xxx&shop_cipher=ROW_Y-vWxxx&shop_id=×tamp=1697708762&version=202309'; | ||
const appSecret = '4ebxxx'; | ||
const signature = tiktokShop.signByUrl(url, appSecret); | ||
console.info(signature); | ||
``` | ||
Response Data | ||
```console | ||
{ | ||
signature: '96f15922fbacd220cea0d8370ba7dff2273674f2a2856868b7e32f7d98da0efe', | ||
timestamp: 1697540200 | ||
} | ||
``` | ||
[Back](#content) | ||
## Generate Signature using Config | ||
```js | ||
const tiktokShop = require('tiktok-shop') | ||
// Extract all query param EXCEPT ' sign ', ' access_token ', You do not need to reorder the params based on alphabetical order. | ||
@@ -53,0 +76,0 @@ const config = { |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
14219
224
162