joshmanders's avatar

Laravel Socialite Facebook - Bad Request

I am trying to use Facebook with Laravel Socialite but I keep getting Bad Request errors from Guzzle and I honestly have no idea how to fix this.

If I copy and paste the url in the error, I get an access token response.

0 likes
27 replies
bashy's avatar

Have you tried turning it on and off again?

Serious: More info, can't tell what you're on about :)

citricsquid's avatar

The Bad Request error means that your request is bad, Facebook is receiving data from you that is not valid: invalid configuration and invalid tokens are common causes. If the request URL is valid when you copy and paste it from the error message -- but isn't valid when the request is made by your application -- that would indicate that you have some form of hidden character somewhere, which your browser is removing and making the request valid.

The first step to debug would be to ensure that you're not introducing problems with your configuration: remove your Facebook entry from config/services.php and then start over, add it in again ensuring that your keys (and values) are all accurate.

Side note, a piece of advice for the future: debugging depends heavily on context, posting threads without any makes it very difficult to provide you help. If you're posting a help thread include any error messages at a minimum, and if you have any additional context add that too. For example this thread could have been:

"Hello, I'm following this episode on Laravel Socialite in Laravel 5 (using Facebook instead of GitHub) and when I try and login the following error message is displayed:

Client error response [url] https://graph.facebook.com/oauth/access_token?client_id=805980719423812&client_secret=00f242e263af42a69bd1e986716f8c60&code=AQCUmRQp6ElbKDVig8n2luAnioWwcMQQDdy1FdlSRrogcne6p1ro0f2YoLCY96AZZmFqg8NMc3LBe9uu2LfhVwj6vJp7FykeKQtxcv-IzkOAlJ8Coctp-BCo2rwGpHAtlxZjST4mXyOh5s0i8JIRNFBpwA4Lu6MsGV9SSS-s6rcw1QuXg76UvqsaNf0Gn_kvFDxz5mWRPlXJ1rGhwpwCmrOJSUCw3shywvSgM9zy_tr5ZE73OrKQEnKbk3_tT4NPZl_1Zd-JVCGz19cnrmYkZWL0tdHIC8_7GWA15qRSEd3tfHt46oBgtidM_zRbXWb2uTfGbLY5FIy4pBucjHJ054hdGsP8cdLrBFuTisaIaQGtpw&redirect_uri=http%3A%2F%2F192.168.63.2%2Flogin [status code] 400 [reason phrase] Bad Request

I can copy and paste that url and it works fine."

:)

2 likes
joshmanders's avatar

I've done everything, still getting bad requests and I can't even debug what the actual error message returned by Facebook is.

joshmanders's avatar
joshmanders
OP
Best Answer
Level 52

Figured it out after going into vendor/laravel/socialite/src/Two/FaceBookProvider.php and wrapping the calls with try/catches to get the actual response from Facebook. Have to submit a pull request with these in it, because generic 400 errors are not helpful. I spent all night and half of today trying to figure this out.

troelsjohnsen's avatar

So, what was the problem and how did you fix it? I am getting bad requests as well.

deanhowe's avatar

@troelsjohnsen I think the issue was probably with config/service.php

I was getting the same error until I had mine setup like this...

    'facebook' => [
        'client_id'         =>  'your-facebook-app-id',
        'client_secret'     =>  'your-facebook-app-secret',
        'redirect'          =>  'http://your-registered-call-back-url',
    ]           

I think the confusion is with the 'client_secret' this needs to be your 'App Secret' (yes the one you need to type your password in for).

It's easy enough to assume that the required 'client_secret' for Socialite is the 'Client Token' under the 'Advanced' tab in the Facebook settings, which it isn't.

1 like
antonioribeiro's avatar

Had the very same problem here. A Four Hundred not helping at all, but... the information you need is in the ClientException object:

exception

1 like
antonioribeiro's avatar

@khoanguyenme, usually I only use MyImgur to create the Imgur link, but I had to edit this one, so I did:

  • Print screen (to copy).
  • Paste on Paint.NET (to crop and edit), very easy to edit on this one.
  • ( ctrl+shift+4 ) opens MyImgur in " take a screen capture" mode to upload and create the Imgur link.
1 like
JackD's avatar

can somebody post a step by step using the latest laravel 5 with facebook login

1 like
Alain1405's avatar

FYI, I had the same error. I realized the callback url didn't match the one set in Facebook. It took a while do find it out. I had double // after the domain name, %2F%2F in url friend hex values.

2 likes
administrator@myfarms.com's avatar

I had this same error too, but none of the above solved my problem. It turns out that I needed to disable the "Require app secret for server API calls" setting in the Facebook app Advanced settings.

dlenzin's avatar

@Killswitchthe callback url should be matched with the one set in Facebook. Did you notice about that?

epileftro85's avatar

i am having this very same issue, i cleaned up the logs folder and after that it works fine, but when you return to your app it appears again, so according to @antonioribeiro 's log, i think that it is just because the facebook authorization code still in the log's cache, so i don't know if it is related to not logout from the app.

i found this in stackoverflow and it worked, i can't say that it is the appropriate way to do that but it worked for me.

file : AbstractProvider.php (two) line: 160 public function user() { //if ($this->hasInvalidState()) { // throw new InvalidStateException; //}

$user = $this->mapUserToObject($this->getUserByToken(
    $token = $this->getAccessToken($this->getCode())
));
return $user->setToken($token);

}

parweb's avatar

same as you @Alain1405 pfff lucky to find this quick and not spent hours crying alone ^^

JokeDiaz's avatar

Hi ! in my case this error was related with a little javascript i've placed for clean de #_=_ url after de redirection , i've just quit and it works !

NITIN@TIWARI's avatar

I just encountered this problem and I fixed it by resetting my client token (Advanced > Security on the Facebook App settings page) and changing my app type from Native / Desktop to Web app.

dashty's avatar

I am still facing this problem, due to my understanding, I think it is because of the #_=_ in url after the posting with guzzle and redirection. But I don't know how to fix that. Anyone?

Your help is highly appreciated

Srinivasan's avatar

Solved !! I too had the same problem and couldn't solve it so I changed back to GitHub and it worked so I decided it is basically set up with the, facebook account .

In my case the error was since the FaceBook App was set to Native or desktop app. selecting No to Native or desktop app? in Settings > Advanced fixed the things.

For some who hit this page like I did, it may be of some use.

loginn's avatar

Eu resolvi o meu problema retirando " / "do meu arquivo .env, estava assim "https://meusite.com/" Depois retirei a barra do final do endereço "/ "do final e funcionou! ;)

#2020

actuallyakash's avatar

IDK if anybody had mentioned this above, I hadn't read all the answers

but I had missed the s in https

I have used callback URL in .env file without https and was using a callback URL with https in Facebook dashboard.

Please or to participate in this conversation.