Tích hợp GoLang SDK
Làm theo hướng dẫn để cài đặt GoLang SDK vào dự án của bạn và thực hiện lệnh gọi API đầu tiên bằng SDK. API thường dùng Search Products được sử dụng làm ví dụ.
Điều kiện tiên quyết
Trước khi tích hợp TikTok Shop API SDK vào dự án và thực hiện lệnh gọi API đầu tiên bằng SDK, bạn phải tạo một tài khoản người bán thử nghiệm.
Sau khi tài khoản được tạo, hãy truy cập vào tài khoản của bạn, đồng ý chia sẻ dữ liệu với TikTok Shop App thử nghiệm của bạn, và lấy authorization code trong return URL mà bạn đã chỉ định khi tạo TikTok Shop App thử nghiệm. Để được hướng dẫn, hãy tham khảo Tạo access token thử nghiệm.
Môi trường
Đảm bảo dự án của bạn đáp ứng tất cả các điều kiện sau:
- GoLang 1.18+
Cài đặt
- Giải nén gói đã tải xuống để lấy thư mục mã nguồn.
- Sao chép thư mục mã nguồn vào thư mục dự án của bạn.
- Tải xuống các phụ thuộc trong gói SDK. Chạy các lệnh sau trong thư mục gốc của dự án:
GOWord Wrap
go get github.com/stretchr/testify/assert
go get golang.org/x/net/context
- Thêm TikTok Shop API SDK vào các module được yêu cầu trong dự án của bạn. Thêm nội dung sau vào go.mod:
GOWord Wrap
// replace 1.0.0 with the version number of the SDK you downloaded.
require tiktokshop/open/<name-of-the-SDK-folder> v1.0.0
replace tiktokshop/open/<name-of-the-SDK-folder> => ./sdk_golang
- Biên dịch dự án bằng lệnh go build. Nếu không có lỗi, việc import SDK đã thành công.
Khởi tạo API Request Client
Import module tương ứng.
GOWord Wrap
import (
"context"
"fmt"
"log"
"testing"
"github.com/luci/go-render/render"
"tiktokshop/open/sdk_golang/apis"
product_V202502 "tiktokshop/open/sdk_golang/models/product/v202502"
"tiktokshop/open/sdk_golang/utils"
)
Thiết lập thông tin authorization cho API request.
GOWord Wrap
var (
appKey = "wiwiow594"
appSecret = "825cahe7h404u04j49dj80wc3d777759c"
authCode = "ROW_H4MBwAAAAADRK3uVWpMjrRJUtbDmh3gbNvOKafk6rg6fdVSoyUv06rX4-JR5za8dkBt6gExeFADqILbGgIdLIaw2bop28KEqOPLELKlQZXYLVeneQSLyrYPLoPt2wTO6rp0REOwMmIk2MANjNOLVt3AthiUjAThaUTLBeL07gd8upAv5hSCgjA"
)
Tạo một instance API request client.
GOWord Wrap
configuration := apis.NewConfiguration()
configuration.AddAppInfo(appKey, appSecret)
apiClient := apis.NewAPIClient(configuration)
request := apiClient.ProductV202502API.Product202502ProductsSearchPost(context.Background())
request = request.ContentType("application/json")
request = request.XTtsAccessToken(accessToken)
request = request.ShopCipher(shopCipher)
request = request.PageSize(1)
reqBody := product_V202502.Product202502SearchProductsRequestBody{
Status: utils.PtrString("ALL"),
}
request = request.Product202502SearchProductsRequestBody(reqBody)
Lấy Access Token
Bạn có thể sử dụng phương thức có sẵn trong SDK để lấy Access Token:
GOWord Wrap
at := apis.NewAccessToken(appKey, appSecret)
resp, err := at.GetToken(authCode)
if err != nil {
log.Printf("GetToken err:%v", err)
}
fmt.Printf("resp:%v", render.Render(resp))
if resp.Code != 0 {
log.Printf("resp code:%v", resp.Code)
return
}
fmt.Printf("AccessToken:%v", resp.Data.AccessToken)
fmt.Printf("RefreshToken:%v", resp.Data.RefreshToken)
accessToken := resp.Data.AccessToken
Lấy Shop Cipher
shop_cipher là một tham số trong query của API request của Search Products. Bạn sẽ cần sử dụng thuộc tính này để truyền thông tin shop khi yêu cầu tìm kiếm sản phẩm.
Để lấy shop_cipher, bạn sẽ cần gọi API Get Authorized Shops, API này cũng được cung cấp dưới dạng một phương thức trong SDK.
GOWord Wrap
shopsRequest := apiClient.AuthorizationV202502API.Authorization202502ShopsGet(ctx)
shopsRequest = shopsRequest.ContentType("application/json")
shopsRequest = shopsRequest.XTtsAccessToken(accessToken)
shopsResponse, httpRes, err := shopsRequest.Execute()
if err != nil || httpRes.StatusCode != 200 {
fmt.Printf("productsRequest err:%v resbody:%s", err, httpRes.Body)
return
}
if shopsResponse == nil {
fmt.Printf("response is nil")
return
}
if shopsResponse.GetCode() != 0 {
fmt.Printf("response business is error! errorCode:%d errorMessage:%s", shopsResponse.GetCode(), shopsResponse.GetMessage())
return
}
var shopCipher string
for _, shop := range shopsResponse.Data.Shops {
fmt.Printf("ShopID: %s, ShopCipher: %s \n", *shop.Id, *shop.Cipher)
}
shopCipher := *shop.Cipher
Search Products
Với Access Token và Shop Cipher, bạn có thể gọi API để truy xuất danh sách các sản phẩm đáp ứng các điều kiện đã chỉ định. Mỗi lần bạn thực hiện một API request, TikTok Shop sẽ gửi lại cho bạn một response. Hãy truy cập response và xử lý các lỗi có thể xảy ra.
GOWord Wrap
productsRequest := apiClient.ProductV202502API.Product202502ProductsSearchPost(ctx)
productsRequest = productsRequest.ContentType("application/json")
productsRequest = productsRequest.XTtsAccessToken(accessToken)
productsRequest = productsRequest.ShopCipher(shopCipher)
productsRequest = productsRequest.PageSize(1)
reqBody := product_V202502.Product202502SearchProductsRequestBody{
Status: utils.PtrString("ALL"),
}
productsRequest = productsRequest.Product202502SearchProductsRequestBody(reqBody)
searchProductsResponse, httpRes, err := productsRequest.Execute()
if err != nil || httpRes.StatusCode != 200 {
fmt.Printf("productsRequest err:%v resbody:%s", err, httpRes.Body)
return
}
if searchProductsResponse == nil {
fmt.Printf("response is nil")
return
}
if searchProductsResponse.GetCode() != 0 {
fmt.Printf("response business is error! errorCode:%d errorMessage:%s", searchProductsResponse.GetCode(), searchProductsResponse.GetMessage())
return
}
fmt.Println("searchProductsResponse data := ", render.Render(searchProductsResponse.GetData()))
Demo mã nguồn
GOWord Wrap
package main
import (
"context"
"fmt"
"log"
"testing"
"github.com/luci/go-render/render"
"tiktokshop/open/sdk_golang/apis"
product_V202502 "tiktokshop/open/sdk_golang/models/product/v202502"
"tiktokshop/open/sdk_golang/utils"
)
var (
appKey = "wiwiow594"
appSecret = "825cahe7h404u04j49dj80wc3d777759c"
authCode = "ROW_H4MBwAAAAADRK3uVWpMjrRJUtbDmh3gbNvOKafk6rg6fdVSoyUv06rX4-JR5za8dkBt6gExeFADqILbGgIdLIaw2bop28KEqOPLELKlQZXYLVeneQSLyrYPLoPt2wTO6rp0REOwMmIk2MANjNOLVt3AthiUjAThaUTLBeL07gd8upAv5hSCgjA"
)
// TestProduct202502ProductsSearchPost test Product202502ProductsSearchPost
func TestProduct202502ProductsSearchPost(t *testing.T) {
ctx := context.Background()
configuration := apis.NewConfiguration()
configuration.AddAppInfo(appKey, appSecret)
apiClient := apis.NewAPIClient(configuration)
// 1. get access_token
at := apis.NewAccessToken(appKey, appSecret)
resp, err := at.GetToken(authCode)
if err != nil {
log.Printf("GetToken err:%v", err)
}
fmt.Printf("resp:%v", render.Render(resp))
if resp.Code != 0 {
log.Printf("resp code:%v", resp.Code)
return
}
fmt.Printf("AccessToken:%v", resp.Data.AccessToken)
fmt.Printf("RefreshToken:%v", resp.Data.RefreshToken)
accessToken := resp.Data.AccessToken
// 2. get shop_cipher
shopsRequest := apiClient.AuthorizationV202502API.Authorization202502ShopsGet(ctx)
shopsRequest = shopsRequest.ContentType("application/json")
shopsRequest = shopsRequest.XTtsAccessToken(accessToken)
shopsResponse, httpRes, err := shopsRequest.Execute()
if err != nil || httpRes.StatusCode != 200 {
fmt.Printf("productsRequest err:%v resbody:%s", err, httpRes.Body)
return
}
if shopsResponse == nil {
fmt.Printf("response is nil")
return
}
if shopsResponse.GetCode() != 0 {
fmt.Printf("response business is error! errorCode:%d errorMessage:%s", shopsResponse.GetCode(), shopsResponse.GetMessage())
return
}
for _, shop := range shopsResponse.Data.Shops {
fmt.Printf("ShopID: %s, ShopCipher: %s \n", *shop.Id, *shop.Cipher)
}
shopCipher := "{{Your Shop Cipher}}"
// 3. search products
productsRequest := apiClient.ProductV202502API.Product202502ProductsSearchPost(ctx)
productsRequest = productsRequest.ContentType("application/json")
productsRequest = productsRequest.XTtsAccessToken(accessToken)
productsRequest = productsRequest.ShopCipher(shopCipher)
productsRequest = productsRequest.PageSize(1)
reqBody := product_V202502.Product202502SearchProductsRequestBody{
Status: utils.PtrString("ALL"),
}
productsRequest = productsRequest.Product202502SearchProductsRequestBody(reqBody)
searchProductsResponse, httpRes, err := productsRequest.Execute()
if err != nil || httpRes.StatusCode != 200 {
fmt.Printf("productsRequest err:%v resbody:%s", err, httpRes.Body)
return
}
if searchProductsResponse == nil {
fmt.Printf("response is nil")
return
}
if searchProductsResponse.GetCode() != 0 {
fmt.Printf("response business is error! errorCode:%d errorMessage:%s", searchProductsResponse.GetCode(), searchProductsResponse.GetMessage())
return
}
fmt.Println("searchProductsResponse data := ", render.Render(searchProductsResponse.GetData()))
}