If you are trying to integrate Symfony2 with Twitter, you're probably trying to install FOSTwitterBundle, and you may have faced two problems, one is that you can't create a user session after you login with Twitter and the other one is that there is no TwitterProvider class available as example in the instructions.
To help you with the second one here is a TwitterProvider class I made based on FacabookProvider.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
namespace Social\SiteBundle\Security\User\Provider; | |
use Symfony\Component\Security\Core\Exception\UsernameNotFoundException; | |
use Symfony\Component\Security\Core\Exception\UnsupportedUserException; | |
use Symfony\Component\Security\Core\User\UserProviderInterface; | |
use Symfony\Component\Security\Core\User\UserInterface; | |
use Symfony\Component\DependencyInjection\Container; | |
use FOS\TwitterBundle\Services\Twitter; | |
class TwitterProvider implements UserProviderInterface | |
{ | |
/** | |
* @var \Twitter | |
*/ | |
protected $twitter; | |
protected $twitter_oauth; | |
protected $userManager; | |
protected $validator; | |
protected $container; | |
public function __construct(Twitter $twitter,\TwitterOAuth $twitter_oauth, $userManager, $validator, Container $container) | |
{ | |
$this->twitter = $twitter; | |
$this->twitter_oauth = $twitter_oauth; | |
$this->userManager = $userManager; | |
$this->validator = $validator; | |
$this->container = $container; | |
} | |
public function supportsClass($class) | |
{ | |
return $this->userManager->supportsClass($class); | |
} | |
public function findUserByTwitterId($twitterID) | |
{ | |
return $this->userManager->findUserBy(array('twitterID' => $twitterID)); | |
} | |
public function loadUserByUsername($username) | |
{ | |
$user = $this->findUserByTwitterId($username); | |
$request = $this->container->get('request'); | |
$session = $request->getSession(); | |
$this->twitter_oauth->setOAuthToken( $session->get('access_token') , $session->get('access_token_secret')); | |
try { | |
$info = $this->twitter_oauth->get('account/verify_credentials'); | |
} catch (Exception $e) { | |
$info = null; | |
} | |
if (!empty($info)) { | |
if (empty($user)) { | |
$user = $this->userManager->createUser(); | |
$user->setEnabled(true); | |
$user->setPassword(''); | |
$user->setAlgorithm(''); | |
} | |
$username = $info->screen_name; | |
$user->setTwitterID($info->id); | |
$user->setTwitterUsername($username); | |
$user->setEmail(''); | |
$user->setFirstname($info->name); | |
$this->userManager->updateUser($user); | |
} | |
if (empty($user)) { | |
throw new UsernameNotFoundException('The user is not authenticated on twitter'); | |
} | |
return $user; | |
} | |
public function refreshUser(UserInterface $user) | |
{ | |
if (!$this->supportsClass(get_class($user)) || !$user->getTwitterID()) { | |
throw new UnsupportedUserException(sprintf('Instances of "%s" are not supported.', get_class($user))); | |
} | |
return $this->loadUserByUsername($user->getTwitterID()); | |
} | |
} |
To solve the authentication problem on Symfony what I found is that Twitter anywhere only works at client level, so I decided to create a custom login button and built the authorization URL.
Controller action:
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* @Route("/connectTwitter", name="connect_twitter") | |
* | |
*/ | |
public function connectTwitterAction() | |
{ | |
$request = $this->get('request'); | |
$twitter = $this->get('fos_twitter.service'); | |
$authURL = $twitter->getLoginUrl($request); | |
$response = new RedirectResponse($authURL); | |
return $response; | |
} |
Twig template:
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{% if is_granted('IS_AUTHENTICATED_FULLY') %} | |
<a href="{{ path('_security_logout')}}"><img src="/images/twitterLogOutButton.png"></a> | |
{% else %} | |
<a href="{{ path ('connect_twitter')}}"> <img src="/images/twitterLoginButton.png"></a> | |
{% endif %} |
Then after the user authenticates with Twitter, the custom TwitterProvider can get the authentication tokens from the session and authenticate the user in Symfony.