2014-11-04 175 views
1

我想實現Facebook登錄到我的網站。我也使用FOSUserBundle。使用FOSUB登錄工作正常,但當我嘗試登錄facebook時,只要我到了url/login/check-facebook,我就收到錯誤「發生了身份驗證異常。」我使用這個tutorial做了Facebook登錄的實現。我也得到了應用程序ID和App祕密從Facebook(不知道這兩個應該設置爲client_idconfig.ymlclient_secret,也不能肯定,如果我不需要設置任何東西在Facebook)的Symfony2 FOSUserbundle2 HWIOAuth包Facebook登錄錯誤

FOSUBUserProvider。 PHP

<?php 
namespace Atotrukis\MainBundle\Service; 

use HWI\Bundle\OAuthBundle\OAuth\Response\UserResponseInterface; 
use HWI\Bundle\OAuthBundle\Security\Core\User\FOSUBUserProvider as BaseClass; 
use Symfony\Component\Security\Core\User\UserInterface; 

class FOSUBUserProvider extends BaseClass 
{ 

/** 
* {@inheritDoc} 
*/ 
public function connect(UserInterface $user, UserResponseInterface $response) 
{ 
    $property = $this->getProperty($response); 
    $username = $response->getUsername(); 

    //on connect - get the access token and the user ID 
    $service = $response->getResourceOwner()->getName(); 

    $setter = 'set'.ucfirst($service); 
    $setter_id = $setter.'Id'; 
    $setter_token = $setter.'AccessToken'; 

    //we "disconnect" previously connected users 
    if (null !== $previousUser = $this->userManager->findUserBy(array($property => $username))) { 
     $previousUser->$setter_id(null); 
     $previousUser->$setter_token(null); 
     $this->userManager->updateUser($previousUser); 
    } 

    //we connect current user 
    $user->$setter_id($username); 
    $user->$setter_token($response->getAccessToken()); 

    $this->userManager->updateUser($user); 
} 

/** 
* {@inheritdoc} 
*/ 
public function loadUserByOAuthUserResponse(UserResponseInterface $response) 
{ 
    $username = $response->getUsername(); 
    $user = $this->userManager->findUserBy(array($this->getProperty($response) => $username)); 
    //when the user is registrating 
    if (null === $user) { 
     $service = $response->getResourceOwner()->getName(); 
     $setter = 'set'.ucfirst($service); 
     $setter_id = $setter.'Id'; 
     $setter_token = $setter.'AccessToken'; 
     // create new user here 
     $user = $this->userManager->createUser(); 
     $user->$setter_id($username); 
     $user->$setter_token($response->getAccessToken()); 
     //I have set all requested data with the user's username 
     //modify here with relevant data 
     $user->setUsername($username); 
     $user->setEmail($username); 
     $user->setPassword($username); 
     $user->setEnabled(true); 
     $this->userManager->updateUser($user); 
     return $user; 
    } 

    //if user exists - go with the HWIOAuth way 
    $user = parent::loadUserByOAuthUserResponse($response); 

    $serviceName = $response->getResourceOwner()->getName(); 
    $setter = 'set' . ucfirst($serviceName) . 'AccessToken'; 

     //update access token 
     $user->$setter($response->getAccessToken()); 

     return $user; 
    } 

} 

user.php的

<?php 
namespace Atotrukis\MainBundle\Entity; 

use Doctrine\ORM\Mapping as ORM; 
use FOS\UserBundle\Model\User as BaseUser; 
use Symfony\Component\Validator\Constraints as Assert; 

/** 
* @ORM\Entity 
* @ORM\Table(name="users") 
*/ 
class User extends BaseUser 
{ 
    /** 
    * @ORM\Column(type="integer") 
    * @ORM\Id 
    * @ORM\GeneratedValue(strategy="AUTO") 
    */ 
    protected $id; 

    /** @ORM\Column(name="facebook_id", type="string", length=255, nullable=true) */ 
    protected $facebook_id; 

    /** @ORM\Column(name="facebook_access_token", type="string", length=255, nullable=true) */ 
    protected $facebook_access_token; 

    /** 
    * @ORM\Column(type="string", length=255) 
    */ 
    protected $name; 

