Skip to content

Commit 2bb32b3

Browse files
feat: Add TikTokShop Provider (#1358)
1 parent 9e059c6 commit 2bb32b3

5 files changed

Lines changed: 224 additions & 0 deletions

File tree

monorepo-builder.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -208,6 +208,7 @@ parameters:
208208
src/ThirtySevenSignals: 'git@github.com:SocialiteProviders/37Signals.git'
209209
src/Threads: 'git@github.com:SocialiteProviders/Threads.git'
210210
src/TikTok: 'git@github.com:SocialiteProviders/TikTok.git'
211+
src/TikTokShop: 'git@github.com:SocialiteProviders/TikTokShop.git'
211212
src/Todoist: 'git@github.com:SocialiteProviders/Todoist.git'
212213
src/Toyhouse: 'git@github.com:SocialiteProviders/Toyhouse.git'
213214
src/Trakt: 'git@github.com:SocialiteProviders/Trakt.git'

src/TikTokShop/Provider.php

Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
<?php
2+
3+
namespace App\Providers\Socialite\TikTokShop;
4+
5+
use SocialiteProviders\Manager\OAuth2\AbstractProvider;
6+
use SocialiteProviders\Manager\OAuth2\User;
7+
8+
/**
9+
* @see https://partner.tiktokshop.com/docv2/page/678e3a3292b0f40314a92d75
10+
*/
11+
class Provider extends AbstractProvider implements ProviderInterface
12+
{
13+
public const IDENTIFIER = 'TIKTOKSHOP';
14+
protected $scopes = [];
15+
16+
protected function getAuthUrl($state)
17+
{
18+
return $this->buildAuthUrlFromBase(
19+
'https://auth.tiktok-shops.com/api/v2/oauth/authorize',
20+
$state
21+
);
22+
}
23+
24+
/**
25+
* {@inheritDoc}
26+
*/
27+
protected function getTokenUrl()
28+
{
29+
return 'https://auth.tiktok-shops.com/api/v2/token/get';
30+
}
31+
32+
/**
33+
* {@inheritDoc}
34+
*/
35+
public function user()
36+
{
37+
$response = $this->getHttpClient()->get($this->getTokenUrl(), [
38+
'query' => $this->getTokenFields($this->request->get('code')),
39+
]);
40+
41+
$json = json_decode((string)$response->getBody(), true);
42+
$tokenData = $json['data'] ?? $json;
43+
44+
$user = $this->mapUserToObject($tokenData);
45+
46+
$user->token = $tokenData['access_token'] ?? null;
47+
$user->refreshToken = $tokenData['refresh_token'] ?? null;
48+
$user->expiresIn = $tokenData['access_token_expire_in'] ?? null;
49+
50+
return $user;
51+
}
52+
53+
/**
54+
* {@inheritDoc}
55+
*/
56+
protected function getTokenFields($code)
57+
{
58+
return [
59+
'app_key' => $this->clientId,
60+
'app_secret' => $this->clientSecret,
61+
'auth_code' => $code,
62+
'grant_type' => 'authorized_code',
63+
];
64+
}
65+
66+
/**
67+
* {@inheritdoc}
68+
*/
69+
protected function mapUserToObject(array $user)
70+
{
71+
return (new User)->setRaw($user)->map([
72+
'id' => $user['open_id'] ?? null,
73+
'nickname' => $user['seller_name'] ?? null,
74+
'name' => $user['seller_name'] ?? null,
75+
'email' => null,
76+
'avatar' => null,
77+
]);
78+
}
79+
}

src/TikTokShop/README.md

Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
1+
# TikTok Shop
2+
3+
```bash
4+
composer require socialiteproviders/manager
5+
```
6+
7+
## Installation & Basic Usage
8+
9+
Please see the [Base Installation Guide](https://socialiteproviders.com/usage/), then follow the provider specific
10+
instructions below.
11+
12+
## Configuration
13+
14+
First, add your TikTok Shop app credentials to `config/services.php`:
15+
16+
```php
17+
'tiktokshop' => [
18+
'client_id' => env('TIKTOKSHOP_APP_KEY'), // Your Partner Center App Key
19+
'client_secret' => env('TIKTOKSHOP_APP_SECRET'), // Your Partner Center App Secret
20+
'redirect' => env('TIKTOKSHOP_REDIRECT_URI'), // Your callback URI
21+
],
22+
```
23+
24+
Make sure you have these in your `.env`:
25+
26+
```bash
27+
TIKTOKSHOP_APP_KEY=your_partner_center_app_key
28+
TIKTOKSHOP_APP_SECRET=your_partner_center_app_secret
29+
TIKTOKSHOP_REDIRECT_URI=https://yourapp.com/auth/tiktokshop/callback
30+
```
31+
32+
### Add provider event listener
33+
34+
#### Laravel 11+
35+
36+
In Laravel 11, register the listener directly in your `AppServiceProvider@boot`:
37+
38+
```php
39+
use SocialiteProviders\Manager\SocialiteWasCalled;
40+
use App\Providers\Socialite\TikTokShop\TikTokShopExtendSocialite;
41+
use Illuminate\Support\Facades\Event;
42+
43+
public function boot()
44+
{
45+
Event::listen(function (SocialiteProviders\Manager\SocialiteWasCalled $event) {
46+
$event->extendSocialite('tiktokshop', \App\Providers\Socialite\TikTokShop\Provider::class);
47+
});
48+
}
49+
```
50+
51+
<details>
52+
<summary>Laravel 10 or below</summary>
53+
54+
Configure the package’s listener in `app/Providers/EventServiceProvider.php`:
55+
56+
```php
57+
protected $listen = [
58+
\SocialiteProviders\Manager\SocialiteWasCalled::class => [
59+
\App\Providers\Socialite\TikTokShop\TikTokShopExtendSocialite::class.'@handle',
60+
],
61+
];
62+
```
63+
64+
</details>
65+
66+
## Usage
67+
68+
You can now redirect to TikTok Shop for authorization:
69+
70+
```php
71+
use Laravel\Socialite\Facades\Socialite;
72+
73+
return Socialite::driver('tiktokshop')->redirect();
74+
```
75+
76+
And handle the callback:
77+
78+
```php
79+
$shopUser = Socialite::driver('tiktokshop')->user();
80+
81+
// Access mapped fields:
82+
$shopUser->getId();
83+
$shopUser->getName();
84+
$shopUser->token;
85+
$shopUser->refreshToken;
86+
$shopUser->expiresIn;
87+
```
88+
89+
### Returned User fields
90+
91+
* `id` – the TikTok Shop `open_id`
92+
* `nickname` / `name` – the `seller_name`
93+
* `token` – the `access_token`
94+
* `refreshToken` – the `refresh_token`
95+
* `expiresIn` – seconds until token expiry
96+
97+
## References
98+
99+
* [TikTok Shop Partner Center Authorization Overview (202407)](https://partner.tiktokshop.com/docv2/page/678e3a3292b0f40314a92d75)
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
<?php
2+
3+
namespace App\Providers\Socialite\TikTokShop;
4+
5+
use SocialiteProviders\Manager\SocialiteWasCalled;
6+
7+
class TikTokShopExtendSocialite
8+
{
9+
public function handle(SocialiteWasCalled $socialiteWasCalled)
10+
{
11+
$socialiteWasCalled->extendSocialite('tiktokshop', Provider::class);
12+
}
13+
}

src/TikTokShop/composer.json

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
{
2+
"name": "socialiteproviders/tiktokshop",
3+
"description": "TikTok Shop OAuth2 Provider for Laravel Socialite",
4+
"license": "MIT",
5+
"keywords": [
6+
"laravel",
7+
"oauth",
8+
"provider",
9+
"socialite",
10+
"tiktokshop"
11+
],
12+
"authors": [
13+
{
14+
"name": "Gabriel R. Barbosa",
15+
"email": "gabrieltads2016@gmail.com"
16+
}
17+
],
18+
"support": {
19+
"issues": "https://github.com/socialiteproviders/providers/issues",
20+
"source": "https://github.com/socialiteproviders/providers",
21+
"docs": "https://socialiteproviders.com/tiktokshop"
22+
},
23+
"require": {
24+
"php": "^8.0",
25+
"socialiteproviders/manager": "^4.4"
26+
},
27+
"autoload": {
28+
"psr-4": {
29+
"SocialiteProviders\\TikTokShop\\": ""
30+
}
31+
}
32+
}

0 commit comments

Comments
 (0)