artdarek / oauth-4-laravel

OAuth Service Provider for Laravel 4
685 stars 217 forks source link

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

Closed chhumsina closed 7 years ago

chhumsina commented 8 years ago

1 2

Thanks you

FebriPratama commented 8 years ago

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'))

chhumsina commented 8 years ago

Hi @FebriPratama ,

Could you please paste the full sample?

Thanks you :)

FebriPratama commented 8 years ago

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

`

chhumsina commented 8 years ago

@FebriPratama

After putting your full sample code I get this: capture

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

FebriPratama commented 8 years ago

@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

chhumsina commented 8 years ago

@FebriPratama

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

Sorry for interrupt you again Thank you :)

thecodecafe commented 7 years ago

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

thecodecafe commented 7 years ago

@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.

arirah commented 7 years ago

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."]); }