From 145a8e0a2926b6eb05cc9bd7dd7ae28ebff4a8a2 Mon Sep 17 00:00:00 2001 From: fokosun Date: Thu, 12 Oct 2023 12:15:34 -0400 Subject: [PATCH] small updates --- app/Http/Clients/TikTokHttpClient.php | 37 +++++++++++++++------------ config/services.php | 10 ++++---- 2 files changed, 25 insertions(+), 22 deletions(-) diff --git a/app/Http/Clients/TikTokHttpClient.php b/app/Http/Clients/TikTokHttpClient.php index 330b056..4745475 100644 --- a/app/Http/Clients/TikTokHttpClient.php +++ b/app/Http/Clients/TikTokHttpClient.php @@ -27,7 +27,7 @@ public function getAccessToken(string $code) { $response = $this->client->request( 'POST', - $this->getV1BaseUri() . '/oauth/access_token/', + $this->getV1DisplayApiHostname() . '/oauth/access_token/', [ 'form_params' => [ 'client_key' => $this->getClientId(), @@ -52,7 +52,7 @@ public function getUserInfo(string $access_token): array return $this->makeHttpRequest( AllowedHttpMethod::GET, UserInfoEnum::values(), - ['Authorization' => $access_token, 'Path' => '/user/info/?fields='] + ['access_token' => $access_token, 'uri' => '/user/info/'] ); } @@ -61,32 +61,35 @@ public function listVideos(TikTokUserDto $userDto): array return $this->makeHttpRequest( AllowedHttpMethod::POST, VideoListEnum::values(), - ['Authorization' => $userDto->getCode(), 'Path' => '/video/list/?fields='] + ['access_token' => $userDto->getCode(), 'uri' => '/video/list/'] ); } - private function makeHttpRequest(AllowedHttpMethod $httpMethod, $fields = [], $headers = []): array + private function makeHttpRequest(AllowedHttpMethod $httpMethod, $fields = [], $options = []): array { - $options = ['headers' => ['Content-Type' => 'application/json']]; - $v2DisplayApiEndpoint = $this->getV2DisplayApiEndpoint(); + $headers = ['headers' => ['Content-Type' => 'application/json']]; + $hostname = $this->getV2DisplayApiHostname(); - if ($bearer = Arr::get($headers, 'Authorization')) { - $options['headers']['Authorization'] = 'Bearer ' . $bearer; + if ($bearer = Arr::get($options, 'access_token')) { + $headers['headers']['Authorization'] = 'Bearer ' . $bearer; } - if ($path = Arr::get($headers, 'Path')) { - $v2DisplayApiEndpoint = $v2DisplayApiEndpoint . $path . implode( ',', $fields); + if ($uri = Arr::get($options, 'uri')) { + $hostname = $hostname . $uri . '?fields=' . implode( ',', $fields); } try { - $response = $this->client->request($httpMethod->value, $v2DisplayApiEndpoint, $options); + $response = $this->client->request($httpMethod->value, $hostname, $headers); return json_decode($response->getBody()->getContents(), true); } catch (\Exception $exception) { Log::debug( 'Tiktok: error retrieving user info or listing videos', [ - 'resource' => $path, - 'errorMsg' => $exception->getMessage() + 'method' => $httpMethod, + 'uri' => $uri, + 'exception' => $exception, + 'options' => $options, + 'fields' => $fields ] ); @@ -94,9 +97,9 @@ private function makeHttpRequest(AllowedHttpMethod $httpMethod, $fields = [], $h } } - private function getV1BaseUri(): string + private function getV1DisplayApiHostname(): string { - return Arr::get($this->config, 'uri'); + return Arr::get($this->config, 'v1_host'); } private function getClientId(): string @@ -109,8 +112,8 @@ private function getClientSecret(): string return Arr::get($this->config, 'client_secret'); } - private function getV2DisplayApiEndpoint(): string + private function getV2DisplayApiHostname(): string { - return 'https://open.tiktokapis.com/v2'; + return Arr::get($this->config, 'v2_host'); } } diff --git a/config/services.php b/config/services.php index 790c951..68a3a80 100755 --- a/config/services.php +++ b/config/services.php @@ -31,15 +31,15 @@ 'secret' => env('AWS_SECRET_ACCESS_KEY'), 'region' => env('AWS_DEFAULT_REGION', 'us-east-1'), ], - 'tiktok' => [ - 'uri' => env('TIKTOK_URI', 'https://open-api.tiktok.com'), 'client_id' => env('TIKTOK_CLIENT_ID'), 'client_secret' => env('TIKTOK_CLIENT_SECRET'), 'redirect' => env('TIKTOK_REDIRECT_URI'), 'users' => [ 'secret_pass' => env('TIKOK_GENERIC_PASS', 'fakePass') - ] + ], + 'v1_host' => env('TIKTOK_URI', 'https://open-api.tiktok.com'), + 'v2_host' => env('TIKTOK_V2_API', 'https://open.tiktokapis.com/v2') ], 'ipinfo' => [ 'access_token' => env('IPINFO_SECRET', '') @@ -54,11 +54,11 @@ 'redirects' => [ 'tiktok' => [ 'web-client-vue2' => env('VUE2_APP_URL') . 'tiktok/?', - 'beta-version-1-staging' => env('NUXT_APP_URL') . '/tiktok/?' + 'beta-version-1-staging' => env('NUXT_APP_URL') . '/tiktok?' ], 'errors' => [ 'web-client-vue2' => env('VUE2_APP_URL') . 'errors/?', - 'beta-version-1-staging' => env('NUXT_APP_URL') . '/errors/?' + 'beta-version-1-staging' => env('NUXT_APP_URL') . '/errors?' ] ] ];