-
Notifications
You must be signed in to change notification settings - Fork 12
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Remove socialite from TikTok composer.json (#796)
- Loading branch information
0 parents
commit 435bd93
Showing
4 changed files
with
230 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,127 @@ | ||
<?php | ||
|
||
namespace SocialiteProviders\TikTok; | ||
|
||
use Illuminate\Support\Arr; | ||
use Laravel\Socialite\Two\InvalidStateException; | ||
use SocialiteProviders\Manager\OAuth2\AbstractProvider; | ||
use SocialiteProviders\Manager\OAuth2\User; | ||
|
||
class Provider extends AbstractProvider | ||
{ | ||
/** | ||
* Unique Provider Identifier. | ||
*/ | ||
public const IDENTIFIER = 'TIKTOK'; | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
protected $scopes = [ | ||
'user.info.basic', | ||
]; | ||
|
||
/** | ||
* @var User | ||
*/ | ||
protected $user; | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
protected function getAuthUrl($state) | ||
{ | ||
return 'https://open-api.tiktok.com/platform/oauth/connect?'.http_build_query([ | ||
'client_key' => $this->clientId, | ||
'state' => $state, | ||
'response_type' => 'code', | ||
'scope' => $this->formatScopes($this->getScopes(), $this->scopeSeparator), | ||
'redirect_uri' => $this->redirectUrl, | ||
]); | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function user() | ||
{ | ||
if ($this->user) { | ||
return $this->user; | ||
} | ||
|
||
if ($this->hasInvalidState()) { | ||
throw new InvalidStateException(); | ||
} | ||
|
||
$response = $this->getAccessTokenResponse($this->getCode()); | ||
|
||
$token = Arr::get($response, 'data.access_token'); | ||
|
||
$this->user = $this->mapUserToObject( | ||
$this->getUserByToken([ | ||
'access_token' => $token, | ||
'open_id' => Arr::get($response, 'data.open_id'), | ||
]) | ||
); | ||
|
||
return $this->user->setToken($token) | ||
->setExpiresIn(Arr::get($response, 'data.expires_in')) | ||
->setRefreshToken(Arr::get($response, 'data.refresh_token')); | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function getTokenUrl() | ||
{ | ||
return 'https://open-api.tiktok.com/oauth/access_token/'; | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
protected function getTokenFields($code) | ||
{ | ||
return [ | ||
'client_key' => $this->clientId, | ||
'client_secret' => $this->clientSecret, | ||
'code' => $code, | ||
'grant_type' => 'authorization_code', | ||
]; | ||
} | ||
|
||
/** | ||
* Get TikTok user by token. | ||
* | ||
* @param array $data | ||
* | ||
* @return mixed | ||
*/ | ||
protected function getUserByToken($data) | ||
{ | ||
// Note: The TikTok api does not have an endpoint to get a user by the access | ||
// token only. Open id is also required therefore: | ||
// $data['access_token'] = $token, $data['open_id'] = $open_id | ||
|
||
$response = $this->getHttpClient()->get( | ||
'https://open-api.tiktok.com/oauth/userinfo?'.http_build_query($data) | ||
); | ||
|
||
return json_decode((string) $response->getBody(), true); | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
protected function mapUserToObject($user) | ||
{ | ||
$user = $user['data']; | ||
|
||
return (new User())->setRaw($user)->map([ | ||
'id' => $user['open_id'], | ||
'union_id' => $user['union_id'], | ||
'name' => $user['display_name'], | ||
'avatar' => $user['avatar_larger'], | ||
]); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
# TikTok | ||
|
||
```bash | ||
composer require socialiteproviders/tiktok | ||
``` | ||
|
||
## Installation & Basic Usage | ||
|
||
Please see the [Base Installation Guide](https://socialiteproviders.com/usage/), then follow the provider specific instructions below. | ||
|
||
### Add configuration to `config/services.php` | ||
|
||
```php | ||
'tiktok' => [ | ||
'client_id' => env('TIKTOK_CLIENT_ID'), | ||
'client_secret' => env('TIKTOK_CLIENT_SECRET'), | ||
'redirect' => env('TIKTOK_REDIRECT_URI') | ||
], | ||
``` | ||
|
||
### Add provider event listener | ||
|
||
Configure the package's listener to listen for `SocialiteWasCalled` events. | ||
|
||
Add the event to your `listen[]` array in `app/Providers/EventServiceProvider`. See the [Base Installation Guide](https://socialiteproviders.com/usage/) for detailed instructions. | ||
|
||
```php | ||
protected $listen = [ | ||
\SocialiteProviders\Manager\SocialiteWasCalled::class => [ | ||
// ... other providers | ||
\SocialiteProviders\TikTok\TikTokExtendSocialite::class.'@handle', | ||
], | ||
]; | ||
``` | ||
|
||
### Usage | ||
|
||
You should now be able to use the provider like you would regularly use Socialite (assuming you have the facade installed): | ||
|
||
```php | ||
return Socialite::driver('tiktok')->redirect(); | ||
``` | ||
|
||
# Returned User Fields | ||
|
||
- id | ||
- union_id | ||
- name | ||
- avatar | ||
|
||
# Reference | ||
|
||
- [TikTok Login Kit](https://developers.tiktok.com/doc/login-kit-web) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
<?php | ||
|
||
namespace SocialiteProviders\TikTok; | ||
|
||
use SocialiteProviders\Manager\SocialiteWasCalled; | ||
|
||
class TikTokExtendSocialite | ||
{ | ||
/** | ||
* Register the provider. | ||
* | ||
* @param \SocialiteProviders\Manager\SocialiteWasCalled $socialiteWasCalled | ||
*/ | ||
public function handle(SocialiteWasCalled $socialiteWasCalled) | ||
{ | ||
$socialiteWasCalled->extendSocialite('tiktok', Provider::class); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
{ | ||
"name": "socialiteproviders/tiktok", | ||
"description": "TikTok (tiktok.com) OAuth2 Provider for Laravel Socialite", | ||
"keywords": [ | ||
"laravel", | ||
"oauth", | ||
"provider", | ||
"socialite", | ||
"tiktok" | ||
], | ||
"license": "MIT", | ||
"authors": [ | ||
{ | ||
"name": "Thomas Banks", | ||
"email": "[email protected]" | ||
} | ||
], | ||
"require": { | ||
"php": "^7.2 || ^8.0", | ||
"socialiteproviders/manager": "~4.0" | ||
}, | ||
"autoload": { | ||
"psr-4": { | ||
"SocialiteProviders\\TikTok\\": "" | ||
} | ||
}, | ||
"support": { | ||
"issues": "https://github.com/socialiteproviders/providers/issues", | ||
"source": "https://github.com/socialiteproviders/providers", | ||
"docs": "https://socialiteproviders.com/tiktok" | ||
} | ||
} |