在Laravel 4中使用Facebook登录

时间:2014-10-01 16:09:56

标签: php facebook-graph-api laravel laravel-4 facebook-php-sdk

我所要做的就是让用户与Facebook联系,获取他的信息(电子邮件,姓名,profile_pic)并在我的网站上自动注册。

问题是当我访问/ user / login / fb /我被重定向到fb应用程序以授予权限然后它将我重定向到

  

http://local.serv/user/login/fb/callback?code=AQB7Uf7pcZSY89WYUKofY8bOgBY1qCcWA5p6_G-9MVdczN60C0TNOgFIi9LBRwquagZ6VbC6_kjD9-D0ZevKHyNGw88LuRJcuqdq2RbwCKNlC02_4eMtiNAWxX76XHm1CzU0F7nSVQ4KxENoDPFpKIs1oV-wQTkO7YuuGznL9UuzfK-hqTtZAIRBvQ_N_yh03LsdgaZ9-f6z5yc5FGnJ3sJyrC-3DGrjkSzhPpJHHk5tr7LorzqW9TNcaFpB9eE51wdsEHOa5oNEWycwzXjY-7Me1Uod3KVBGpb8tzeYVHArphMc2n45IF1pfAWMZZZ51Dw&state=f25e8641df013970542b7463b8899ccf#=

并给我这个错误

Facebook \ FacebookSDKException (700) 
You must provide or set a default application id.

这是我的路线

Route::get('user/login/fb', function() {

    FacebookSession::setDefaultApplication('', '');
    $helper = new FacebookRedirectLoginHelper('user/login/fb/callback');
    $loginUrl = $helper->getLoginUrl();
    return "<a href='$loginUrl'>facebook</a>";
});

Route::get('user/login/fb/callback', function() {
    // A FacebookResponse is returned from an executed FacebookRequest
    $helper = new FacebookRedirectLoginHelper('user/login/fb/callback');
    $session = $helper->getSessionFromRedirect();
    $request = new FacebookRequest($session, 'GET', '/me');

    try {
        $response = $request->execute();
        $me = $response->getGraphObject();
    } catch (FacebookRequestException $ex) {
        echo $ex->getMessage();
    } catch (\Exception $ex) {
        echo $ex->getMessage();
    }

    print_r($me);//->getProperty("id"));

    $user = new Giftee;
        $user->name    = $me->getProperty('first_name') . ' ' . $me->getProperty('last_name');
        $user->email    = $me->getProperty('email');
        $user->photo    = 'http://graph.facebook.com' . $me->getProperty('id') . '/picture?type=large';

        $user->save();

});
PS:如果你有一个清晰的教程,有关于如何将fb登录与laravel集成的一切,请分享,我一直在寻找天,材料是如此有限。

1 个答案:

答案 0 :(得分:2)

当您请求user/login/fb/callback时,未设置您的Facebook API凭据,或者在该路由的开头添加FacebookSession::setDefaultApplication(...),或者使用另一个允许您在全局设置这些路径的库配置文件。

您的路线应如下所示:

Route::get('user/login/fb/callback', function() {
    FacebookSession::setDefaultApplication(); // your API credentials

    // here goes the rest of your code
};