HWIOAuthBundle Symfony2 get user likes from Facebook

676 views Asked by At

In my project I use FOSUserBundle and HWIOAuthBundle for user authentification. integrated HWIOAuthBundle using this tutorial. But additionaly I need to get user likes. I've already added user_likes in a scope and when I try to login facebook says that I want to get user likes but how do I get those likes for example in a Service. Maybe in a FOSUBUserProvider.php which was created in the link I provided above.

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();
        $email = $response->getEmail();
        $name = $response->getRealName();
        $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->setName($name);
            $user->setUsername($email);
            $user->setEmail($email);
            $user->setPlainPassword($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;
    }

}
1

There are 1 answers

0
Charlie Lucas On

To call facebook API i use facebook sdk and for example to get friends, I added this to my controller:

    $user = $this->getUser();
    if ($user && $accessToken = $user->getFacebookAccessToken())
    {
        $friends= [];
        $userRepo = $this->getUserRepository();
        $session = new FacebookSession($accessToken);
        $session->setDefaultApplication($this->container->getParameter('facebook_client_id'), $this->container->getParameter('facebook_client_secret'));
        $friendsData = (new FacebookRequest(
              $session, 'GET', '/me/friends'))->execute()->getGraphObject()->asArray();
        foreach ($friendsData['data'] as $friendData) {
            $friend = $userRepo->findOneByFacebookId($friendData->id);
            if ($friend) {
                $friends[] = $userRepo->findOneByFacebookId($friendData->id);
            }
        }
        return array(
           'user' => $user,
           'friends' => $friends
        );
    }