Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

Marlon's avatar

Socialite and Twitter

Hello again!

Anyone know if have any problem with twitter and socialite?

I have in my application login with facebook, google and twitter. Facebook and google works fine with the same logic, but today twitter got loop and never back to application.

I have a simple code

    public function getLoginTwitter(AuthenticateTwitter $authenticatetwitter, Request $request)
    {
        return $authenticatetwitter->execute($request->has('code'));
    }

and

<?php
namespace App\Http;

use Illuminate\Contracts\Auth\Guard;
use Laravel\Socialite\Contracts\Factory as Socialite;
use App\Repositories\UserRepository;

class   AuthenticateTwitter {

    /**
     * @var UserRepository
     */
    private $user;
    /**
     * @var Socialite
     */
    private $socialite;
    /**
     * @var Guard
     */
    private $auth;

    public function __construct(UserRepository $user, Socialite $socialite, Guard $auth)
    {
        $this->user = $user;
        $this->socialite = $socialite;
        $this->auth = $auth;
    }

    /**
     * @param $hasCode
     * @return \Symfony\Component\HttpFoundation\RedirectResponse
     */
    public function execute($hasCode)
    {
        if(!$hasCode)   //This $hasCode always false
            return $this->getAuthorizationFirst();

        $user = $this->user->findByUserNameOrCreate($this->getTwitterUser());
        return redirect('/auth/register')->with(['name' => $user->name, 'picture' => $user->picture, 'email' => $user->email]);
    }

    private function getAuthorizationFirst()
    {
        return $this->socialite->driver('twitter')->redirect();
    }

    private function getTwitterUser()
    {
        return $this->socialite->driver('twitter')->user();
    }
}

But when I debug $hasCode always got false. I have inspected the $_REQUEST and twitter give oauth_token and oauth_verifier never return code.

Can anyone help me?

Thanks in advance

0 likes
7 replies
Marlon's avatar

Well I made an experience, changed

return $authenticatetwitter->execute($request->has('code'));

by

return $authenticatetwitter->execute($request->has('oauth_token'));

Worked but I do not get the email of the twitter user.

Anyone have any clue?

Thanks in advance

3 likes
rieves's avatar

Twitter does not give the users email address when using their OAuth.

Marlon's avatar

Ah thanks,

but my approach are correct when I get oauth_token instead code

Stolz's avatar

I'm having the same problem. Google, Facebook and GitHub are working. They are all Oauth 2.0 providers. Twitter is the only Oauth 1.0 provider and it does not provide the code in the callback URL. It would be great if Socialite included a function to check if current request is a callback for us not to implement different callback handlers for each provider.

My solution was to intercep InvalidArgumentException exception:

/**
 * Attempt to log in an user using a social authentication provider.
 *
 * @param  string
 * @return Response
 */
public function loginWithProvider($provider)
{
    // Create an Oauth service for this provider
    $oauthService = Socialite::with($provider);

    // If this is a callback from the provider the user information
    // should be available, otherwise an exception will be thrown
    try
    {
        if($user = $oauthService->user())
            return $this->loginSocialUser($user); //This is where you actually login/register the user with Laravel
    }
    catch(\InvalidArgumentException $e)
    {
        // This is not a callback request.
        // Redirect user to authorize our App.

        return $oauthService->redirect();
    }
}
nolros's avatar

@stolz @Marlon have you guys managed to get twitter to work. Pulling my hair out. It loses the temp credentials and have no idea why.

Please or to participate in this conversation.