$facebook->getUser() suddenly stopped working - php

I've got a login with facebook on my website. This has worked well for some time but yesterday suddenly stopped working. I've tracked the problem to the getUser() method which seems to always returns 0 now.
my code looks like:
<?php
require_once('facebook.php');
$facebook = new Facebook(array(
'appId' => 'xxxxxxxxxxxxx',
'secret' => 'xxxxxxxxxxxxxxxxxxxxxxxxxx'
));
$user = $facebook->getUser();
if ($user) {
try {
$profile = $facebook->api('/me');
$logoutUrl = $facebook->getLogoutUrl(
array(
'next'=>$baseUrl.'/fblogin/fblogin.php?logout'
)
);
$userIsLoggedIn=true;
} catch (FacebookApiException $e) {
echo '<pre>'.htmlspecialchars(print_r($e, true)).'</pre>';
$user = null;
$loginUrl = $facebook->getLoginUrl(
array(
'scope'=>'email,publish_stream',
'redirect_uri'=>$returnAfterLoginUrl
)
);
}
}else{
$loginUrl = $facebook->getLoginUrl(
array(
'scope'=>'email,publish_stream',
'redirect_uri'=>$returnAfterLoginUrl
)
);
}
?>
What I've tried (and could find back in my history)
Update the SDK to the latest version
Solution of Facebook PHP SDK - getUser() suddenly returns 0 (adding 2 $CURL_OPTS)
Solution of suddenly, getUser became to return 0.(PHP 3.1.1 SDK) (adding base_domain to $DROP_QUERY_PARAMS)
Solution of Facebook login is suddenly not working anymore? (increasing curlopt_connecttimeout)
Creating a new app, without luck
I'm using PHP Version 5.3.3
I've been trying to get it working since yesterday afternoon, without any luck :(
Does anyone know what might be the problem and more importantly, what might be the solution?
Thank you

Your port 80 or port 241 may be blocked causing a CurlException 7. To check that just upload the example file in your website. It will show the error.
In my case, I had my beta domain hosted on 000webhost.com. It had recently blocked the port 80 which caused the same error. When I moved my domain to another hosting service, the problem was solved.
I hope this helps.
=====EDIT=====
The solution to the problem is that (I think although it is not proven) get an https connection as it my observance that the php sdk does not work in an http connection. Look at my question here
======YEAH! THE FINAL AND CORRECT ANSWER AT LAST=======
This situation can only arise when cURL has been disabled by your web host(applicable for 100% of the cases of similar problem). Your website will run properly if cURL is installed. Usually, the error that users receive (if errorreporting is turned on) is
Fatal error: Uncaught CurlException: 7: couldn't connect to host thrown in /home/.../base_facebook.php on line ...

Make sure sandbox mode is disabled and recheck your settings again including appId, secret .
and try changing your login url to $loginUrl = $this->facebook->getLoginUrl($pram);

Solved it by moving the script to another subdirectory on the server...
/login/ became /facebook/login/
I haven't got the slightest clue why that makes a difference...

$facebook = new Facebook(array(
'appId' => '***************',
'secret' => '**************************',
'cookie' => false //add this and try
));
and try adding permission "read_stream"
check this after clearing your browser cookies..

Its Happen to me and when i check my apache log i found this :
And i found its an SSL problems when communicate with Facebook via PHP SDK (using Curl).
You have to set CURL_OPTS "CURLOPT_SSL_VERIFYPEER" to "false" .
Ex:
facebook::$CURL_OPTS[CURLOPT_SSL_VERIFYPEER] = false;

Related

Facebook login fails on website with SSL

