Skip to content

Commit

Permalink
migrate userinfo endpoint to from display api v1 to v2
Browse files Browse the repository at this point in the history
  • Loading branch information
fokosun committed Oct 11, 2023
1 parent a7e59ff commit 8436eaf
Show file tree
Hide file tree
Showing 5 changed files with 66 additions and 40 deletions.
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());
}
}
53 changes: 16 additions & 37 deletions app/Http/Clients/TikTokHttpClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
namespace App\Http\Clients;

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

Expand All @@ -13,19 +15,6 @@ 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,29 +45,17 @@ 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/',
$fields = implode( ',', UserInfoEnum::values());

$userInfoResponse = $this->client->request('GET',
$this->getV2DisplayApiEndpoint() . '/user/info/?fields=' . $fields,
[
'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'
],
],
'headers' => [
'Authorization' => 'Bearer ' . $access_token,
'Content-Type' => 'application/json'
]
]
);

Expand All @@ -87,8 +64,10 @@ public function getUserInfo(string $open_id, string $access_token)

public function listVideos(TikTokUserDto $userDto): array
{
$fields = implode( ',', VideoListEnum::values());

$response = $this->client->request('POST',
self::getVideoListEndpoint() . implode( ',', self::$claims),
$this->getV2DisplayApiEndpoint() . '/video/list/?fields=' . $fields,
[
'headers' => [
'Authorization' => 'Bearer ' . $userDto->getCode(),
Expand All @@ -115,8 +94,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 8436eaf

Please sign in to comment.