    /** 
    * @ORM\OneToMany(targetEntity="Event", mappedBy="createdBy") 
    */ 
    protected $events; 

    /** 
    * @ORM\OneToMany(targetEntity="UserInterest", mappedBy="userId") 
    */ 
    protected $interests; 

    /** 
    * @ORM\OneToMany(targetEntity="UserAttending", mappedBy="userId") 
    */ 
    protected $attendingTo; 

    public function __construct() 
    { 
     parent::__construct(); 
    } 

    /** 
    * Get id 
    * 
* @return integer 
*/ 
public function getId() 
{ 
    return $this->id; 
} 

/** 
* Add events 
* 
* @param \Atotrukis\MainBundle\Entity\Event $events 
* @return User 
*/ 
public function addEvent(\Atotrukis\MainBundle\Entity\Event $events) 
{ 
    $this->events[] = $events; 

    return $this; 
} 

/** 
* Remove events 
* 
* @param \Atotrukis\MainBundle\Entity\Event $events 
*/ 
public function removeEvent(\Atotrukis\MainBundle\Entity\Event $events) 
{ 
    $this->events->removeElement($events); 
} 

/** 
* Get events 
* 
* @return \Doctrine\Common\Collections\Collection 
*/ 
public function getEvents() 
{ 
    return $this->events; 
} 

/** 
* Add interests 
* 
* @param \Atotrukis\MainBundle\Entity\UserInterests $interests 
* @return User 
*/ 
public function addInterest(\Atotrukis\MainBundle\Entity\UserInterests $interests) 
{ 
    $this->interests[] = $interests; 

    return $this; 
} 

/** 
* Remove interests 
* 
* @param \Atotrukis\MainBundle\Entity\UserInterests $interests 
*/ 
public function removeInterest(\Atotrukis\MainBundle\Entity\UserInterests $interests) 
{ 
    $this->interests->removeElement($interests); 
} 

/** 
* Get interests 
* 
* @return \Doctrine\Common\Collections\Collection 
*/ 
public function getInterests() 
{ 
    return $this->interests; 
} 

/** 
* Add attendingTo 
* 
* @param \Atotrukis\MainBundle\Entity\UserAttending $attendingTo 
* @return User 
*/ 
public function addAttendingTo(\Atotrukis\MainBundle\Entity\UserAttending $attendingTo) 
{ 
    $this->attendingTo[] = $attendingTo; 

    return $this; 
} 

/** 
* Remove attendingTo 
* 
* @param \Atotrukis\MainBundle\Entity\UserAttending $attendingTo 
*/ 
public function removeAttendingTo(\Atotrukis\MainBundle\Entity\UserAttending $attendingTo) 
{ 
    $this->attendingTo->removeElement($attendingTo); 
} 

/** 
* Get attendingTo 
* 
* @return \Doctrine\Common\Collections\Collection 
*/ 
public function getAttendingTo() 
{ 
    return $this->attendingTo; 
} 

public function setEmail($email) 
{ 
    $email = is_null($email) ? '' : $email; 
    parent::setEmail($email); 
    $this->setUsername($email); 
} 

/** 
* Set name 
* 
* @param string $name 
* @return User 
*/ 
public function setName($name) 
{ 
    $this->name = $name; 

    return $this; 
} 

/** 
* Get name 
* 
* @return string 
*/ 
public function getName() 
{ 
    return $this->name; 
} 

/** 
* Set facebook_id 
* 
* @param string $facebookId 
* @return User 
*/ 
public function setFacebookId($facebookId) 
{ 
    $this->facebook_id = $facebookId; 

    return $this; 
} 

/** 
* Get facebook_id 
* 
* @return string 
*/ 
public function getFacebookId() 
{ 
    return $this->facebook_id; 
} 

/** 
* Set facebook_access_token 
* 
* @param string $facebookAccessToken 
* @return User 
*/ 
public function setFacebookAccessToken($facebookAccessToken) 
{ 
    $this->facebook_access_token = $facebookAccessToken; 

    return $this; 
} 

/** 
* Get facebook_access_token 
* 
    * @return string 
    */ 
    public function getFacebookAccessToken() 
    { 
     return $this->facebook_access_token; 
    } 
} 

的routing.yml:

