laravelfacebook-graph-api

How to get birthday gender and religion information using laravel socialite implementing facebook Api


I just started using laravel, I am using laravel socialite.

I am able to get username and other data by using following code. But how do I get other data like birthday, gender, religion etc that is not in $user variable ??

my code

$user = Socialite::with('facebook')->user();

return "<h2> User Name ".$user->getName()."</h2><h2> ID :".$user->getId()."</h2><h2>Full Object: ".var_dump($user)"About".$user->getAbout();

my output:

object(Laravel\Socialite\Two\User)#170 (10) { ["token"]=> string(162) "EAADg3CcIylABAESNrt1ZBsHiLzlR7uu4e4SCjNsU8jJK6zAurEZBGW7BexQiMjiRkrIw57LYPuNrFZBiaW8MIWAh0oIOIIyFF916PH8SDCtU68r83OYBsXbJzS5x4AzIQlpxX8W7V6Mtg446CqwocGXZCHXRFv8ZD" ["refreshToken"]=> NULL ["expiresIn"]=> string(7) "5170770" ["id"]=> string(15) "624135571074425" ["nickname"]=> NULL ["name"]=> string(21) "Muhammad Abdullah Ali" ["email"]=> string(27) "abdullahbinali3@hotmail.com" ["avatar"]=> string(67) "https://graph.facebook.com/v2.6/624135571074425/picture?type=normal" ["user"]=> array(5) { ["name"]=> string(21) "Muhammad Abdullah Ali" ["email"]=> string(27) "abdullahbinali3@hotmail.com" ["gender"]=> string(4) "male" ["verified"]=> bool(true) ["id"]=> string(15) "624135571074425" } ["avatar_original"]=> string(66) "https://graph.facebook.com/v2.6/624135571074425/picture?width=1920" }

User Name Muhammad Abdullah Ali

ID :624135571074425


Solution

  • In socialite redirect to facebook method :

        public function redirectToFacebook()
        {
            return Socialite::driver('facebook')->fields([
                'first_name', 'last_name', 'email', 'gender', 'birthday'
            ])->scopes([
                'email', 'user_birthday'
            ])->redirect();
        }
    

    To get the birth date.

    Then to use it in your own models :

        public function handleFacebookCallback()
        {
            $facebook_user = Socialite::driver('facebook')->fields([
                'first_name', 'last_name', 'email', 'gender', 'birthday'
            ])->user();
        }
    

    Note: it will ask for the user to accept sharing their birth date when they connect to Facebook though.