Vince
Vince

Reputation: 4222

Facebook TokenResponseException with Laravel 4.1 and oauth-4-laravel

I'm getting a TokenResponseException from Laravel when I try to login to my web site with Facebook using oauth-4-laravel.

OAuth \ Common \ Http \ Exception \ TokenResponseException
Failed to request resource.

Using Facebook's JavaScript API works like its supposed to, so I'm pretty sure my application is configured correctly.

Are there any known issues that might cause this problem? Am I doing something wrong, or is this a bug in the library?

With the exception of a redirect line that works around another bug, my code is identical to the example code at the GitHub page. I think the exception is thrown when I try to get the token from the Facebook Service object.

Here's the code:

public function loginWithFacebook() {
    // get data from input
    $code = Input::get( 'code' );

    // get fb service
    $fb = OAuth::consumer( 'Facebook' );

    // check if code is valid

    // if code is provided get user data and sign in
    if ( !empty( $code ) ) {
        // This was a callback request from google, get the token
        $token = $fb->requestAccessToken( $code );

        // Send a request with it
        $result = json_decode( $fb->request( '/me' ), true );

        $message = 'Your unique facebook user id is: ' . $result['id'] . ' and your name is ' . $result['name'];
        echo $message. "<br/>";

        //Var_dump
        //display whole array().
        dd($result);

    }
    // if not ask for permission first
    else {
        // get fb authorization
        $url = $fb->getAuthorizationUri();

        // return to facebook login url
        // ref: https://github.com/artdarek/oauth-4-laravel/issues/27
        //return Response::make()->header( 'Location', (string)$url );
        return Redirect::to((string)$url);
    }

}

And a screenshot:

screenshot

Upvotes: 3

Views: 2592

Answers (6)

Mahfuz
Mahfuz

Reputation: 467

first try to load file_get_contents("https://www.facebook.com");

if allow_url_fopen=0 was set in the php.ini it will not work so you need to change it to allow_url_fopen=1

Upvotes: 1

rockstardev
rockstardev

Reputation: 13527

I had the same problem. I was trying to access facebook graph. It wasn't that it could not access the URL, it was that Facebook was returning a 400 error because I wasn't passing parameters through. Try connecting to HTTPS on a site that will have a working HTTPS connection. E.g. Try this:

file_get_contents("https://www.namhost.com");

If that works, you must see why the HTTPs connection you are connecting to is failing, because the problem isn't that you can't connect to HTTPs, but rather that what you are connecting to isn't liking the request.

Upvotes: 0

Khay
Khay

Reputation: 1570

The problem should be here $token = $fb->requestAccessToken( $code );

When I tried var_dump it, I get this.

object(OAuth\OAuth2\Token\StdOAuth2Token)[279]
protected 'accessToken' => string 'your-accessToken' (length=180)
protected 'refreshToken' => null
protected 'endOfLife' => int 1404625939
protected 'extraParams' => 
     array (size=0)
  empty

Try this and check what you've get as accessToken. Anyway, your function loginWithFacebook working fine with me.

Upvotes: 0

Phil McCarty
Phil McCarty

Reputation: 486

I had this exact same problem. It turns out that my issue was that my return_uri was missing a trailing slash, which through off the entire process. Make sure that when you're calling it you've added it.

$fb = OAuth::consumer('Facebook','http://url.to.redirect.to/');

NOT

$fb = OAuth::consumer('Facebook','http://url.to.redirect.to');

Upvotes: 1

Robert
Robert

Reputation: 5963

Since my previous answer was deleted.. Let's try again..

--

After some debugging I think I got it. Commenting out the error_reporting part visible in your code snippet will already tell you a lot. The file_get_contents call got a 401 Unauthorized because the tokens were no longer valid.

After changing code, start your auth process from the beginning and don't refresh your url half-way, that wil cause the error.

Upvotes: 0

Rene Weteling
Rene Weteling

Reputation: 524

The weirdest thing i have your problem with google not with facebook, so here is my code maby it helps you fix your fb problem

public function loginWithFacebook() {

    // get data from input
    $code = Input::get( 'code' );

    // get fb service
    $fb = OAuth::consumer( 'Facebook' );



    // check if code is valid

    // if code is provided get user data and sign in
    if ( !empty( $code ) ) {

        // This was a callback request from facebook, get the token
        $token = $fb->requestAccessToken( $code );

        // Send a request with it
        $result = json_decode( $fb->request( '/me' ), true );

        $message = 'Your unique facebook user id is: ' . $result['id'] . ' and your name is ' . $result['name'];
        echo $message. "<br/>";

        //Var_dump
        //display whole array().
        dd($result);

    }
    // if not ask for permission first
    else {


        // get fb authorization
        $url = $fb->getAuthorizationUri();

        // return to facebook login url
        return Redirect::to( (string)$url );            
    }

}

Upvotes: 0

Related Questions