Skip to content

Commit

Permalink
Tiktok – Upgrade tiktok API URLs to use v2 (v1 deprecating in Sep 202…
Browse files Browse the repository at this point in the history
…3) (#1026)

Co-authored-by: atymic <atymicq@gmail.com>
  • Loading branch information
IsraelOrtuno and atymic authored May 25, 2023
1 parent c306089 commit 742ae17
Showing 1 changed file with 27 additions and 7 deletions.
34 changes: 27 additions & 7 deletions Provider.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@
use SocialiteProviders\Manager\OAuth2\AbstractProvider;
use SocialiteProviders\Manager\OAuth2\User;

/**
* @see https://developers.tiktok.com/bulletin/migration-guidance-oauth-v1/
* @see https://developers.tiktok.com/doc/oauth-user-access-token-management
*/
class Provider extends AbstractProvider
{
public const IDENTIFIER = 'TIKTOK';
Expand All @@ -28,13 +32,17 @@ class Provider extends AbstractProvider
*/
protected function getAuthUrl($state)
{
return 'https://open-api.tiktok.com/platform/oauth/connect?'.http_build_query([
$fields = [
'client_key' => $this->clientId,
'state' => $state,
'response_type' => 'code',
'scope' => $this->formatScopes($this->getScopes(), $this->scopeSeparator),
'redirect_uri' => $this->redirectUrl,
]);
];

$fields = array_merge($fields, $this->parameters);

return 'https://www.tiktok.com/v2/auth/authorize/?'.http_build_query($fields);
}

/**
Expand All @@ -52,24 +60,24 @@ public function user()

$response = $this->getAccessTokenResponse($this->getCode());

$token = Arr::get($response, 'data.access_token');
$token = Arr::get($response, 'access_token');

$this->user = $this->mapUserToObject(
$this->getUserByToken($token)
);

return $this->user->setToken($token)
->setExpiresIn(Arr::get($response, 'data.expires_in'))
->setRefreshToken(Arr::get($response, 'data.refresh_token'))
->setApprovedScopes(explode($this->scopeSeparator, Arr::get($response, 'data.scope', '')));
->setExpiresIn(Arr::get($response, 'expires_in'))
->setRefreshToken(Arr::get($response, 'refresh_token'))
->setApprovedScopes(explode($this->scopeSeparator, Arr::get($response, 'scope', '')));
}

/**
* {@inheritdoc}
*/
public function getTokenUrl()
{
return 'https://open-api.tiktok.com/oauth/access_token/';
return 'https://open.tiktokapis.com/v2/oauth/token/';
}

/**
Expand All @@ -82,6 +90,7 @@ protected function getTokenFields($code)
'client_secret' => $this->clientSecret,
'code' => $code,
'grant_type' => 'authorization_code',
'redirect_uri' => $this->redirectUrl,
];
}

Expand Down Expand Up @@ -116,4 +125,15 @@ protected function mapUserToObject($user)
'avatar' => $user['avatar_large_url'],
]);
}

/**
* {@inheritdoc}
*/
protected function getTokenHeaders($code)
{
return [
'Accept' => 'application/json',
'Content-Type' => 'application/x-www-form-urlencoded',
];
}
}

0 comments on commit 742ae17

Please sign in to comment.