🔐 Step 1: Authenticate with TikTok Business API
Connect your TikTok Business account to access audience insights
🚀 Authenticate with TikTok📋 Configuration Status
App ID: 7498293660247195664
Redirect URI: http://www.socialdiscovery.ai/callback.php
Backend: PHP OAuth Handler Ready
Status: Not Authenticated
Audience Insights for #hashtag
Demographic breakdown and engagement metrics
📈 Detailed Metrics
| Dimension | Value | Percentage | Audience Size |
|---|
Raw API Response:
// API response will appear here
🔧 Required Backend Files
Create these PHP files on your server to enable the TikTok API integration:
📁 callback.php - OAuth Callback Handler
<?php
// TikTok OAuth Callback Handler
session_start();
$appId = '7498293660247195664';
$appSecret = '9fff36f2176744cba6c82f29a41f3c486cf9237a';
$redirectUri = 'http://www.socialdiscovery.ai/callback.php';
if (isset($_GET['code']) && isset($_GET['state'])) {
$authCode = $_GET['code'];
$state = $_GET['state'];
// Exchange authorization code for access token
$tokenUrl = 'https://business-api.tiktok.com/open_api/v1.3/oauth2/access_token/';
$postData = [
'app_id' => $appId,
'secret' => $appSecret,
'auth_code' => $authCode
];
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $tokenUrl);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($postData));
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, [
'Content-Type: application/json'
]);
$response = curl_exec($ch);
curl_close($ch);
$tokenData = json_decode($response, true);
if (isset($tokenData['data']['access_token'])) {
$_SESSION['access_token'] = $tokenData['data']['access_token'];
$_SESSION['advertiser_ids'] = $tokenData['data']['advertiser_ids'];
// Redirect back to main page with success
header('Location: index.html?auth=success');
exit;
} else {
header('Location: index.html?auth=error&msg=' . urlencode('Token exchange failed'));
exit;
}
} else {
header('Location: index.html?auth=error&msg=' . urlencode('Authorization failed'));
exit;
}
?>
📁 api.php - API Proxy Handler
<?php
// TikTok API Proxy
header('Content-Type: application/json');
header('Access-Control-Allow-Origin: *');
header('Access-Control-Allow-Methods: POST, GET, OPTIONS');
header('Access-Control-Allow-Headers: Content-Type');
session_start();
if ($_SERVER['REQUEST_METHOD'] === 'OPTIONS') {
exit(0);
}
if (!isset($_SESSION['access_token'])) {
http_response_code(401);
echo json_encode(['error' => 'Not authenticated']);
exit;
}
$input = json_decode(file_get_contents('php://input'), true);
$action = $input['action'] ?? '';
switch ($action) {
case 'get_insights':
getHashtagInsights($input);
break;
default:
http_response_code(400);
echo json_encode(['error' => 'Invalid action']);
break;
}
function getHashtagInsights($input) {
$accessToken = $_SESSION['access_token'];
$advertiserId = $input['advertiser_id'];
$hashtag = $input['hashtag'];
$dimension = $input['dimension'] ?? 'age';
// TikTok Audience Insights API endpoint
$apiUrl = 'https://business-api.tiktok.com/open_api/v1.3/audience/insight/info/';
$postData = [
'advertiser_id' => $advertiserId,
'hashtag_ids' => [getHashtagId($hashtag)], // You'd need to implement hashtag ID lookup
'dimensions' => [$dimension],
'start_date' => date('Y-m-d', strtotime('-30 days')),
'end_date' => date('Y-m-d')
];
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $apiUrl);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($postData));
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, [
'Content-Type: application/json',
'Access-Token: ' . $accessToken
]);
$response = curl_exec($ch);
$httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
curl_close($ch);
if ($httpCode === 200) {
echo $response;
} else {
http_response_code($httpCode);
echo json_encode(['error' => 'API request failed', 'details' => $response]);
}
}
function getHashtagId($hashtag) {
// In a real implementation, you'd use TikTok's hashtag search API
// For demo purposes, return a simulated ID
return 'hashtag_' . hash('crc32', $hashtag);
}
?>