We're building a web app in PHP. We've set up SSL certificate and right after that the FB login stopped working. When the script tries to get user ID, exception is thrown
An active access token must be used to query information about the current user.
I'm 100% positive the SSL is the problem because it works fine on my localhost and it also worked on the production before setting up the SSL.
We're using Facebook PHP SDK. Here is the first part (getting login url)
$facebook = new Facebook([
"appId" => "APP_ID",
"secret" => "SECRET"
]);
$permissions = ["email", "user_birthday", "publish_stream", "offline_access"];
$loginURL = $facebook->getLoginUrl([
"scope" => join(",", $permissions),
"redirect_uri" => "REDIRECT_URI"
]);
// redirect to $loginURL
And here's the callback
$facebook = new Facebook([
"appId" => "APP_ID",
"secret" => "SECRET"
]);
$userID = $facebook->getUser();
$token = $_GET['code'];
if(isset($userID) && isset($token)) {
// ... some cool stuff
} else {
// error, something happened
}
Thanx for help
EDIT: I was getting the error because I was trying to get details about user by the $userID. Problem was, the $userID is always 0 so no wonder I got error. Somehow I can't get user's ID on the website with SSL...
OK in the end I found the cause of my problems and it was realy stupid (like almost always, right?). When I was building the callback URL that is sent as an argument to the FB login page, I was getting the base url with a method from the framework I use. For some reason it gave me domain.com:443/ instead of domain.com/. After I fixed it, it suddenly works.
In mu case it was the WWW that was missing from the redirect URL. SSL cert was working only with it

when attempting to connect to facebook with PHP script endless redirects

first facebook asks me to click okay to authorize the app then chrome gives me this error. and the I have this endless redirect problem. I have read many cases about this but none clear and simple enough to get me over this obstacle.
The webpage at __________&redirect_uri=http%3A%2F%2Fmizu.net46.net%2Ffacebookpost6.php&state=_________________&sdk=php-sdk-3.2.3&req_perms=user_status%2Cpublish_stream%2Cuser_photos%2Coffline_access%2Cmanage_pages#=">https://www.facebook.com/dialog/oauth?client_id=__________&redirect_uri=http%3A%2F%2Fmizu.net46.net%2Ffacebookpost6.php&state=_________________&sdk=php-sdk-3.2.3&req_perms=user_status%2Cpublish_stream%2Cuser_photos%2Coffline_access%2Cmanage_pages#= has resulted in too many redirects. Clearing your cookies for this site or allowing third-party cookies may fix the problem. If not, it is possibly a server configuration issue and not a problem with your computer.
what I know.
facebook.php is working
the secret and appid are being passed correctly by the script.
I must have configured my app settings on the facebook development site almost correctly
how do I know this?
because when I change those variables in my script or adjust the settings
on facebook development server, I dont even get this far, I dont get asked to click ok or see my facebook profile in chrome.
what I dont know.
How to fix this redirect problem. or complete the authentication process. echo $user always returns 0.
my code looks like this:
require 'src/facebook.php';
$app_id = "xxxxxxxxxxxxxxxxxx";
$app_secret = "xxxxxxxxxxx";
$facebook = new Facebook(array(
'appId' => $app_id,
'secret' => $app_secret,
'cookie' => true
));
$user = $facebook->getUser();
echo $user;
if(($facebook->getUser())==0)
{
header("Location:{$facebook->getLoginUrl(array('req_perms' =>
'user_status,publish_stream,user_photos,offline_access,manage_pages'))}");
exit;
}
thanks for any help
changing the setting to no when asked" is this a desktop app? " in the advanced section of app configuration on developers.facebook.com seem to resolve this issue.

Redirect gives 302Found Error in Laravel4

