Skip to content

Commit

Permalink
feat(tiktok): adding oauth tiktok business
Browse files Browse the repository at this point in the history
  • Loading branch information
joaopalopes24 committed Nov 16, 2023
1 parent 7cec8ba commit d8fb5e9
Show file tree
Hide file tree
Showing 2 changed files with 134 additions and 0 deletions.
15 changes: 15 additions & 0 deletions src/SocialiteManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
use Laravel\Socialite\Two\LinkedInOpenIdProvider;
use Laravel\Socialite\Two\LinkedInProvider;
use Laravel\Socialite\Two\SlackProvider;
use Laravel\Socialite\Two\TiktokBusinessProvider;

Check failure on line 18 in src/SocialiteManager.php

View workflow job for this annotation

GitHub Actions / Static Analysis

Class Laravel\Socialite\Two\TikTokBusinessProvider referenced with incorrect case: Laravel\Socialite\Two\TiktokBusinessProvider.
use Laravel\Socialite\Two\TwitterProvider as TwitterOAuth2Provider;
use League\OAuth1\Client\Server\Twitter as TwitterServer;

Expand Down Expand Up @@ -184,6 +185,20 @@ protected function createSlackDriver()
);
}

/**
* Create an instance of the specified driver.
*
* @return \Laravel\Socialite\Two\AbstractProvider
*/
protected function createTiktokBusinessDriver()
{
$config = $this->config->get('services.tiktok-business');

return $this->buildProvider(
TikTokBusinessProvider::class, $config

Check failure on line 198 in src/SocialiteManager.php

View workflow job for this annotation

GitHub Actions / Static Analysis

Class Laravel\Socialite\Two\TikTokBusinessProvider referenced with incorrect case: Laravel\Socialite\Two\TiktokBusinessProvider.
);
}

/**
* Build an OAuth 2 provider instance.
*
Expand Down
119 changes: 119 additions & 0 deletions src/Two/TikTokBusinessProvider.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,119 @@
<?php

namespace Laravel\Socialite\Two;

use GuzzleHttp\RequestOptions;
use Illuminate\Support\Arr;

class TikTokBusinessProvider extends AbstractProvider implements ProviderInterface
{
/**
* The base TikTok Business API URL.
*
* @var string
*/
protected $businessUrl = 'https://business-api.tiktok.com';

/**
* The TikTok Business API version for the request.
*
* @var string
*/
protected $version = 'v1.3';

/**
* {@inheritdoc}
*/
protected function getAuthUrl($state)
{
return $this->buildAuthUrlFromBase("{$this->businessUrl}/portal/auth", $state);
}

/**
* {@inheritdoc}
*/
protected function getTokenUrl()
{
return "{$this->businessUrl}/open_api/{$this->version}/oauth2/access_token";
}

/**
* {@inheritdoc}
*/
protected function getUserByToken($token)
{
$response = $this->getHttpClient()->get("{$this->businessUrl}/open_api/{$this->version}/user/info", [
RequestOptions::HEADERS => ['Access-Token' => $token],
]);

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

/**
* {@inheritdoc}
*/
protected function mapUserToObject(array $user)
{
return (new User())->map([
'id' => Arr::get($user, 'data.core_user_id'),
'name' => Arr::get($user, 'data.display_name'),
'email' => Arr::get($user, 'data.email'),
'token' => Arr::get($user, 'token'),
]);
}

/**
* {@inheritdoc}
*/
protected function userInstance(array $response, array $user)
{
$this->user = $this->mapUserToObject($user);

return $this->user->setToken(Arr::get($response, 'access_token'));
}

/**
* {@inheritdoc}
*/
public function getAccessTokenResponse($code)
{
$response = $this->getHttpClient()->post($this->getTokenUrl(), [
RequestOptions::HEADERS => $this->getTokenHeaders($code),
RequestOptions::JSON => $this->getTokenFields($code),
]);

return Arr::get(json_decode($response->getBody(), true), 'data');
}

/**
* {@inheritdoc}
*/
protected function getCode()
{
return $this->request->input('auth_code');
}

/**
* {@inheritdoc}
*/
protected function getCodeFields($state = null)
{
$fields = parent::getCodeFields($state);

$fields['app_id'] = $this->clientId;

return Arr::only($fields, ['app_id', 'state', 'redirect_uri']);
}

/**
* {@inheritdoc}
*/
protected function getTokenFields($code)
{
return [
'auth_code' => $code,
'app_id' => $this->clientId,
'secret' => $this->clientSecret,
];
}
}

0 comments on commit d8fb5e9

Please sign in to comment.