Facebook PHP SDK getLoginUrl failed - strange - facebook

I was developing facebook canvas application, and I found this simple code fails. I don't know what went wrong, because I took it straight from tutorial:
<?php
require 'facebook.php';
/*
Facebook::$CURL_OPTS[CURLOPT_SSL_VERIFYPEER] = false;
Facebook::$CURL_OPTS[CURLOPT_SSL_VERIFYHOST] = 2;
*/
$facebook = new Facebook(array(
'appId' => 'xxx',
'secret' => 'xxx',
'cookie' => true,
));
//Request params
if(!($facebook->getSession()))
{
header("Location:" . $facebook->getLoginUrl(array('req_perms' => 'publish_stream')));
exit;
}
?>
the problem is at header("Location:" . $facebook->getLoginUrl(array('req_perms' => 'publish_stream')));. When I remove it (including removing the exit), the application can run well. However, when I have it, the application doesn;t show anything. Just blank page. And there is "load resource error from channel.facebook.com" on chrome's developer tools.
Can anybody help me spot what when wrong? I don't understand what went wrong In this code. I have made sure that appId and secret are correct.

This might help you:
load resource error from channel.facebook.com is irrelevant to your problem, I've seen that error plenty of times on the facebook page, it is their problem.
Most likely you are getting a blank screen because there is a PHP error, and you have error_reporting set to 0. Try error_reporting(E_ALL) to see if this is the problem.
Where is this code located, is the header you are sending ahead of other text output? Try placing the code on top of the page.
These are just some basic stuff you can check, hope it helps. Good luck.

I had the same problem (blank page) when trying to redirect (in PHP from the server side) to the login URL generated with the function getLoginUrl of the Facebook PHP SDK.
The problem seems to be related to the fact that the redirection is in the iFrame.
The redirection needs to be for the parent frame and not the canvas frame.
In PHP there is no way to tell the browser to redirect the parent frame soo you have to do it in Javascript:
$loginUrl = $facebook->getLoginUrl(array('req_perms' => 'publish_stream'));
echo '<script type="text/javascript">top.window.location="'.$loginUrl.'";</script>';

Related

Issue : Login with facebook

This is my first time to try integrate my page with facebook login. I using the way shown in http://www.domagojsalopek.com/Details/Create-a-Registration-and-Login-System-using-Facebook-Registration-Plugin/21.
I did exactly the same as in the tutorial. I even downloaded the sample and tried, yet it did not work.
But once I filled in all the detail and clicked the 'Register' button, it went to 'Server Error' page. I found that the error came from the following line in register.php
$facebook = new Facebook(array(
'appId' => FB_ID,
'secret' => FB_SECRET,
));
Note: I had replace my ID and Secret in config.php.
I had the solution already. In the sample code it does not include the facebook.php.

Directing users to facebook login page without asking for permissions

I'm trying to redirect users to the facebook login page then back to my app without the Log In to app page displaying. Is this possible?
I've looked into the "next" parameter which is automatically generated by the PHP-SDK but cannot seem to change it.
Is there anyway to do what I'm looking for? I want to make sure users are logged in in order to check to see if they have authed my app but do not want them to if they are not already.
Thanks for any help!
No, i don't think it is possible to modify the next parameter. The only place where i saw the next parameter(in the PHP SDK) is in the getLogoutUrl() function, as seen in the source of the base_facebook.php. And that parameter defines which url to go, after logout.
For getLoginUrl() in the base_facebook.php file, we have
return $this->getUrl(
'www',
'dialog/oauth',
array_merge(array(
'client_id' => $this->getAppId(),
'redirect_uri' => $currentUrl, // possibly overwritten
'state' => $this->state),
$params));
as the last line in the function, which obviously means that the oauth-dialog will be shown, no matter what you try.
If your app is not on Facebook, then you can go for <fb:login-button>Login with Facebook</fb:login-button> in Javascript, and subsequently follow the example from this url. You'll see there that it's not possible to remove that dialog entirely, even if you don't ask for any permissions.
The Javascript SDK's FB.login() method will also bring up the auth dialog.
Hope this helps.
They have to accept permissions for the first time only. Facebook will never let you to get user's data without their knowledge/accept
If you are using php :
// Create our Application instance.
$facebook = new Facebook(array(
'appId' => $fbconfig['appid'],
'secret' => $fbconfig['secret'],
'cookie' => true
));
//Facebook Authentication part
$user = $facebook->getUser();
if (!$user) {
// user is not logged on --> redirect to the login url
} else {
// user is logged on you can do what you want
}

