在我的项目中,我使用FOSUserBundle和HWIOAuthBundle进行用户身份验证。使用此tutorial集成HWIOAuthBundle。但另外我需要得到用户的喜欢。我已经在范围中添加了user_likes
,当我尝试登录facebook时,我想让用户喜欢,但我如何在服务中获得这些喜欢。也许在我上面提供的链接中创建的FOSUBUserProvider.php中。
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;
}
}
答案 0 :(得分:0)
要拨打facebook API,我使用facebook sdk,例如为了结交朋友,我将其添加到我的控制器中:
$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
);
}