Access Tokens - jstolpe/tiktok-business-ads-api-php-sdk GitHub Wiki
To get an access token, users must first login through the TikTok login dialog. TikTok will then send the user back to your redirect uri with a code variable in the url which you then exchange for an access token. We can also refresh or revoke an access token.
Display a link for the user to click on and login with TikTok. Once the user logs in with TikTok, TikTok will send them to the redirect uri. The redirect uri in your code must also be entered in the TikTok Login settings of you app under "Redirect URIs" section under "Login Kit".
// use auth class
use TikTokAds\Authentication\Authentication;
$authentication = new Authentication( array( // instantiate authentication
'app_id' => '<APP_ID>', // your app id
'app_secret' => '<APP_SECRET>' // your app secret
) );
// uri TikTok will send the user to after they login that must match what you have in your app dashboard
$redirectUri = 'https://path/to/tiktok/login/redirect.php';
// get login url
$advertiserAuthorizationUrl = $authentication->getAdvertiserAuthenticationUrl( $redirectUri );
// display login dialog link
echo '<a href="' . $advertiserAuthorizationUrl . '">' .
'Continue With TikTok' .
'</a>';
Once the user logs in through TikTok, TikTok redirects them to your redirect uri and appends on a code. For the above example, once the user logs in, TikTok would redirect them to "https://path/to/tiktok/login/redirect.php?code={code}". We then can exchange this code for an access token.
use TikTokAds\Authentication\Authentication;
$authentication = new Authentication( array( // instantiate authentication
'app_id' => '<APP_ID>', // your app id
'app_secret' => '<APP_SECRET>' // your app secret
) );
// get access token from code
$tokenFromCode = $authentication->generateAccessToken( $_GET['auth_code'] );
// access token
$userToken = $tokenFromCode['access_token'];