![]() |
Chilkat HOME Android™ AutoIt C C# C++ Chilkat2-Python CkPython Classic ASP DataFlex Delphi DLL Go Java Node.js Objective-C PHP Extension Perl PowerBuilder PowerShell PureBasic Ruby SQL Server Swift Tcl Unicode C Unicode C++ VB.NET VBScript Visual Basic 6.0 Visual FoxPro Xojo Plugin
(PowerBuilder) Refresh a TikTok Shop OAuth2 Access TokenSee more TikTok Shop ExamplesRefreshes a TikTok Shop OAuth2 access token. When an access token expires, HTTPS requests will receive a 401 status response indicating failure. When that happens, your application can run this code to refresh the access token, and then retry the request using the new access token. Refreshing an access token does not need user interaction (i.e. does not need to display a browser to have the user interactive authorize access).
integer li_rc integer li_Success oleobject loo_JsonToken oleobject loo_Oauth2 oleobject loo_SbJson li_Success = 0 // This example requires the Chilkat API to have been previously unlocked. // See Global Unlock Sample for sample code. // 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 loo_JsonToken = create oleobject li_rc = loo_JsonToken.ConnectToNewObject("Chilkat.JsonObject") if li_rc < 0 then destroy loo_JsonToken MessageBox("Error","Connecting to COM object failed") return end if li_Success = loo_JsonToken.LoadFile("qa_data/tokens/tiktok-shops.json") if li_Success <> 1 then Write-Debug "Failed to load tiktok-shops.json" destroy loo_JsonToken return end if // The access token JSON looks like this: // { // "code": 0, // "message": "success", // "data": { // "access_token": "GCP_3n......", // "access_token_expire_in": 1722342011, // "refresh_token": "GCP_hA......", // "refresh_token_expire_in": 4841467787, // "open_id": "_VIz.......", // "seller_name": "****", // "seller_base_region": "GB", // "user_type": 0, // "granted_scopes": [ // "seller.authorization.info", // "seller.delivery.status.write", // "seller.product.basic", // "seller.order.info", // "seller.fulfillment.basic", // "seller.shop.info", // "seller.fulfillment.package.write", // "seller.product.write" // ] // }, // "request_id": "20240723......" // } loo_Oauth2 = create oleobject li_rc = loo_Oauth2.ConnectToNewObject("Chilkat.OAuth2") loo_Oauth2.TokenEndpoint = "https://auth.tiktok-shops.com/api/v2/token/refresh" // Replace these with actual values. loo_Oauth2.ClientId = "APP_KEY" loo_Oauth2.ClientSecret = "APP_SECRET" // Get the "refresh_token" loo_Oauth2.RefreshToken = loo_JsonToken.StringOf("data.refresh_token") // Send the HTTP request to refresh the access token.. li_Success = loo_Oauth2.RefreshAccessToken() if li_Success = 0 then Write-Debug loo_Oauth2.LastErrorText destroy loo_JsonToken destroy loo_Oauth2 return end if // Save the new JSON access token response to a file. loo_SbJson = create oleobject li_rc = loo_SbJson.ConnectToNewObject("Chilkat.StringBuilder") loo_JsonToken.EmitCompact = 0 loo_JsonToken.EmitSb(loo_SbJson) loo_SbJson.WriteFile("qa_data/tokens/tiktok-shops.json","utf-8",0) Write-Debug "OAuth2 token refreshed!" Write-Debug "New Access Token = " + loo_Oauth2.AccessToken destroy loo_JsonToken destroy loo_Oauth2 destroy loo_SbJson |
© 2000-2025 Chilkat Software, Inc. All Rights Reserved.