├── Provider.php ├── README.md ├── WeiboExtendSocialite.php └── composer.json /Provider.php: -------------------------------------------------------------------------------- 1 | buildAuthUrlFromBase('https://api.weibo.com/oauth2/authorize', $state); 16 | } 17 | 18 | protected function getTokenUrl(): string 19 | { 20 | return 'https://api.weibo.com/oauth2/access_token'; 21 | } 22 | 23 | /** 24 | * {@inheritdoc}. 25 | */ 26 | protected function getUserByToken($token) 27 | { 28 | $response = $this->getHttpClient()->get('https://api.weibo.com/2/users/show.json', [ 29 | RequestOptions::QUERY => [ 30 | 'access_token' => $token, 31 | 'uid' => $this->getUid($token), 32 | ], 33 | ]); 34 | 35 | return json_decode($this->removeCallback((string) $response->getBody()), true); 36 | } 37 | 38 | /** 39 | * {@inheritdoc}. 40 | */ 41 | protected function mapUserToObject(array $user) 42 | { 43 | return (new User)->setRaw($user)->map([ 44 | 'id' => $user['idstr'], 'nickname' => $user['name'], 45 | 'avatar' => $user['avatar_large'], 'name' => null, 'email' => null, 46 | ]); 47 | } 48 | 49 | public function getAccessToken($code) 50 | { 51 | $response = $this->getHttpClient()->post($this->getTokenUrl(), [ 52 | RequestOptions::QUERY => $this->getTokenFields($code), 53 | ]); 54 | 55 | $this->credentialsResponseBody = json_decode((string) $response->getBody(), true); 56 | 57 | return $this->parseAccessToken($response->getBody()); 58 | } 59 | 60 | /** 61 | * @param mixed $response 62 | * @return string 63 | */ 64 | protected function removeCallback($response) 65 | { 66 | if (str_contains($response, 'callback')) { 67 | $lpos = strpos($response, '('); 68 | $rpos = strrpos($response, ')'); 69 | $response = substr($response, $lpos + 1, $rpos - $lpos - 1); 70 | } 71 | 72 | return $response; 73 | } 74 | 75 | protected function getUid(string $token): string 76 | { 77 | $response = $this->getHttpClient()->get('https://api.weibo.com/2/account/get_uid.json', [ 78 | RequestOptions::QUERY => ['access_token' => $token], 79 | ]); 80 | 81 | return json_decode((string) $response->getBody(), true)['uid']; 82 | } 83 | } 84 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Weibo 2 | 3 | ```bash 4 | composer require socialiteproviders/weibo 5 | ``` 6 | 7 | ## Installation & Basic Usage 8 | 9 | Please see the [Base Installation Guide](https://socialiteproviders.com/usage/), then follow the provider specific instructions below. 10 | 11 | ### Add configuration to `config/services.php` 12 | 13 | ```php 14 | 'weibo' => [ 15 | 'client_id' => env('WEIBO_CLIENT_ID'), 16 | 'client_secret' => env('WEIBO_CLIENT_SECRET'), 17 | 'redirect' => env('WEIBO_REDIRECT_URI') 18 | ], 19 | ``` 20 | 21 | ### Add provider event listener 22 | 23 | #### Laravel 11+ 24 | 25 | In Laravel 11, the default `EventServiceProvider` provider was removed. Instead, add the listener using the `listen` method on the `Event` facade, in your `AppServiceProvider` `boot` method. 26 | 27 | * Note: You do not need to add anything for the built-in socialite providers unless you override them with your own providers. 28 | 29 | ```php 30 | Event::listen(function (\SocialiteProviders\Manager\SocialiteWasCalled $event) { 31 | $event->extendSocialite('weibo', \SocialiteProviders\Weibo\Provider::class); 32 | }); 33 | ``` 34 |
35 | 36 | Laravel 10 or below 37 | 38 | Configure the package's listener to listen for `SocialiteWasCalled` events. 39 | 40 | Add the event to your `listen[]` array in `app/Providers/EventServiceProvider`. See the [Base Installation Guide](https://socialiteproviders.com/usage/) for detailed instructions. 41 | 42 | ```php 43 | protected $listen = [ 44 | \SocialiteProviders\Manager\SocialiteWasCalled::class => [ 45 | // ... other providers 46 | \SocialiteProviders\Weibo\WeiboExtendSocialite::class.'@handle', 47 | ], 48 | ]; 49 | ``` 50 |
51 | 52 | ### Usage 53 | 54 | You should now be able to use the provider like you would regularly use Socialite (assuming you have the facade installed): 55 | 56 | ```php 57 | return Socialite::driver('weibo')->redirect(); 58 | ``` 59 | 60 | ### Returned User fields 61 | 62 | - ``id`` 63 | - ``nickname`` 64 | - ``avatar`` 65 | -------------------------------------------------------------------------------- /WeiboExtendSocialite.php: -------------------------------------------------------------------------------- 1 | extendSocialite('weibo', Provider::class); 12 | } 13 | } 14 | -------------------------------------------------------------------------------- /composer.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "socialiteproviders/weibo", 3 | "description": "weibo.com OAuth2 Provider for Laravel Socialite", 4 | "license": "MIT", 5 | "keywords": [ 6 | "laravel", 7 | "oauth", 8 | "provider", 9 | "socialite", 10 | "weibo" 11 | ], 12 | "authors": [ 13 | { 14 | "name": "xyxu", 15 | "email": "techxu@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/weibo" 22 | }, 23 | "require": { 24 | "php": "^8.0", 25 | "ext-json": "*", 26 | "socialiteproviders/manager": "^4.4" 27 | }, 28 | "autoload": { 29 | "psr-4": { 30 | "SocialiteProviders\\Weibo\\": "" 31 | } 32 | } 33 | } 34 | --------------------------------------------------------------------------------