debugging the infinite loop after authorization with javascript sdk - facebook

As per usual, FB has me pulling out my hair.
I've been able to test my app in IE9, but when using Firefox, after a user authorizes the canvas app it goes into a redirect loop, adding state and code variables to the URL.
I'm using the javascript and php sdk with this code:
$facebook = new Facebook(array(
'appId' => $app_id,
'secret' => $application_secret,
'cookie' => true, // enable optional cookie support
));
$user = $facebook->getUser();
if(!($user))
{
echo"<script> top.location.href='" . $facebook->getLoginUrl(array('redirect_uri'
=> $fbconfig['appBaseUrl'],
'scope' => 'manage_notifications,publish_stream,publish_actions'
)) . "'</script>";
exit();
}
I read about adding this:
if (window.location.hash =='#=') window.location.hash=''; but it didn't seem to do anything.

I had the same issue on my latest app.
Solved it by using the code above in the < head > section.
Don't forget to upgrade your PHP SDK to the latest version.
<?
$facebook = new Facebook(array(
'appId' => 'XXX',
'secret' => 'YYY',
));
$user = $facebook->getUser();
if ($user) {
$logoutUrl = $facebook->getLogoutUrl();
} else {
$loginUrl = $facebook->getLoginUrl(array(
'scope' => 'user_likes'
));
echo "<script type='text/javascript'>top.location.href = '$loginUrl';</script>";
exit;
}
?>

Related

Strange behavior in Facebook login with PHP SDK

