我有一个网站(Symfony2),HWIOauthBundle用来连接Facebook,一切都很好。
现在,我正在尝试用Cordova和Ionic框架(AngularJS)构建一个AngularJS应用程序,我想用Facebook验证我的用户:
$cordovaFacebook,我对我的用户进行身份验证,并获得一个有效的Facebook访问令牌,没关系因此,我的问题是:如何使用Facebook连接在前端和后端验证我的用户?
谢谢:)
发布于 2015-09-12 17:40:33
我还想知道如何用HWIOAuthBundle实现服务器端登录。我没有在网络上找到任何解决方案,所以我根据我在网上读到的提示编写了功能。基本上,你必须:
这是我的Symfony控制器:
public function getSecurityFbAction($token)
{
// Get the token's FB app info.
@$tokenAppResp = file_get_contents('https://graph.facebook.com/app/?access_token='.$token);
if (!$tokenAppResp) {
throw new AccessDeniedHttpException('Bad credentials.');
}
// Make sure it's the correct app.
$tokenApp = json_decode($tokenAppResp, true);
if (!$tokenApp || !isset($tokenApp['id']) || $tokenApp['id'] != $this->container->getParameter('oauth.facebook.id')) {
throw new AccessDeniedHttpException('Bad credentials.');
}
// Get the token's FB user info.
@$tokenUserResp = file_get_contents('https://graph.facebook.com/me/?access_token='.$token);
if (!$tokenUserResp) {
throw new AccessDeniedHttpException('Bad credentials.');
}
// Try to fetch user by it's token ID, create it otherwise.
$tokenUser = json_decode($tokenUserResp, true);
if (!$tokenUser || !isset($tokenUser['id'])) {
throw new AccessDeniedHttpException('Bad credentials.');
}
$userManager = $this->get('fos_user.user_manager');
$user = $userManager->findUserBy(array('facebookId' => $tokenUser['id']));
if (!$user) {
// Create user and store its facebookID.
}
// Return the user's JSON web token for future app<->server communications.
}我抛出Symfony\Component\HttpKernel\Exception\AccessDeniedHttpException异常来处理应用程序上的登录错误。
当然,您确实应该使用http的,因为您将交换合理的信息。
我不知道这是否是最好的方法,但效果很好。希望能帮上忙!
发布于 2015-07-28 20:18:40
我觉得Symfony并没有拒绝你的请求。脸谱网是。我不确定这是否有帮助,但我知道在处理Facebook Auth时可能会出现一些问题:
总的来说,您的问题似乎几乎一直与redirect_uri URI格式有关。
https://stackoverflow.com/questions/29898255
复制相似问题