PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Friday, February 4, 2022

[FIXED] Laravel 5 get user info from facebook

 February 04, 2022     facebook, facebook-php-sdk, laravel, laravel-5, php     No comments   

Issue

I'm trying to get an user id from Facebook with the Facebook PHP-sdk with Laravel 5. I did a lot of search on the errors I got, but none of the answers helped.

Set keys:

use Facebook\FacebookSession;
FacebookSession::setDefaultApplication('XXX' , 'XXX');

Redirect to Facebook:

public function getFacebook()
{
    $helper = new FacebookHandler(url('settings/set_facebook'));
    return Redirect::to($helper->getLoginUrl());
}

Get the info:

public function setFacebook()
{
    try{
        $session = new FacebookSession(\Session::get('state'));
        $request = new FacebookRequest($session, 'GET', '/me');
        $response = $request->execute();
        $graphObject = $response->getGraphObject()->asArray();
        dd($graphObject);
    } catch(Exception $e){
        dd(
            $e->getMessage(),
            $e->getCode(),
            $e
        );
    }
}

Code for Laravel sessions:

use Facebook\FacebookRedirectLoginHelper;
use Illuminate\Support\Facades\Session;

class FacebookHandler extends FacebookRedirectLoginHelper
{
    protected function storeState($state)
    {
        Session::put('state', $state);
    }

    protected function loadState()
    {
        return $this->state = Session::get('state');
    }
}

Output:

"Invalid OAuth access token."

190

FacebookAuthorizationException {#215 ▶}

What am I doing wrong?


Solution

use Laravel Socialite. It supports facebook, twitter, google+ and github. and also easy to use.



Answered By - Jignesh Solanki
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing