(Facebook Login) Failed to request resource. HTTP Code: HTTP/1.1 400 Bad Request
chhumsina opened this issue · comments
got same issue, it seems that the requestAccessToken method a bit deprecated due fb api update, this is what i do
- change to curl
$OAuth = new OAuth(); $OAuth::setHttpClient('CurlClient');
- then add redir uri param
$fb = $OAuth::consumer( 'Facebook' , Input::get('redirectUri'))
@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 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'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."]);
}