Skip to content

Commit

Permalink
Add X Provider (#712)
Browse files Browse the repository at this point in the history
  • Loading branch information
tomocrafter authored Aug 24, 2024
1 parent 1e177bf commit 6e66b22
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 0 deletions.
15 changes: 15 additions & 0 deletions src/SocialiteManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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.
*
Expand Down
38 changes: 38 additions & 0 deletions src/Two/XProvider.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
<?php

namespace Laravel\Socialite\Two;

use GuzzleHttp\RequestOptions;
use Illuminate\Support\Arr;

class XProvider extends TwitterProvider
{
/**
* {@inheritdoc}
*/
public function getAuthUrl($state)
{
return $this->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');
}
}

0 comments on commit 6e66b22

Please sign in to comment.