I'm learning about Facebook apps programming and I've wroted the next code that works OK, but if I comment the indicated line in the "do-login.php" file, then when I call getUser() on "index.php" will return 0.
I'm curious about that and I think maybe I'am doing something bad.
At this point, I think it wouldn't be necessary to create a Facebook object in the "do-login.php" file.
Finally, note that the link on "index.php" file points to the Heroku URL, and the link on "do-login.php" file points to the "apps.facebook.com" URL... this is the only way I found to the code works.
Any advice, please ? I'm confussed with this and I spent a day and half to get working this
Thanks!
index.php
<?php
require_once("facebook.php");
$facebook = new Facebook(array(
'appId' => THE_APP_ID,
'secret' => THE_APP_SECRET,
'fileUpload' => false,
'cookie' => true,
'trustForwarded' => true,
'allowSignedRequest' => false
));
$user = $facebook->getUser();
if( $user )
{
try
{
$user_info = $facebook->api(THE_USER_ID?fields=id,name,first_name,last_name,email&access_token='.$facebook->getAccessToken());
echo "getUser(): ".$user."<br/>";
echo "<b>Name</b>: ".$user_info['name'];
}
catch( FacebookApiException $e )
{
echo $e->getMessage();
}
}
else
{
$facebook_login_url = $facebook->getLoginUrl(array(
'scope' => 'email, read_stream, user_interests, user_likes, user_location, user_status',
'redirect_uri' => 'https://MY-APP.herokuapp.com/do-login.php'
));
echo "<p><a href='".$facebook_login_url."' target='_top'>Login</a></p>";
}
?>
do-login.php
<?php
require_once("facebook.php");
if( isset($_GET['code']) )
{
echo "<p><b>¡¡ Se autorizó la app !!</b></p>";
$facebook = new Facebook(array(
'appId' => THE_APP_ID,
'secret' => THE_APP_SECRET,
'fileUpload' => false,
'cookie' => true,
'trustForwarded' => true,
'allowSignedRequest' => false
));
// PROBLEM IS that if I comment this line, getUser() will return 0 when called in index.php
echo $facebook->getUser();
echo "<p><a href='https://apps.facebook.com/MY-APP/index.php' >Go back</a></p>";
}
else
{
echo "Error: ".$_GET['error']."<br />";
echo "Description: ".$_GET['error_description'];
}
?>

facebook app wont ask for extended permissions when validating

Have been trying for weeks, but my App won't ask for extended permissions, obviously that means that it wont POST to the timeline afterwards either.
require_once('/wp-content/php-sdk/src/facebook.php');
$facebook = new Facebook(array(
'appId' => 'xxxxxx',
'secret' => 'xxxxxxx',
));
$user = $facebook->getUser();
if ($userID) {
try {
// Proceed knowing you have a logged in user who's authenticated.
$ret_obj = $facebook->api('/me/feed', 'POST',
array(
'link' => 'www.theaandrdepartment.com/raw',
'message' => 'I just helped an Aussie band make it to
radio play! By voting, you can too.'
));
echo '<pre>Post ID: ' . $ret_obj['id'] . '</pre>';
} catch (FacebookApiException $e) {
error_log($e);
$user = null;
}
} else { $login_url = $facebook->getLoginUrl(array(
'scope' => 'publish_stream'
));
header("Location: ".$login_url);
}
If anyone could help that would amazing.
I copied most of this code straight from the Facebook example, but to no avail.

Why can't I redirect to Facebook's getLoginUrl()?

<?php
$fb = new Facebook(array(
'appId' => $fb_config['app_id'],
'secret' => $fb_config['secret'],
));
$params = array(
'scope' => 'email, read_stream, user_interests, user_likes, user_location, user_status',
'redirect_uri' => 'http://myurl/facebook_connect',
);
$fb_login_url = $fb->getLoginUrl($params);
$this->redirect($fb_login_url);
$user = $fb->getUser();
print_r($user); // returns 0
?>
In the example above, if I redirect to the getLoginUrl result, the getUser() method returns zero.
Is there a reason why this is happening?
Replace Facebook PHP SDK path, appId, secret and redirect_uri with your app settings and Try:
require_once dirname(__FILE__) . '/facebook/facebook.php'; // You need Facebook PHP SDK
$facebook = new Facebook(array(
'appId' => $fb_config['app_id'], // Your Facebook app id
'secret' => $fb_config['secret'], // Your Facebook app secret
'cookie' => true
));
$fb_user_id = $facebook->getUser();
if ($fb_user_id) {
try {
$fb_user_profile = $facebook->api($fb_user_id);
var_dump($fb_user_profile);
} catch (FacebookApiException $e) {
error_log($e);
}
} else {
$params = array(
'scope' => 'email, read_stream, user_interests, user_likes, user_location, user_status',
'redirect_uri' => 'http://myurl/facebook_connect', // Replace with your app url
);
$facebook_login_url = $facebook->getLoginUrl($params);
echo '<script>top.location="' . $facebook_login_url . '";</script>';
exit();
}

Get birthdays of all facebook friends in php

My Code
require 'src/facebook.php';
$app_id = 'My App Id';
$app_secret = 'My App Secret Id';
$facebook = new Facebook(array(
'appId' => $app_id,
'secret' => $app_secret,
'cookie' => true,
));
$user = $facebook->getUser();
if ($user) {
$user_albums = $facebook->api('/me/friends?fields=id,name,birthday');
}
if ($user) {
$params = array( 'next' => 'http://localhost/friends_bday/logout.php?logout=1' );
$logoutUrl = $facebook->getLogoutUrl($params);
} else {
$loginUrl = $facebook->getLoginUrl(array(
'scope' => 'user_photos'
));
}
What I require
The above code only provides friends id and name but not birthdays. I have done some researching and was unsuccessful in finding a solution.
You probably need to request permission of the user to access the birthday information of their friends.
The Facebook permission you are looking for is 'friends_birthday'.
Request the user their access token with the above permission and set the access token.
$facebook = new Facebook(array(
'appId' => $app_id,
'secret' => $app_secret,
'cookie' => true,
)
);
$facebook->setAccessToken($userAccessToken);
$user = $facebook->getUser();
if ($user) {
$user_albums = $facebook->api('/me/friends?fields=id,name,birthday');
}
if ($user) {
$params = array( 'next' => 'http://localhost/friends_bday/logout.php?logout=1' );
$logoutUrl = $facebook->getLogoutUrl($params);
} else {
$loginUrl = $facebook->getLoginUrl(array(
'scope' => 'user_photos, friends_birthday'
));
}
Actually I think you just have to add friends_birthday to the scope.

Facebook - my app permissions are not displayed in login (php)

I am using php SDK example from github:
$user_id = $facebook->getUser();
As far as I understand this should open the facebook login dialog with the permissions I defined for the app, but I only see the basic permissions.
The option to check authenticated referrals is no longer available on the facebook app settings form.
Do I have to somehow request the permissions in the code itself?
You need code, to make permission window to pop out.
Something like this shold help:
Do you check on user?
// Init the Facebook SDK
$facebook = new Facebook(array(
'appId' => $app_id,
'secret' => $app_secret,
'cookie' => true
));
// Get the current user
$user = $facebook->getUser();
if (!$user) {
$loginUrl = $facebook->getLoginUrl(array(
'scope' => $scope,
'redirect_uri' => $app_url,
));
echo "<script type='text/javascript'>top.location.href = '$loginUrl';</script>";
}
else{
$user_profile = $facebook->api('/me');
// STUFF
}