SQL Server
SQL Server
TikTok Shop Get Authorized Shops
See more TikTok Shop Examples
An example showing how to get the authorized shops in the TikTok Shops API.Chilkat SQL Server Downloads
-- Important: See this note about string length limitations for strings returned by sp_OAMethod calls.
--
CREATE PROCEDURE ChilkatSample
AS
BEGIN
DECLARE @hr int
DECLARE @iTmp0 int
-- Important: Do not use nvarchar(max). See the warning about using nvarchar(max).
DECLARE @sTmp0 nvarchar(4000)
DECLARE @success int
SELECT @success = 0
-- This example assumes the Chilkat API to have been previously unlocked.
-- See Global Unlock Sample for sample code.
DECLARE @http int
EXEC @hr = sp_OACreate 'Chilkat.Http', @http OUT
IF @hr <> 0
BEGIN
PRINT 'Failed to create ActiveX component'
RETURN
END
-- It is assumed we previously obtained an OAuth2 access token.
-- This example loads the JSON access token file
-- saved by this example: Get TikTok Shop OAuth2 Access Token
-- or refrehsed by this example: Get TikTok Shop Refresh OAuth2 Access Token
DECLARE @jsonToken int
EXEC @hr = sp_OACreate 'Chilkat.JsonObject', @jsonToken OUT
EXEC sp_OAMethod @jsonToken, 'LoadFile', @success OUT, 'qa_data/tokens/tiktok-shops.json'
IF @success <> 1
BEGIN
PRINT 'Failed to load tiktok-shops.json'
EXEC @hr = sp_OADestroy @http
EXEC @hr = sp_OADestroy @jsonToken
RETURN
END
DECLARE @queryParams int
EXEC @hr = sp_OACreate 'Chilkat.JsonObject', @queryParams OUT
EXEC sp_OAMethod @queryParams, 'UpdateString', @success OUT, 'app_key', 'APP_KEY'
DECLARE @dt int
EXEC @hr = sp_OACreate 'Chilkat.CkDateTime', @dt OUT
EXEC sp_OAMethod @dt, 'GetAsUnixTimeStr', @sTmp0 OUT, 0
EXEC sp_OAMethod @queryParams, 'UpdateString', @success OUT, 'timestamp', @sTmp0
EXEC sp_OAMethod @queryParams, 'UpdateInt', @success OUT, 'version', 202309
-- Sort the JSON members by member name, in ascending order (A-Z), case sensitive..
DECLARE @ascending int
SELECT @ascending = 1
DECLARE @caseSensitive int
SELECT @caseSensitive = 1
EXEC sp_OAMethod @queryParams, 'Sort', NULL, @ascending, @caseSensitive
DECLARE @appSecret nvarchar(4000)
SELECT @appSecret = 'APP_SECRET'
DECLARE @path nvarchar(4000)
SELECT @path = '/authorization/202309/shops'
-- Build the StringToSign
DECLARE @sb int
EXEC @hr = sp_OACreate 'Chilkat.StringBuilder', @sb OUT
EXEC sp_OAMethod @sb, 'Append', @success OUT, @appSecret
EXEC sp_OAMethod @sb, 'Append', @success OUT, @path
DECLARE @numParams int
EXEC sp_OAGetProperty @queryParams, 'Size', @numParams OUT
DECLARE @i int
SELECT @i = 0
WHILE @i < @numParams
BEGIN
EXEC sp_OAMethod @queryParams, 'NameAt', @sTmp0 OUT, @i
EXEC sp_OAMethod @sb, 'Append', @success OUT, @sTmp0
EXEC sp_OAMethod @queryParams, 'StringAt', @sTmp0 OUT, @i
EXEC sp_OAMethod @sb, 'Append', @success OUT, @sTmp0
SELECT @i = @i + 1
END
EXEC sp_OAMethod @sb, 'Append', @success OUT, @appSecret
DECLARE @crypt int
EXEC @hr = sp_OACreate 'Chilkat.Crypt2', @crypt OUT
EXEC sp_OASetProperty @crypt, 'HashAlgorithm', 'SHA256'
EXEC sp_OASetProperty @crypt, 'MacAlgorithm', 'HMAC'
EXEC sp_OASetProperty @crypt, 'EncodingMode', 'hex_lower'
EXEC sp_OAMethod @crypt, 'SetMacKeyString', @success OUT, @appSecret
DECLARE @sig nvarchar(4000)
EXEC sp_OAMethod @sb, 'GetAsString', @sTmp0 OUT
EXEC sp_OAMethod @crypt, 'MacStringENC', @sig OUT, @sTmp0
EXEC sp_OAMethod @jsonToken, 'StringOf', @sTmp0 OUT, 'data.access_token'
EXEC sp_OAMethod @queryParams, 'UpdateString', @success OUT, 'access_token', @sTmp0
EXEC sp_OAMethod @queryParams, 'UpdateString', @success OUT, 'sign', @sig
EXEC sp_OAMethod @jsonToken, 'StringOf', @sTmp0 OUT, 'data.access_token'
EXEC sp_OAMethod @http, 'SetRequestHeader', NULL, 'x-tts-access-token', @sTmp0
EXEC sp_OAMethod @http, 'SetRequestHeader', NULL, 'content-type', 'application/json'
DECLARE @resp int
EXEC @hr = sp_OACreate 'Chilkat.HttpResponse', @resp OUT
EXEC sp_OAMethod @http, 'HttpParams', @success OUT, 'GET', 'https://open-api.tiktokglobalshop.com/authorization/202309/shops', @queryParams, @resp
IF @success = 0
BEGIN
EXEC sp_OAGetProperty @http, 'LastErrorText', @sTmp0 OUT
PRINT @sTmp0
EXEC @hr = sp_OADestroy @http
EXEC @hr = sp_OADestroy @jsonToken
EXEC @hr = sp_OADestroy @queryParams
EXEC @hr = sp_OADestroy @dt
EXEC @hr = sp_OADestroy @sb
EXEC @hr = sp_OADestroy @crypt
EXEC @hr = sp_OADestroy @resp
RETURN
END
DECLARE @json int
EXEC @hr = sp_OACreate 'Chilkat.JsonObject', @json OUT
EXEC sp_OAMethod @resp, 'GetBodyJson', @success OUT, @json
EXEC sp_OAGetProperty @resp, 'StatusCode', @iTmp0 OUT
PRINT @iTmp0
EXEC sp_OAGetProperty @resp, 'BodyStr', @sTmp0 OUT
PRINT @sTmp0
-- Sample JSON response...
-- Use this online tool to generate parsing code from sample JSON:
-- Generate Parsing Code from JSON
-- {
-- "code": 0,
-- "data": {
-- "shops": [
-- {
-- "cipher": "GCP_XF90igAAAABh00qsWgtvOiGFNqyubMt3",
-- "code": "CNGBCBA4LLU8",
-- "id": "7000714532876273420",
-- "name": "Maomao beauty shop",
-- "region": "GB",
-- "seller_type": "CROSS_BORDER"
-- }
-- ]
-- },
-- "message": "Success",
-- "request_id": "202203070749000101890810281E8C70B7"
-- }
DECLARE @cipher nvarchar(4000)
DECLARE @code_str nvarchar(4000)
DECLARE @id nvarchar(4000)
DECLARE @name nvarchar(4000)
DECLARE @region nvarchar(4000)
DECLARE @seller_type nvarchar(4000)
DECLARE @code int
EXEC sp_OAMethod @json, 'IntOf', @code OUT, 'code'
DECLARE @message nvarchar(4000)
EXEC sp_OAMethod @json, 'StringOf', @message OUT, 'message'
DECLARE @request_id nvarchar(4000)
EXEC sp_OAMethod @json, 'StringOf', @request_id OUT, 'request_id'
SELECT @i = 0
DECLARE @count_i int
EXEC sp_OAMethod @json, 'SizeOfArray', @count_i OUT, 'data.shops'
WHILE @i < @count_i
BEGIN
EXEC sp_OASetProperty @json, 'I', @i
EXEC sp_OAMethod @json, 'StringOf', @cipher OUT, 'data.shops[i].cipher'
EXEC sp_OAMethod @json, 'StringOf', @code_str OUT, 'data.shops[i].code'
EXEC sp_OAMethod @json, 'StringOf', @id OUT, 'data.shops[i].id'
EXEC sp_OAMethod @json, 'StringOf', @name OUT, 'data.shops[i].name'
EXEC sp_OAMethod @json, 'StringOf', @region OUT, 'data.shops[i].region'
EXEC sp_OAMethod @json, 'StringOf', @seller_type OUT, 'data.shops[i].seller_type'
SELECT @i = @i + 1
END
EXEC @hr = sp_OADestroy @http
EXEC @hr = sp_OADestroy @jsonToken
EXEC @hr = sp_OADestroy @queryParams
EXEC @hr = sp_OADestroy @dt
EXEC @hr = sp_OADestroy @sb
EXEC @hr = sp_OADestroy @crypt
EXEC @hr = sp_OADestroy @resp
EXEC @hr = sp_OADestroy @json
END
GO