Skip to content

Commit

Permalink
small updates
Browse files Browse the repository at this point in the history
  • Loading branch information
fokosun committed Oct 12, 2023
1 parent 275a39b commit 145a8e0
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 22 deletions.
37 changes: 20 additions & 17 deletions app/Http/Clients/TikTokHttpClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -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(),
Expand All @@ -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/']
);
}

Expand All @@ -61,42 +61,45 @@ 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
]
);

return [];
}
}

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
Expand All @@ -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');
}
}
10 changes: 5 additions & 5 deletions config/services.php
Original file line number Diff line number Diff line change
Expand Up @@ -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', '')
Expand All @@ -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?'
]
]
];

0 comments on commit 145a8e0

Please sign in to comment.