Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adding OAuth TikTok Business #674

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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;
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
);
}

/**
* 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,
];
}
}