atotrukis_main: 
    resource: "@AtotrukisMainBundle/Resources/config/routing.yml" 
    prefix: /

fos_user: 
    resource: "@FOSUserBundle/Resources/config/routing/all.xml" 

#HWIOAuthBundle routes 
hwi_oauth_security: 
    resource: "@HWIOAuthBundle/Resources/config/routing/login.xml" 
    prefix: /login 

hwi_oauth_connect: 
    resource: "@HWIOAuthBundle/Resources/config/routing/connect.xml" 
    prefix: /login 

hwi_oauth_redirect: 
    resource: "@HWIOAuthBundle/Resources/config/routing/redirect.xml" 
    prefix: /login 

facebook_login: 
    pattern: /login/check-facebook 

config.yml

os_user: 
    db_driver: orm # other valid values are 'mongodb', 'couchdb' and 'propel' 
    firewall_name: main 
    user_class: Atotrukis\MainBundle\Entity\User 
    registration: 
     confirmation: 
     from_email: 
      address:  [email protected] 
      sender_name: Sender name 
     enabled: false 
     form: 
     type: atotrukis_user_registration 

hwi_oauth: 
    #this is my custom user provider, created from FOSUBUserProvider - will manage the 
    #automatic user registration on your site, with data from the provider (facebook. google, etc.) 
    #and also, the connecting part (get the token and the user_id) 
    connect: 
     account_connector: my_user_provider 
    # name of the firewall in which this bundle is active, this setting MUST be set 
    firewall_name: main 
    fosub: 
     username_iterations: 30 
     properties: 
      # these properties will be used/redefined later in the custom FOSUBUserProvider service. 
      facebook: facebook_id 
    resource_owners: 
     facebook: 
      type:    facebook 
      client_id:   "app_id" 
      client_secret:  "app_secret" 
      scope:    "" 

security.yml 
security: 
    encoders: 
     FOS\UserBundle\Model\UserInterface: sha512 

    role_hierarchy: 
     ROLE_ADMIN:  ROLE_USER 
     ROLE_SUPER_ADMIN: ROLE_USER 

    providers: 
     fos_userbundle: 
      id: fos_user.user_provider.username_email 

    firewalls: 
     dev: 
      pattern: ^/(_(profiler|wdt)|css|images|js)/ 
      security: false 

     main: 
      pattern: ^/ 
      form_login: 
       provider: fos_userbundle 
       csrf_provider: form.csrf_provider 
       login_path: /login 
       check_path: /login_check 
      oauth: 
       resource_owners: 
        facebook:   "/login/check-facebook" 
       login_path:  /login 
       failure_path:  /login 

       oauth_user_provider: 
        #this is my custom user provider, created from FOSUBUserProvider - will manage the 
        #automatic user registration on your site, with data from the provider (facebook. google, etc.) 
        service: my_user_provider 
      logout:  true 
      anonymous: true 

     login: 
      pattern: ^/login$ 
      security: false 

      remember_me: 
       key: "%secret%" 
       lifetime: 31536000 # 365 days in seconds 
       path:/
       domain: ~ # Defaults to the current domain from $_SERVER 

    access_control: 
     - { path: ^/mano-renginiai, role: ROLE_USER } 
     - { path: ^/login$, role: IS_AUTHENTICATED_ANONYMOUSLY } 
     - { path: ^/register, role: IS_AUTHENTICATED_ANONYMOUSLY } 
     - { path: ^/resetting, role: IS_AUTHENTICATED_ANONYMOUSLY } 
     - { path: ^/admin/, role: ROLE_ADMIN } 

services.yml

parameters: 
    my_user_provider.class: Atotrukis\MainBundle\Service\FOSUBUserProvider 

services: 
    my_user_provider: 
     class: "%my_user_provider.class%" 
     #this is the place where the properties are passed to the UserProvider - see config.yml 
     arguments: [@fos_user.user_manager,{facebook: facebook_id}] 
+0

你的範圍沒有被定義,像這樣:'scope:「email,basic_info,public_profile,user_birthday」' – stevenll 2014-11-05 14:58:15

+0

可悲的是,沒有任何區別 – Einius 2014-11-05 15:27:09

回答

2

所以,問題是,我用錯了路......我不得不用/登錄/ Facebook的不是/登錄/籤的Facebook