集成FosUserBundle和HWIOAuthBundle

时间:2016-09-13 10:35:12

标签: facebook symfony fosuserbundle hwioauthbundle

Integrating with FOSUserBundle中实施安装和文档,并在A way to integrate FosUserBundle and HWIOAuthBundle

后面添加评论和说明

从Facebook连接API获取正确的代码我能够将我的Facebook帐户与项目连接,我可以看到应用程序已添加到我的Facebook应用程序中。 但现在我想从FB获取用户数据并将其存储在我的数据库中。我有点理解该做什么但是在连接FB后没有调用函数public function loadUserByOAuthUserResponse(UserResponseInterface $response)namespace MarketplaceBundle\Security\Core\User; use HWI\Bundle\OAuthBundle\OAuth\Response\UserResponseInterface; use HWI\Bundle\OAuthBundle\Security\Core\User\FOSUBUserProvider as BaseFOSUBProvider; use Symfony\Component\Security\Core\User\UserInterface; class MyFOSUBUserProvider extends BaseFOSUBProvider { /** * {@inheritDoc} */ public function connect(UserInterface $user, UserResponseInterface $response) { // get property from provider configuration by provider name // , it will return `facebook_id` in that case (see service definition below) $property = $this->getProperty($response); $username = $response->getUsername(); // get the unique user identifier //we "disconnect" previously connected users $existingUser = $this->userManager->findUserBy(array($property => $email)); if (null !== $existingUser) { // set current user id and token to null for disconnect // ... $this->userManager->updateUser($existingUser); } // we connect current user, set current user id and token // ... $this->userManager->updateUser($user); } /** * {@inheritdoc} */ public function loadUserByOAuthUserResponse(UserResponseInterface $response) { $userEmail = $response->getEmail(); $user = $this->userManager->findUserByEmail($userEmail); // if null just create new user and set it properties if (null === $user) { $username = $response->getRealName(); $user = new User(); $user->setFirstName($response->getFirstName()); $em = $this->getDoctrine()->getManager(); $em->persist($user); $em->flush(); // ... save user to database return $user; }else{ $username = $response->getYamen(); $user = new User(); $user->setUsername($username); } // else update access token of existing user $serviceName = $response->getResourceOwner()->getName(); $setter = 'set' . ucfirst($serviceName) . 'AccessToken'; $user->$setter($response->getAccessToken());//update access token return $user; } } 。 我确信这是因为我在那里添加了不存在的方法,就像getWhatever和setSomething一样,即使语法错误也没有发生任何事情并且没有记录日志,这意味着100%这个类永远不会被调用。 如何解决这个问题? 以下是在示例中编辑的文件。 提前谢谢。

MyFOSUBUserProvider.php

security:
    encoders:
        FOS\UserBundle\Model\UserInterface: bcrypt

    providers:
        fos_userbundle:
            id: fos_user.user_provider.username

    firewalls:
        ...
        secured_area:
            pattern: ^/
            oauth:
                resource_owners:
                    facebook:      /login_facebook
                login_path:        /login
                failure_path:      /login
                oauth_user_provider:
                    service: my_user_provider

            # Turn on anonymous for testings need.
            anonymous: ~

    access_control:
        ...

security.yml

...
hwi_oauth:
    connect:
        account_connector: my_user_provider
    firewall_names:        [secured_area]
    fosub:
        username_iterations: 30
        properties:
            facebook:    facebook_id
    resource_owners:
        facebook:
            type:          facebook
            client_id:     'id'
            client_secret: 'secret'
            scope:         "email,public_profile,user_friends,user_birthday,user_location"
            options:
                display: popup

config.yml

parameters:
    ...
    my_user_provider.class: MarketplaceBundle\Security\Core\User\MyFOSUBUserProvider

services:
    ...
    hwi_oauth.user.provider.entity:
        class: HWI\Bundle\OAuthBundle\Security\Core\User\OAuthUserProvider
    my_user_provider:
        class:        "%my_user_provider.class%"
        arguments: ['@fos_user.user_manager', { facebook: facebook_id }]

#routing.yml
hwi_oauth_redirect:
    resource: "@HWIOAuthBundle/Resources/config/routing/redirect.xml"
    prefix:   /facebook/connect

facebook_login:
    path: /login_facebook
...

services.yml

window.fbAsyncInit = function() {
                    // init the FB JS SDK
                    FB.init({
                        appId      : 'id',                        // App ID from the app dashboard
                        status     : true,                                 // Check Facebook Login status
                        xfbml      : true,                                  // Look for social plugins on the page
                        version    : 'v2.5'
                    });
                };

                // Load the SDK asynchronously
                (function(d, s, id){
                    var js, fjs = d.getElementsByTagName(s)[0];
                    if (d.getElementById(id)) {return;}
                    js = d.createElement(s); js.id = id;
                    js.src = "//connect.facebook.net/en_US/all.js";
                    fjs.parentNode.insertBefore(js, fjs);
                }(document, 'script', 'facebook-jssdk'));

                function fb_login() {
                    FB.getLoginStatus(function(response) {
                        if (response.status === 'connected') {
                            // connected
                            alert('Already connected, redirect to login page to create token.');
                            console.log(response);
                            document.location = "{{ url("account") }}";
                        } else {
                            // not_authorized
                            FB.login(function(response) {
                                if (response.authResponse) {
                                    document.location = "{{ url("account") }}";
                                } else {
                                    alert('Cancelled.');
                                }
                            }, {scope: 'email,public_profile,user_friends,user_birthday,user_location'});
                        }
                    });
                }

facebookJsCallFunction

TestBed.configureTestingModule({imports: [AppModule]});

1 个答案:

答案 0 :(得分:1)

这是我自己的解决方案:

security:
    firewalls:
        main:
            oauth:
                oauth_user_provider:
#                    service: hwi_oauth.user.provider.fosub_bridge
                    service: acme.user_provider

即使我收到“用户帐户被禁用”。我的FB测试App测试用户

上的错误消息