简体   繁体   中英

Facebook PHP SDK 4.0 Login with Lumen

I writing a small application where I'm trying to get some Facebook information, but I can't manage to get the authentication to work.

At first I've setup the AppServiceProvider with:

FacebookSession::setDefaultApplication(static::APP_ID, static::APP_SECRET);

Then In my view I just have a button which goes to the auth URL.
The auth route is using the HomeController@index , which contains the following:

public function auth(Request $request)
{
    $helper = new FacegroupRedirectLoginHelper($request->url());

    if($session = $helper->getSessionFromRedirect()) {
        dd($session);
    }

    return redirect($helper->getLoginUrl());
}

I've created my own FacebookRedirectLoginHelper which extends the base class of Facebook. This has the following methods, just to leverage the Lumen Session implementation:

protected function storeState($state)
{
    Session::put(static::SESSION_KEY, $state);
}

protected function loadState()
{
    return $this->state = Session::get(static::SESSION_KEY, null);
}

This code is not working somehow, I've seen people doing this on the internet as well. What could be wrong? The session is always NULL .

Facebook SDK V5 for Laravel Works Fine with Lumen you just have to change the Routing ... Here is how you configure and use LaravelFacebookSdk with Lumen

To use first

{
    "require": {
        "sammyk/laravel-facebook-sdk": "~3.0"
    }
}

Then in your bootstrap/app.php of lumen add

// Facebook Sdk Provider
$app->register('SammyK\LaravelFacebookSdk\LaravelFacebookSdkServiceProvider');

Then pubish the config ( Make sure you have config folder setup with lumen , By default it isn't there )

$ php artisan vendor:publish --provider="SammyK\LaravelFacebookSdk\LaravelFacebookSdkServiceProvider"

The Main Step is to configure The Service provider and Laravel Facebook Sdk By Default LaravelFacebookSdk.php and LaravelUrlDetectionHandler.php uses Illuminate\\Routing\\UrlGenerator . You will find those files in vendor src folder replace it with Laravel\\Lumen\\Routing\\UrlGenerator . And you are done . ( Be careful editing vendor src files are not recommended because it will be overwritten next time you call composer update or new version appears on composer repository . This is just a Quick fix for testing . Fork it, make your own repo or find a way )

Then you wil be able to use LaravelFacebookSdk in Lumen like this

$app->get('/user', function(SammyK\LaravelFacebookSdk\LaravelFacebookSdk $fb){

    // Get basic info on the user from Facebook.
    try {

        $token = "your-access-token-here";

        $fb->setDefaultAccessToken($token);

        $response = $fb->get('/me?fields=id,name,email');
    } catch (Facebook\Exceptions\FacebookSDKException $e) {
        dd($e->getMessage());
    }

    // Convert the response to a `Facebook/GraphNodes/GraphUser` collection
    $facebook_user = $response->getGraphUser();

    var_dump($facebook_user);

});

For the people who are new with Lumen. In order to make the Session service working, you have to enable it in your bootstrap/app.php line 55 to line 61.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM