Sample code for 30+ languages & platforms
SQL Server

TikTok Shop Get Categories

See more TikTok Shop Examples

An example showing how to use a TikTok Shops access token in an API call.

Chilkat SQL Server Downloads

SQL Server
-- 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

    -- Replace values in all caps with your specific values.
    -- SHOP_CIPHER and SHOP_ID are returned from this example: Get Authorized Shops
    DECLARE @queryParams int
    EXEC @hr = sp_OACreate 'Chilkat.JsonObject', @queryParams OUT

    EXEC sp_OAMethod @queryParams, 'UpdateString', @success OUT, 'app_key', 'APP_KEY'
    EXEC sp_OAMethod @queryParams, 'UpdateString', @success OUT, 'shop_cipher', 'SHOP_CIPHER'
    EXEC sp_OAMethod @queryParams, 'UpdateString', @success OUT, 'shop_id', 'SHOP_ID'
    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 = '/product/202309/categories'

    -- 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/product/202309/categories', @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

    DECLARE @id nvarchar(4000)

    DECLARE @is_leaf int

    DECLARE @local_name nvarchar(4000)

    DECLARE @parent_id nvarchar(4000)

    DECLARE @j int

    DECLARE @count_j int

    DECLARE @strVal 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.categories'
    WHILE @i < @count_i
      BEGIN
        EXEC sp_OASetProperty @json, 'I', @i
        EXEC sp_OAMethod @json, 'StringOf', @id OUT, 'data.categories[i].id'
        EXEC sp_OAMethod @json, 'BoolOf', @is_leaf OUT, 'data.categories[i].is_leaf'
        EXEC sp_OAMethod @json, 'StringOf', @local_name OUT, 'data.categories[i].local_name'

        PRINT 'local_name: ' + @local_name
        EXEC sp_OAMethod @json, 'StringOf', @parent_id OUT, 'data.categories[i].parent_id'
        SELECT @j = 0
        EXEC sp_OAMethod @json, 'SizeOfArray', @count_j OUT, 'data.categories[i].permission_statuses'
        WHILE @j < @count_j
          BEGIN
            EXEC sp_OASetProperty @json, 'J', @j
            EXEC sp_OAMethod @json, 'StringOf', @strVal OUT, 'data.categories[i].permission_statuses[j]'
            SELECT @j = @j + 1
          END
        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