I have a Laravel4 project in which I use Facebook PHP SDK.
In that I use the code to redirect to facebook for auth
$facebook = new Facebook(Config::get('facebook'));
$params = array(
'redirect_uri' => url('/fbcallback'),
'scope' => 'email',
);
return Redirect::to($facebook->getLoginUrl($params));
It was all okay when the project was in local (development) environment but as soon as I deployed the code to (client's) production server (Shared Hosting, cPanel) I started getting errors in redirection.
First I thought it was Facebook but then I realized the errors occur in every redirection that uses the Redirect::to() or similar.
The error is a page
Apart from the fact that the Laravel Redirector class defines the status code 302 as default value (source) which might be the problem here, check out this SO post for details and examples that might help you figure it out:
401 redirect magically results in a 302 redirect
try this instead of the Redirect::to()
return Response::make()->header( 'Location', (string)$facebook->getLoginUrl($params) );

After migrating, Facebook signed_request not being sent back

I've recently migrated our site to Amazon EC2 from another host. On the old server, our Facebook login worked fine, but it's failing on EC2. I've done some digging, and it looks like it's failing because no value is being passed back from Facebook for the signed_request variable. I've done print_r($_REQUEST) on my return page, and the only things Facebook is passing back is code and state, in the URL itself. Nothing in POST.
I did notice that the site URL defined in my app didn't have a slash at the end, which seems to be problematic based on some comments in this forum, but that didn't help. Other answers were from a couple of years ago, and seemed to be obsolete since I couldn't find what they were referring to.
I also tried adding my EC2 IP to my app's whitelist, and that didn't help either.
Here's my current code:
if(!session_start())session_start();
require_once("facebook.php");
require_once("config.php");
$facebook = new Facebook(array(
'appId' => constant("FB_CONSUMER_KEY"),
'secret' => constant("FB_CONSUMER_SECRET"),
'cookie' => false
));
$FbId = intval($facebook->getUser());
if($FbId != 0) {
...
} else {
// log in to FB
$login_url = $facebook->getLoginUrl(array(
'scope' => 'email,user_likes,publish_stream',
'next' => '<return_page>',
'cancel_url' => '<cancel_page>'
));
exit;
}
$FbId keeps coming back with a 0 value, even after logging into Facebook. Some digging showed that this is because the user is never being set in the getUserFromAvailableData function because $_REQUEST['signed_request'] doesn't have a value.
I've tried this both already logged into Facebook and not logged in, but $_REQUEST['signed_request'] isn't returned in either case.
I've seen some posts on here that indicate that old Facebook apps seem to have some kind of grandfathering where they continue to work until they get migrated, or another new app just like it gets created. Could that be the case here?
Any help would be appreciated.
Thanks,

PHP-SDK too many redirects

I'm using the PHP-SDK for Facebook and got the example to work on my localhost. However, when building a script on a server the script stays quite busy with redirecting from my script to facebook and back. (Error: Fout 310 (net::ERR_TOO_MANY_REDIRECTS):)
It seems that many more people have got this issue (here, here, here, here). Though, I can't find a clear answer what goes wrong.
Redirect to facebook (response 302):
https://www.facebook.com/dialog/oauth?client_id=166958180001271&redirect_uri=http%3A%2F%2Fdomain.com%2Fscripts%2FGateway.php%3Faction%3DAllowFacebookAccessAction%26app%3D14&state=0dbc178a375595da4751265a7147c01e#_=_
Redirect to mydomain (response 302):
http://domain.com/scripts/Gateway.php?action=AllowFacebookAccessAction&app=14&state=0dbc178a375595da4751265a7147c01e&code=AQD-dTeyns0OWpGb_PzfHxUy2iRmpc1XgP6Q24DDRX8MiRTE10lV-b-aSNIlOLVHk576vRs3H8Pf9n0kGwU827MrkzUCUoQGFGEQBkkOJnCy9zb6hZs7TVBsKL2iSuZIhDjLsCOPeKy3zfb37Q6LGhtMICCdB_IQAvU0uRvAkSAX8tdVJ65PEv8imx-2yvLaMoGJleZwKogh7m03vlhV8hJk#_=_
Part of the code that creates this issue
...
$facebook = new FacebookApi(array(
'appId' => $app->getProperty('apiKey'),
'secret' => $app->getProperty('secretKey'),
));
$user = $facebook->getUser();
if (!$user) {
header('location: ' . $facebook->getLoginUrl());
exit;
}
...
UPDATE:
I ruled out that its a server setting. I was able to run the original example script on that server.
Recheck your app secret in your app settings https://developers.facebook.com/apps otherwise dump the session
<?php print_r($_SESSION); ?>
And lint the token returned https://developers.facebook.com/tools/debug
Pretty sure either the token is mangled or the secret.
If not, it's in code you haven't shown.

Categories