Abdul Hadi
Abdul Hadi

Reputation: 21

How to use Oauth2 Provider instead of Oauth1 in Laravel Socialite?

I'm currently developing social listening API with twitter, the flow is logged in user (using basic API Token to send request to the API) are redirected to twitter app and log in to twitter account through redirect link provided by Laravel Socialite. After successfully login the account info will be saved in MySQL along with user who registered the account.

I'm using Laravel 7 with Socialite v5.4.0

i've beend trying to provide query parameters in the callback url using :

Socialite::driver('twitter')
        ->redirectUrl('https://some-site-url.com/twitter/login/callback?api_token={sample-token}')
        ->redirect()->getTargetUrl());

but Socialite return error Call to undefined method Laravel\\Socialite\\One\\TwitterProvider::redirectUrl()",

thats when i realized that the currently used twitter provider is using Oauth1 Laravel\Socialite\One\TwitterProvider. when i look at the vendor in Laravel\Socialite\\SocialiteManager.php its creating instance of this :

/**
 * Create an instance of the specified driver.
 *
 * @return \Laravel\Socialite\One\AbstractProvider
 */
protected function createTwitterDriver()
{
    $config = $this->config->get('services.twitter');

    return new TwitterProvider(
        $this->container->make('request'), new TwitterServer($this->formatConfig($config))
    );
}

but in the same Laravel\Socialite\\SocialiteManager.php its also have method to create Oauth2 instance like below :

/**
 * Create an instance of the specified driver.
 *
 * @return \Laravel\Socialite\Two\AbstractProvider
 */
protected function createTwitterOAuth2Driver()
{
    $config = $this->config->get('services.twitter');

    return $this->buildProvider(
        TwitterOAuth2Provider::class, $config
    );
}

Questions now my question is how to force the Socialite::driver('twitter') method to use Oauth2 instead of Oauth1 which basically is available in the Socialite itself ? i have tried to override the method but found no link to which instance is calling SocialiteManager.php, so currently i have only tried to modify the vendor function to return Oauth2 AbstractProvider (which i know its really ugly approach but i feel really curious), its like this :

// /**
//  * Create an instance of the specified driver.
//  *
//  * @return \Laravel\Socialite\One\AbstractProvider
//  */
// protected function createTwitterDriver()
// {
//     $config = $this->config->get('services.twitter');

//     return new TwitterProvider(
//         $this->container->make('request'), new TwitterServer($this->formatConfig($config))
//     );
// }

/**
 * Create an instance of the specified driver.
 *
 * @return \Laravel\Socialite\Two\AbstractProvider
 */
protected function createTwitterDriver()
{
    $config = $this->config->get('services.twitter');

    return $this->buildProvider(
        TwitterOAuth2Provider::class, $config
    );
}

.the method works and its return the redirect url successfully but failed to logged in to twitter page for unknown reason, which make me think is there a way to cleanly switch Socialite provider version between Oauth1 and Oauth2. Or is there any alternative to provide a callback with user identifier instead ?

the url return after i ditch the method in vendor SocialiteManager.php But failed to log in to twitter app for unknown reason

thanks in advance, it's my first question and i've been looking for the answer since yesterday but found no specific way to switch Socialite provider between version 1 and version 2

Upvotes: 2

Views: 1730

Answers (1)

F KIng
F KIng

Reputation: 498

Laravel\Socialite\SocialiteManager.php looks for an oauth key in your twitter credentials to determine whether to use OAuth1 or Oauth2

/**
 * Create an instance of the specified driver.
 *
 * @return \Laravel\Socialite\One\AbstractProvider
 */
protected function createTwitterDriver()
{
    $config = $this->config->get('services.twitter');

    if (($config['oauth'] ?? null) === 2) {
        return $this->createTwitterOAuth2Driver();
    }

    return new TwitterProvider(
        $this->container->make('request'), new TwitterServer($this->formatConfig($config))
    );
}

Simply add an oauth key with a value of 2 to your Twitter credentials in the config/services.php file.

'twitter' => [
    'client_id' => env('TWITTER_CLIENT_ID'),
    'client_secret' => env('TWITTER_CLIENT_SECRET'),
    'redirect' => env('TWITTER_REDIRECT_URL'),
    'oauth' => 2
],

If you are switching from oauth1 do not forget to update your credentials to use OAuth 2.0 Client ID and Client Secret which you can get from your Twitter developer account.

Upvotes: 3

Related Questions