artdarek / oauth-4-laravel

OAuth Service Provider for Laravel 4

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

(Facebook Login) Failed to request resource. HTTP Code: HTTP/1.1 400 Bad Request

chhumsina opened this issue · comments

1
2

Thanks you

got same issue, it seems that the requestAccessToken method a bit deprecated due fb api update, this is what i do

  1. change to curl

$OAuth = new OAuth(); $OAuth::setHttpClient('CurlClient');

  1. then add redir uri param

$fb = $OAuth::consumer( 'Facebook' , Input::get('redirectUri'))

Hi @FebriPratama ,

Could you please paste the full sample?

Thanks you :)

@chhumsina
Here is the sample

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

    $OAuth = new OAuth();
    $OAuth::setHttpClient('CurlClient');

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

    // 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 );

        //$token = json_decode( $token, true );

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

           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 );
    }

`

@FebriPratama

After putting your full sample code I get this:
capture

Could you please tell me about this?
Thank you :)

@chhumsina download cacert.pem http://curl.haxx.se/ca/cacert.pem then paste it to your php/extras/ssl folder, ex : xampp/php/extras/ssl . then restart ur server

@FebriPratama

I already added and it's working with this certificate but another issue is appeared.
capture

Sorry for interrupt you again
Thank you :)

@FebriPratama I'm having the same problem as @chhumsina

@FebriPratama @chhumsina think i figured it out, you can't request for an access token more than once so the person will have to rerun the login process again, to avoid this try wrapping the code snippet for requesting access token in a try catch scope like so.

try{
    // 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 );
}catch(Exception $e){
    return Redirect::to('auth/login')->with("sys_message", ["body"=>"An error occurred please try again."])
}

Hope this helped.

try{
// 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 );
}catch(Exception $e){
return Redirect::to('auth/login')->with("sys_message", ["body"=>"An error occurred please try again."]);
}