Device fraud detection SDK for TikTok Mini Program
The Fraud Detection software development kit (SDK) for TikTok mini programs collects device fingerprints and generates a device risk identifier (deviceToken). This topic describes how to download, configure, and initialize the SDK, obtain the deviceToken, and call the Fraud Detection API.
Background information
The TikTok mini program SDK is a Fraud Detection tool designed for the mini program environment. After you embed the SDK, you can integrate device fingerprinting, obtain device information in real time, and connect to the Fraud Detection service. The SDK supports scenarios such as registration, logon, and marketing campaigns to prevent fraudulent activities and improve business security.
Prerequisites
Before you integrate the SDK, complete the following preparations:
Complete service registration for Fraud Detection in the Alibaba Cloud Management Console.
The
appKeyof your mini program. TheappKeyidentifies your application. Obtain it fromDevice App Managementin the Alibaba Cloud Management Console.Add the domain names in
endpointsto the request whitelist in the TikTok mini program console. Otherwise, the SDK cannot send requests to the server. For the domain names, see Default endpoints.
SDK integration guide
Complete the following steps in order:
Step 1: Import the mini program SDK
Download theTikTok mini program SDK. (Recommended) Import the SDK on the index page, which is the first screen that the mini program loads, and mount the AliyunFP object globally so that other pages can access it.
import * as AliyunFP from './feilin.tiktok.xxxx.js';
const app = getApp();
app.AliyunFP = AliyunFP;Step 2: Add the feilin-view node to the TTML file
Before you call the mini program SDK operations, add the feilin-view node to the corresponding TTML file:
<view>
<view> Business UI layout </view>
<view id = "feilin-view" style="position:fixed;top:99999rpx;">
<canvas id="feilin-canvas" type="2d" style="width:150px;height:150px;"></canvas>
<canvas id="feilin-webgl" type="webgl" style="width:150px;height:150px;"></canvas>
</view>
</view>Step 3: Initialize the SDK
After you import the mini program SDK, initialize the SDK in the onReady lifecycle of the page.
Page({
onReady() {
AliyunFP.init({
appKey: 'Identifies your application. Obtain it from Device App Management in the console',
appName: 'Your mini program name',
openId: 'Mini program user ID, optional but recommended',
endpoints: ['https://cloudauth-device.aliyuncs.com'],
}, (initStatus, deviceToken) => {
console.log("initStatus:" + initStatus + " deviceToken:" + deviceToken);
});
}
});If you do not specify a service address, the endpoints parameter uses the default configurations. Add the domain names in endpoints to the request whitelist in the TikTok mini program console. Otherwise, the SDK cannot send requests to the server.
Default endpoints
The following table describes the default endpoints by region.
| Region | Default endpoint |
| The Chinese mainland (default) | https://cloudauth-device.aliyuncs.com |
Step 4: Obtain the deviceToken
To obtain the deviceToken, use the value returned by theAliyunFP.init callback. You can also callAliyunFP.getToken when a user triggers a business action such as registration, logon, or placing an order. Reserve an interval of at least 1 second betweenAliyunFP.getToken andAliyunFP.init.
AliyunFP.getToken()
// Pass bizId to bind this token to a unique business identifier
AliyunFP.getToken(bizId)Integration example
The following example shows a complete mini program page that integrates the Fraud Detection SDK. The page imports the SDK, initializes the SDK in the onReady lifecycle, and passes the deviceToken to the business server when a user logs on. The TTML structure of the page contains the same feilin-view node that Step 2: Add the feilin-view node to the TTML file describes.
Page structure
<view>
<view class="page-description"> {{ message }} </view>
<button type="primary" onTap="onLogin">Log on</button>
<view id = "feilin-view" style="position:fixed;top:99999rpx;">
<canvas id="feilin-canvas" type="2d" style="width:150px;height:150px;"></canvas>
<canvas id="feilin-webgl" type="webgl" style="width:150px;height:150px;"></canvas>
</view>
</view>Logic implementation
import * as AliyunFP from './feilin.tiktok.xxxx.js';
const app = getApp();
app.AliyunFP = AliyunFP;
Page({
onReady() {
AliyunFP.init({
appKey: 'Identifies your application. Obtain it from Device App Management in the console',
appName: 'Your mini program name',
openId: 'Mini program user ID, optional but recommended',
endpoints: ['https://cloudauth-device.aliyuncs.com'],
}, (initStatus, deviceToken) => {
console.log("initStatus:" + initStatus + " deviceToken:" + deviceToken);
});
},
onLogin() {
// Triggered when a user taps the logon button
console.info("onLogin!");
const deviceToken = AliyunFP.getToken();
// Pass the deviceToken to your business server
const data = {
"deviceToken": deviceToken,
"otherBusinessParameters": ""
};
// Send the request to your server. The server uses the deviceToken to query risk information.
}
});Call the Fraud Detection API
Submit the deviceToken together with your other business parameters to the Fraud Detection API as described inServer-side API integration.
FAQ
For answers to common questions about SDK integration, deviceToken retrieval, and initialization failures, see FAQ about Device Fraud Detection service integration.