Skip to content
Merged
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
1 change: 1 addition & 0 deletions monorepo-builder.yml
Original file line number Diff line number Diff line change
Expand Up @@ -205,6 +205,7 @@ parameters:
src/ThirtySevenSignals: 'git@github.com:SocialiteProviders/37Signals.git'
src/Threads: 'git@github.com:SocialiteProviders/Threads.git'
src/TikTok: 'git@github.com:SocialiteProviders/TikTok.git'
src/TikTokShop: 'git@github.com:SocialiteProviders/TikTokShop.git'
src/Todoist: 'git@github.com:SocialiteProviders/Todoist.git'
src/Toyhouse: 'git@github.com:SocialiteProviders/Toyhouse.git'
src/Trakt: 'git@github.com:SocialiteProviders/Trakt.git'
Expand Down
79 changes: 79 additions & 0 deletions src/TikTokShop/Provider.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
<?php

namespace App\Providers\Socialite\TikTokShop;

use SocialiteProviders\Manager\OAuth2\AbstractProvider;
use SocialiteProviders\Manager\OAuth2\User;

/**
* @see https://partner.tiktokshop.com/docv2/page/678e3a3292b0f40314a92d75
*/
class Provider extends AbstractProvider implements ProviderInterface
{
public const IDENTIFIER = 'TIKTOKSHOP';
protected $scopes = [];

protected function getAuthUrl($state)
{
return $this->buildAuthUrlFromBase(
'https://auth.tiktok-shops.com/api/v2/oauth/authorize',
$state
);
}

/**
* {@inheritDoc}
*/
protected function getTokenUrl()
{
return 'https://auth.tiktok-shops.com/api/v2/token/get';
}

/**
* {@inheritDoc}
*/
public function user()
{
$response = $this->getHttpClient()->get($this->getTokenUrl(), [
'query' => $this->getTokenFields($this->request->get('code')),
]);

$json = json_decode((string)$response->getBody(), true);
$tokenData = $json['data'] ?? $json;

$user = $this->mapUserToObject($tokenData);

$user->token = $tokenData['access_token'] ?? null;
$user->refreshToken = $tokenData['refresh_token'] ?? null;
$user->expiresIn = $tokenData['access_token_expire_in'] ?? null;

return $user;
}

/**
* {@inheritDoc}
*/
protected function getTokenFields($code)
{
return [
'app_key' => $this->clientId,
'app_secret' => $this->clientSecret,
'auth_code' => $code,
'grant_type' => 'authorized_code',
];
}

/**
* {@inheritdoc}
*/
protected function mapUserToObject(array $user)
{
return (new User)->setRaw($user)->map([
'id' => $user['open_id'] ?? null,
'nickname' => $user['seller_name'] ?? null,
'name' => $user['seller_name'] ?? null,
'email' => null,
'avatar' => null,
]);
}
}
99 changes: 99 additions & 0 deletions src/TikTokShop/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
# TikTok Shop

```bash
composer require socialiteproviders/manager
```

## Installation & Basic Usage

Please see the [Base Installation Guide](https://socialiteproviders.com/usage/), then follow the provider specific
instructions below.

## Configuration

First, add your TikTok Shop app credentials to `config/services.php`:

```php
'tiktokshop' => [
'client_id' => env('TIKTOKSHOP_APP_KEY'), // Your Partner Center App Key
'client_secret' => env('TIKTOKSHOP_APP_SECRET'), // Your Partner Center App Secret
'redirect' => env('TIKTOKSHOP_REDIRECT_URI'), // Your callback URI
],
```

Make sure you have these in your `.env`:

```bash
TIKTOKSHOP_APP_KEY=your_partner_center_app_key
TIKTOKSHOP_APP_SECRET=your_partner_center_app_secret
TIKTOKSHOP_REDIRECT_URI=https://yourapp.com/auth/tiktokshop/callback
```

### Add provider event listener

#### Laravel 11+

In Laravel 11, register the listener directly in your `AppServiceProvider@boot`:

```php
use SocialiteProviders\Manager\SocialiteWasCalled;
use App\Providers\Socialite\TikTokShop\TikTokShopExtendSocialite;
use Illuminate\Support\Facades\Event;

public function boot()
{
Event::listen(function (SocialiteProviders\Manager\SocialiteWasCalled $event) {
$event->extendSocialite('tiktokshop', \App\Providers\Socialite\TikTokShop\Provider::class);
});
}
```

<details>
<summary>Laravel 10 or below</summary>

Configure the package’s listener in `app/Providers/EventServiceProvider.php`:

```php
protected $listen = [
\SocialiteProviders\Manager\SocialiteWasCalled::class => [
\App\Providers\Socialite\TikTokShop\TikTokShopExtendSocialite::class.'@handle',
],
];
```

</details>

## Usage

You can now redirect to TikTok Shop for authorization:

```php
use Laravel\Socialite\Facades\Socialite;

return Socialite::driver('tiktokshop')->redirect();
```

And handle the callback:

```php
$shopUser = Socialite::driver('tiktokshop')->user();

// Access mapped fields:
$shopUser->getId();
$shopUser->getName();
$shopUser->token;
$shopUser->refreshToken;
$shopUser->expiresIn;
```

### Returned User fields

* `id` – the TikTok Shop `open_id`
* `nickname` / `name` – the `seller_name`
* `token` – the `access_token`
* `refreshToken` – the `refresh_token`
* `expiresIn` – seconds until token expiry

## References

* [TikTok Shop Partner Center Authorization Overview (202407)](https://partner.tiktokshop.com/docv2/page/678e3a3292b0f40314a92d75)
13 changes: 13 additions & 0 deletions src/TikTokShop/TikTokShopExtendSocialite.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?php

namespace App\Providers\Socialite\TikTokShop;

use SocialiteProviders\Manager\SocialiteWasCalled;

class TikTokShopExtendSocialite
{
public function handle(SocialiteWasCalled $socialiteWasCalled)
{
$socialiteWasCalled->extendSocialite('tiktokshop', Provider::class);
}
}
32 changes: 32 additions & 0 deletions src/TikTokShop/composer.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
{
"name": "socialiteproviders/tiktokshop",
"description": "TikTok Shop OAuth2 Provider for Laravel Socialite",
"license": "MIT",
"keywords": [
"laravel",
"oauth",
"provider",
"socialite",
"tiktokshop"
],
"authors": [
{
"name": "Gabriel R. Barbosa",
"email": "gabrieltads2016@gmail.com"
}
],
"support": {
"issues": "https://github.com/socialiteproviders/providers/issues",
"source": "https://github.com/socialiteproviders/providers",
"docs": "https://socialiteproviders.com/tiktokshop"
},
"require": {
"php": "^8.0",
"socialiteproviders/manager": "^4.4"
},
"autoload": {
"psr-4": {
"SocialiteProviders\\TikTokShop\\": ""
}
}
}
Loading