Unrecognized field: facebookId

蹲街弑〆低调 提交于 2019-12-22 13:34:26

问题


I try to connect symfony 3 with HWIOAUTH Bundle and follow the tutorial https://gist.github.com/danvbe/4476697.

file config.yml

hwi_oauth:
connect:
    account_connector: my_user_provider
firewall_names: [main]
fosub:

    username_iterations: 30


    properties:
        google: googleId
        facebook: facebookId
resource_owners:
    facebook:
        type:                facebook
        client_id:           ***************
        client_secret:       ***************
        scope:               ""
    google:
        type:                google
        client_id:           ***************
        client_secret:      ***************
        scope:               "https://www.googleapis.com/auth/userinfo.email https://www.googleapis.com/auth/userinfo.profile"

security.yml

main:
        pattern:             .*
        context:             user
        form_login:
            provider:       fos_userbundle
            login_path:     /login
            use_forward:    false
            check_path:     /login_check
            failure_path:   null
            default_target_path: list_items
            always_use_default_target_path: true
        oauth:
            resource_owners:
                facebook:           "/connect/check-facebook"
                google:             "/connect/check-google"
            login_path:        /connect
            failure_path:      /connect

            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

service.yml

my_user_provider:
    class: AppBundle\Entity\FOSUBUserProvider
    arguments: ['@fos_user.user_manager',{facebook: facebookId, google: googleId}]

Please help me fix this bug. I spent many times but can't fix it.

Update: Entity User

 /**
 * @var string
 *
 * @ORM\Column(name="facebookId", type="string", nullable=true)
 */
private $facebookId;

回答1:


I use symfony 3.3.15 , this is correct configuration:

//User.php
<?php

namespace BackendBundle\Entity;

use FOS\UserBundle\Model\User as BaseUser;
use Doctrine\ORM\Mapping as ORM;
use Doctrine\Common\Collections\ArrayCollection;
use Symfony\Bridge\Doctrine\Validator\Constraints\UniqueEntity;

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

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

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

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

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


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

        return $this;
    }

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

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

        return $this;
    }

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

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

        return $this;
    }

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

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

        return $this;
    }

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

}

Other file services.yml:

//services.yml
services:
    my_user_provider:
        class: AppBundle\Security\Core\User\FOSUBUserProvider
        arguments: ['@fos_user.user_manager',{facebook: facebook_id,google: google_id}]

Next file FOSUBUserProvider:

//Security/Core/User
<?php
namespace AppBundle\Security\Core\User;

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)
    {
        $username = $response->getUsername();

        $service = $response->getResourceOwner()->getName();
        $setter = 'set' . ucfirst($service);
        $setterId = $setter . 'Id';
        $setterToken = $setter . 'AccessToken';

        //disconnect previously connected user
        if (null !== $previousUser = $this->userManager->findUserBy(array($this->getProperty($response) => $username))) {
            $previousUser->$setterId(null);
            $previousUser->$setterToken(null);
            $this->userManager->updateUser($previousUser);
        }

        //connect current user
        $user->$setterId($username);
        $user->$setterToken($response->getAccessToken());

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

    /**
     * {@inheritdoc}
     */
    public function loadUserByOAuthUserResponse(UserResponseInterface $response)
    {
        $username = $response->getUsername();
        $email = $response->getEmail();

        $user = $this->userManager->findUserBy(array($this->getProperty($response) => $username));
        if (null === $user) {
            $user = $this->userManager->findUserByEmail($email);

            $service = $response->getResourceOwner()->getName();
            $setter = 'set' . ucfirst($service);
            $setterId = $setter . 'Id';
            $setterToken = $setter . 'AccessToken';

            if (null === $user) {
                $user = $this->userManager->createUser();
                $user->$setterId($username);
                $user->$setterToken($response->getAccessToken());

                $user->setUsername($username);
                $user->setEmail($email);
                $user->setPassword($username);
                $user->setEnabled(true);
                $this->userManager->updateUser($user);

                return $user;
            } else {
                $user->$setterId($username);
                $user->$setterToken($response->getAccessToken());

                $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;
    }
}

The file config.yml

fosub:
        username_iterations: 30
        properties:
            facebook:    facebook_id
            google:      google_id
    resource_owners:
        facebook:
            type:                facebook
            client_id:           gkhgkjgkjgkjgkj
            client_secret:       kjhkjkjhkjhlkjhlkjh
            #scope:               "email"
            #scope:               ""
            scope:               "email,public_profile,user_friends,user_birthday,user_location"

        google:
            type:                google
            client_id:           kjhlkjhlkjhljhlkjhlk-gkhjgkjhgkjhgkgjhkjhgkjgkjgkjh.apps.googleusercontent.com
            client_secret:       gjgjgj-hkjhgkhgkjh
            #scope:               "https://www.googleapis.com/auth/userinfo.email https://www.googleapis.com/auth/userinfo.profile"
            scope:               "email profile"

Next file: security.yml

oauth:
                resource_owners:
                    facebook:           "/login/check-facebook"
                    google:             "/login/check-google"
                login_path:        /login
                failure_path: /login
                oauth_user_provider:
                    service: my_user_provider

Regards




回答2:


First update you schema and see in DB, do you find facebookId field in user table?

If there is no field like facebookId in user. Then update your entity annotation as below:

/**
 * @var string
 *
 * @ORM\Column(name="facebookId", type="string", nullable=true)
 */
private $facebookId;

Notice the change in annotation comments. See the one space before "*" after "/**" line. I have not used Symfony 3 but I had similar issue when I was using Symfony 2.8, so as per my experience I suggested. Might help!!



来源:https://stackoverflow.com/questions/47524045/unrecognized-field-facebookid

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!