Skip to content

Commit

Permalink
Merge pull request #300 from mycookbook/migrate-tiktok-api-to-v2-disp…
Browse files Browse the repository at this point in the history
…lay-api

migrate userinfo endpoint to from display api v1 to v2
  • Loading branch information
fokosun authored Oct 11, 2023
2 parents a7e59ff + 3145ec1 commit 8fa7976
Show file tree
Hide file tree
Showing 6 changed files with 108 additions and 52 deletions.
9 changes: 9 additions & 0 deletions app/Http/Clients/Enums/AllowedHttpMethod.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<?php

namespace App\Http\Clients\Enums;

enum AllowedHttpMethod: string
{
case GET = 'GET';
case POST = 'POST';
}
24 changes: 24 additions & 0 deletions app/Http/Clients/Enums/UserInfoEnum.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<?php

namespace App\Http\Clients\Enums;

enum UserInfoEnum: string
{
case OPEN_ID = 'open_id';
case AVATAR_URL = 'avatar_url';
case DISPLAY_NAME = 'display_name';
case AVATAR_URL_100 = 'avatar_url_100';
case IS_VERIFIED = 'is_verified';
case PROFILE_DEEP_LINK = 'profile_deep_link';
case BIO_DESCRIPTION = 'bio_description';
case AVATAR_LARGE_URL = 'avatar_large_url';
case UNION_ID = 'union_id';
case VIDEO_COUNT = 'video_count';

public static function values(): array
{
return array_map(function($case) {
return $case->value;
}, self::cases());
}
}
23 changes: 23 additions & 0 deletions app/Http/Clients/Enums/VideoListEnum.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?php

namespace App\Http\Clients\Enums;

enum VideoListEnum: string
{
case COVER_IMAGE_URL = 'cover_image_url';
case ID = 'id';
case TITLE = 'title';
case VIDEO_DESCRIPTION = 'video_description';
case DURATION = 'duration';
case HEIGHT = 'height';
case WIDTH = 'width';
case EMBED_HTML = 'embed_html';
case EMBED_LINK = 'embed_link';

public static function values(): array
{
return array_map(function($case) {
return $case->value;
}, self::cases());
}
}
98 changes: 49 additions & 49 deletions app/Http/Clients/TikTokHttpClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,27 +5,18 @@
namespace App\Http\Clients;

use App\Dtos\TikTokUserDto;
use App\Http\Clients\Enums\AllowedHttpMethod;
use App\Http\Clients\Enums\UserInfoEnum;
use App\Http\Clients\Enums\VideoListEnum;
use GuzzleHttp\Client;
use Illuminate\Support\Arr;
use Illuminate\Support\Facades\Log;

class TikTokHttpClient
{
protected array $config;
protected Client $client;

public static array $claims = [
'cover_image_url',
'id',
'title',
'video_description',
'duration',
'height',
'width',
'title',
'embed_html',
'embed_link'
];

public function __construct(Client $client)
{
$this->config = config('services.tiktok');
Expand Down Expand Up @@ -56,48 +47,57 @@ public function getAccessToken(string $code)
return $decoded;
}

public function getUserInfo(string $open_id, string $access_token)
public function getUserInfo(string $access_token)
{
$userInfoResponse = $this->client->request('POST',
$this->getV1BaseUri() . '/user/info/',
[
'json' => [
'open_id' => $open_id,
'access_token' => $access_token,
'fields' => [
'open_id',
'avatar_url',
'display_name',
'avatar_url_100',
'is_verified',
'profile_deep_link',
'bio_description',
'display_name',
'avatar_large_url',
'avatar_url_100',
'union_id',
'video_count'
],
],
]
return $this->makeHttpRequest(
AllowedHttpMethod::GET,
UserInfoEnum::values(),
['Authorization' => $access_token, 'Path' => '/user/info/?fields=']
);

return json_decode($userInfoResponse->getBody()->getContents(), true);
}

public function listVideos(TikTokUserDto $userDto): array
{
$response = $this->client->request('POST',
self::getVideoListEndpoint() . implode( ',', self::$claims),
[
'headers' => [
'Authorization' => 'Bearer ' . $userDto->getCode(),
'Content-Type' => 'application/json'
]
]
return $this->makeHttpRequest(
AllowedHttpMethod::POST,
VideoListEnum::values(),
['Authorization' => $userDto->getCode(), 'Path' => '/video/list/?fields=']
);
}

private function makeHttpRequest(AllowedHttpMethod $httpMethod, $fields = [], $headers = [])
{
$options = ['headers' => ['Content-Type' => 'application/json']];
$v2DisplayApiEndpoint = $this->getV2DisplayApiEndpoint();
$response = [];

if ($bearer = Arr::get($headers, 'Authorization')) {
$options['headers']['Authorization'] = 'Bearer ' . $bearer;
}

if ($path = Arr::get($headers, 'Path')) {
$v2DisplayApiEndpoint = $v2DisplayApiEndpoint . $path . implode( ',', $fields);
}

try {
$response = $this->client->request($httpMethod->value, $v2DisplayApiEndpoint, $options);

if ($response->getBody()->getContents() != '') {
$response = $response->getBody()->getContents();
}
} catch (\Exception $exception) {
Log::debug(
'Tiktok: error retrieving user info or listing videos',
[
'resource' => $path,
'errorMsg' => $exception->getMessage()
]
);

$response['_error'] = $exception->getMessage();
}

return json_decode($response->getBody()->getContents(), true);
return json_decode($response, true);
}

private function getV1BaseUri(): string
Expand All @@ -115,8 +115,8 @@ private function getClientSecret(): string
return Arr::get($this->config, 'client_secret');
}

public static function getVideoListEndpoint(): string
private function getV2DisplayApiEndpoint(): string
{
return 'https://open.tiktokapis.com/v2/video/list/?fields=';
return 'https://open.tiktokapis.com/v2';
}
}
2 changes: 1 addition & 1 deletion app/Http/Controllers/AuthController.php
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,7 @@ public function tikTokHandleCallback(Request $request, TikTokHttpClient $httpCli
throw new \Exception(json_encode($decoded));
}

$userInfo = $httpClient->getUserInfo($open_id, $access_token);
$userInfo = $httpClient->getUserInfo($access_token);

if (!empty($userInfo['data']['user'])) {
$open_id = Arr::get($userInfo, 'data.user.open_id');
Expand Down
4 changes: 2 additions & 2 deletions app/Listeners/GetTikTokUserVideos.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace App\Listeners;

use App\Http\Clients\Enums\VideoListEnum;
use App\Http\Clients\TikTokHttpClient;
use GuzzleHttp\Client;
use Illuminate\Support\Carbon;
Expand Down Expand Up @@ -39,8 +40,7 @@ public function handle(object $event): void
'Error listing TikTok Videos',
[
'errorMsg' => $exception->getMessage(),
'claims' => TikTokHttpClient::$claims,
'endpoint' => TikTokHttpClient::getVideoListEndpoint()
'fields' => VideoListEnum::values()
]
);
}
Expand Down

0 comments on commit 8fa7976

Please sign in to comment.