From fbebce5c5ad6c0831af5866d8308973cabfeeb11 Mon Sep 17 00:00:00 2001 From: tomocrafter Date: Sat, 24 Aug 2024 17:10:32 +0900 Subject: [PATCH] Add X Provider --- src/SocialiteManager.php | 15 +++++++++++++++ src/Two/XProvider.php | 38 ++++++++++++++++++++++++++++++++++++++ 2 files changed, 53 insertions(+) create mode 100644 src/Two/XProvider.php diff --git a/src/SocialiteManager.php b/src/SocialiteManager.php index 5ac38b56..31dcd71d 100644 --- a/src/SocialiteManager.php +++ b/src/SocialiteManager.php @@ -17,6 +17,7 @@ use Laravel\Socialite\Two\SlackOpenIdProvider; use Laravel\Socialite\Two\SlackProvider; use Laravel\Socialite\Two\TwitterProvider as TwitterOAuth2Provider; +use Laravel\Socialite\Two\XProvider; use League\OAuth1\Client\Server\Twitter as TwitterServer; class SocialiteManager extends Manager implements Contracts\Factory @@ -171,6 +172,20 @@ protected function createTwitterOAuth2Driver() ); } + /** + * Create an instance of the specified driver. + * + * @return \Laravel\Socialite\Two\AbstractProvider + */ + protected function createXDriver() + { + $config = $this->config->get('services.x') ?? $this->config->get('services.x-oauth-2'); + + return $this->buildProvider( + XProvider::class, $config + ); + } + /** * Create an instance of the specified driver. * diff --git a/src/Two/XProvider.php b/src/Two/XProvider.php new file mode 100644 index 00000000..8a1d5da7 --- /dev/null +++ b/src/Two/XProvider.php @@ -0,0 +1,38 @@ +buildAuthUrlFromBase('https://x.com/i/oauth2/authorize', $state); + } + + /** + * {@inheritdoc} + */ + protected function getTokenUrl() + { + return 'https://api.x.com/2/oauth2/token'; + } + + /** + * {@inheritdoc} + */ + protected function getUserByToken($token) + { + $response = $this->getHttpClient()->get('https://api.x.com/2/users/me', [ + RequestOptions::HEADERS => ['Authorization' => 'Bearer '.$token], + RequestOptions::QUERY => ['user.fields' => 'profile_image_url'], + ]); + + return Arr::get(json_decode($response->getBody(), true), 'data'); + } +}