$facebook->getUser() always returns 0 [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Why is Facebook PHP SDK getUser always returning 0?
Am using the following code to authenticate Facebook uses and redirecting them to login page if not logged in or require permission. Problem is getUser() always returns 0 causing the code to get stuck in a redirecting loop. Any ideas?
include_once("facebook.php");
$app_id = 'xxxxxxxxxxxx';
$app_secret = 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxx';
// initialize facebook
$facebook = new Facebook(array(
'appId' => $app_id,
'secret' => $app_secret,
));
$user = $facebook->getUser();
echo $user;
if ($user) {
try {
$fbme = $facebook->api('/me');
} catch (FacebookApiException $e) {
error_log($e);
$user = null;
}
}
else
{
$loginUrl = $facebook->getLoginUrl(array('scope' => 'email'));
header('Location: '.$loginUrl);
}
After days of searching I found that I just wasn't adding a 'code' key value when using the Facebook API. The system needs it in order to validate
When you use the API function $facebook->getLoginUrl(); it takes you to a login-page that (when you authenticate properly) simply returns you to your own website with a code="x" parameter in the navigation bar. So when instantiating the Facebook Object you simply use a get request to nab this piece of information.
when your page reloads the Facebook API, make sure that one of its key values is
'code' => $_GET['code'],
sorted..
Really angry that I had to figure this out myself.. Look at the getLoginUrl() docs here http://developers.facebook.com/docs/reference/php/facebook-getLoginUrl/ and you'll find that nowhere does it specify what it returns, just that it "authorises" the app, whatever the hell that means.
Also on the main page of the PHP API docs found here http://developers.facebook.com/docs/reference/php/, it states that the minimum number of parameters required are an app secret and an appID. If this is the case, then why does adding the code sort my problem?
If I'm talking nonsense, please respond. I like to know the error of my ways :).
This is not a sdk related problem, SDK (3.1) working well. getUser() and PHP-SDK silently fails if _REQUEST like globals dropping by http server if misconfigured. I was using wrong-configured nginx and after tracing code ~3 hours solved this problem via vhost configuration change.

Facebook App not working tihe facebook.php SDK

Not sure why this isn't working, and was hoping someone might be able to point me in the right direction.
I have a facebook tab app that should display different content depending on whether a user "likes us or not."
require_once('src/facebook.php');
$app_id = "142214XXXXXXXX"; <--this is correct just hidden
$app_secret = "31275c8895XXXXXXXX";
$facebook = new facebook(array(
'appId' => $app_id,
'secret' => $app_secret,
'cookie' => true
));
$signed_request = $facebook->getSignedRequest();
$like_status = $signed_request["page"]["liked"];
//just for testing (nothing shows up, nor does print($signed_request)
echo "the page is ".$page_id;
if(!$like_status){
echo "Please LIKE US to get this offer or check back on Sept 27";
} else {
echo '<img src="https://www2418.ssldomain.com/frightfurnace/SLOSSbogo.jpg"/><BR />
Printable Page';
}
Now, I know that the SDK is installed and working properly since it does work with the example:
http://www.frightfurnace.com/Facebook/example.php
I have also made sure I am using absolute urls in my Facebook App settings.
I am using
http://www.frightfurnace.com/Facebook/coupon.php
Same with SSL.
Any ideas, kinda stymied. All help appreciated.
Well found the answer in case anyone else needs it.
Go to your apps on Facebook
https://developers.facebook.com/apps/
Go to edit settings
On the left click Advanced
signed_request for Canvas: (Make it enabled)
iframe Page Tab: (enabled)
Under Canvas Settings make sure Canvas Type is
iFrame
Seemed to work for me.
Another thing to check: the tab/canvas url must be EXACTLY the same as required. If there is a redirect to another page, then signed request and other values will not be sent. You can check using a browser sniffer, if a call to the page responds with a 300 (301/302 etc) redirect, then you need to change to what it redirects to.
Examples:
https://example.com/ may need to be https://www.example.com/ (add www., or remove www. depending on how server is set up)
www.example.com/ may need to be www.example.com/index.php (add index.php, or the right page).
Check you are using http:// and https:// correctly in the URLs, and that https:// returns a valid page.

facebook login, getuser not working properly

I'm trying to add a facebook login to my site but the problem is that the facebook function "getUser" only works sometimes. Half the time I'll get the user's facebook id from it, but half the times I won't.
Is there a delay somewhere? How can I fix this?
My if facebook_uid set fails and it all ends up in a big mess. :(
Facebooks getSession never fails though. All the above happens when a facebook session is correctly initiated.
Please help!
Here's part of my code:
$facebook = new Facebook(array('appId' => '123','secret' => '789','cookie' => true,));
$facebook_session = $facebook->getSession();
if ($facebook_session) $_SESSION['facebook_uid'] = $facebook->getUser();