Facebook permission request, endless redirect loop - facebook

I have this code:
require 'facebook.php';
$facebook = new Facebook(array(
'appId' => 'xxxxxxx',
'secret' => 'yyyyyyy',
'baseUrl' => 'http://xxx.yyy.zz/',
'appBaseUrl' => 'http://apps.facebook.com/xxxxxx/',
'fileUpload' => 'true',
));
$user = $facebook->getUser();
$params = array(
scope => 'publish_stream,user_photos',
redirect_uri => 'http://www.facebook.com/xxxxxx?sk=app_123456789'
);
if ($user){
....
}
$loginUrl = $facebook->getLoginUrl($params);
if ($user){
// nothing
}else{
echo "<script type=\"text/javascript\">top.location.href = \"".$loginUrl."\";</script>";
}
My problem is, that it gets into an endless loop... reloads the page over and over.
Can someone help me to fix this?
Thank you very much!

I think the problem is your redirect_uri. Try to define the real path to your url (www.yourdomain.com/app/index.php) instead, since Facebook will send a POST (including a code) to that url and won't pass it with the app data or signed request to your iFrame application.

The problem was that the main logic was not int the index.php. After i put everything back into that, the redirection stopped.

Related

Trying to post to facebook page with app but get "The user hasn't authorized the application to perform this action"

I am trying to write a script that will post an update to a facebook page timeline. I created an app and created a key for the app. I am using the code below:
require_once HOME_PATH . '/include/facebook/facebook.php';
$facebook = new Facebook(array(
'appId' => FACEBOOK_APP_ID,
'secret' => FACEBOOK_APP_SECRET,
'cookie' => true,
'scope' => 'publish_stream',
));
$user_id = $facebook->getUser();
$access_token = $facebook->getAccessToken();
$attachment = array(
'access_token' => $access_token,
'message' => 'this is my message',
'name' => 'name',
'link' => ROOT_URL . $blog_data['url'] . '.htm',
'caption' => $blog_data['title'],
'description' => $blog_data['title'],
);
if ($image = get_first_image($blog_data['body'])) {
$attachment['picture'] = $image;
}
$facebook->api(FACEBOOK_BLOG_PAGE . '/feed', 'post', $attachment);
i think this key is connected to the app and not to my user, so it sounds like i would have to grant the permission within facebook, but i have seen some other posts telling me that i need to do this in code. The examples aren't very clear.
I know similar questions have been asked but i haven't seen any clear answers yet. Can anyone please clarify this?
If you want to get user_id by this piece of code: $user_id = $facebook->getUser();
The first thing you need, is to login an user through a URL returned by $facebook->getLoginUrl(array('scope' => array('perm', 'perm2'))).
Also, notice, where I put permissions, which I require. Not into the Facebook class constructor, but into the method getLoginUrl.
And lastly, for posting into a page, you need publish_actions permission (not publish_stream).

Endless redirect loop in Facebook canvas application

I have tried to search this, but have found no answers. I have [had] a working application on Facebook up until yesterday, then all hell broke loose and it simply doesn't come up. Please keep in mind, nothing changed on my end. The application has been running for over a year with no problems.
Yesterday, I accessed the application like normal and got an additional permission request for access to my friends list. Strange, again because I have made no changes to the code or the configuration on FB. I accepted the request and now instead of the application coming up, it simply goes into an endless oauth login redirect loop. In fact it doesn't even look like it ever calls my application url as I cannot debug it (it never breaks on the code)
I guess my question is, does anyone know of something changing recently on FB to cause this?
The application can be found at: https://apps.facebook.com/myworkoutlog
And as I wrote this, the app came up, but after closing the browser and starting again, back into the endless loop. Any help would be great. If you need additional information, please just ask.
Edit:
I finally got it into debug and it breaks when I try to get an access_token using a verification code: (OAuthException - #100) Invalid verification code format.
Try this it works for me ;)
include_once "facebook.php";
$facebook = new Facebook(array(
'appId' => 'xxxxxxxxxxxxxxx',
'secret' => 'xxxxxxxxxxxxxxx',
'cookie' => true
));
$fb_user = $facebook->getUser();
if($fb_user){
if(isset($_GET['code']) && isset($fb_user)){
$fbAppNs = 'appOnFacebookNameSpace';
$facebookAppUrl = 'https://apps.facebook.com/'.$fbAppNs;
header("Location: " . $facebookAppUrl);
exit;
}
try{
$user_profile = $facebook->api('/me');
$smarty->assign('name', $user_profile['first_name'].' '.$user_profile['last_name']);
$smarty->assign('email', $user_profile['email']);
}catch(FacebookApiException $e){
error_log($e);
$user = NULL;
}
}else{
$loginData = array(
'scope' => 'email,publish_stream',
'canvas' => 1,
'fbconnect' => 0,
'redirect_uri' => $siteConfigs['WEBSITE_URL'].'fblead/'
);
$loginUrl = $facebook->getLoginUrl($loginData);
echo "<script type='text/javascript'>top.location.href = '$loginUrl';</script>";
exit;
}

Facebook redirect loop when running locally

I'm working on a local Facebook project. But when I go to localhost, he starts redirecting to localhost/?code=xxxx everytime (xxxx is very long random characters).
My code is:
require_once('sdk/src/facebook.php');
$facebook = new Facebook(array(
'appId' => 'MY_APP_ID',
'secret' => 'MY_APP_SECRET',
'sharedSession' => true,
'trustForwarded' => true,
'cookie' => true
));
$user_id = $facebook->getUser();
if ($user_id) {
try {
// Fetch the viewer's basic information
$basic = $facebook->api('/me','GET');
} catch (FacebookApiException $e) {
if (!$facebook->getUser()) {
//header('Location: '. AppInfo::getUrl($_SERVER['REQUEST_URI']));
$user_id = null;
}
}
}
if(!$user_id) {
$loginUrl = $facebook->getLoginUrl(array(
'scope' => 'email,publish_actions'
));
echo "<script type='text/javascript'>top.location.href = '$loginUrl';</script>";
exit();
}
...
My app settings:
Thanks in advance.
The likely issue is that your app is returning 0 for $user_id even after the user has logged in, causing the JavaScript redirect to happen again, causing it to loop forever.
The case of the issue may be one of many things, but make sure the code GET variable isn't being stripped out of the URL by a .htaccess file or other means. Secondly, take a look at this modification to the base_facebook.php file to change the way the code is checked.

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
}

facebook oauth url problems

I have the following code, which are working partly
<?php
require_once 'facebook.php';
$app_id = 'MY_APP_ID';
$app_secret = 'MY_APP_SECRET';
$app_url = 'http://mysite.com/index.php/';
$scope = 'user_about_me';
// Init the Facebook SDK
$facebook = new Facebook(array(
'appId' => $app_id,
'secret' => $app_secret,
'cookie' => true
));
// Get the current user
$user = $facebook->getUser();
// If the user has not installed the app, redirect them to the Auth Dialog
if (!$user) {
$loginUrl = $facebook->getLoginUrl(array(
'scope' => $scope,
'redirect_uri' => $app_url,
));
print('<script> top.location.href=\'' . $loginUrl . '\'</script>');
}
$user_profile = $facebook->api('/me');
echo "id: " . $user_profile['id'];
?>
I get the correct id printed out on this page, but on several other pages i go into the if(!$user).
But there is an error in the oauth url:
API Error Code: 191
API Error Description: The specified URL is not owned by the application
Error Message: Invalid redirect_uri: Given URL is not allowed by the Application configuration.
I have found similar problems here on stackoverflow, but can't seem to fix it :/
Under my settings I have the same url and $app_url
Website with facebook -> site url -> https://www.facebook.com/pages/myappname/373671679372127/
EDIT: I edit my app_url, so it is now the same as my canvas url under app settings: http://mysite.com/index.php/
Now I don't get an error. I just get a blank window :(
EDIT: Importent to clear cookies once a while when testing. Else some very random errors somewhere
Check if you set the correct URL for "Website with Facebook Login" in the dev settings. This cannot be a page on facebook.com, it has to be a Link to YOUR domain.
Also, i would enclose the last 2 rows of your code in an "else" block. And the JavaScript code is wrong. This would be correct:
print('<script> top.location.href="' . $loginUrl . '";</script>');
(easier to understand with double quotes, you missed a semicolon. even if it´s not always needed, you better use it)
Try this code:
$user = $facebook->getUser();
// If the user has not installed the app, redirect them to the Auth Dialog
if (!$user) {
$loginUrl = $facebook->getLoginUrl(array(
'scope' => $scope,
'redirect_uri' => $app_url,
));
print('<script> top.location.href=\'' . $loginUrl . '\';</script>');
}
else {
echo 'User ID: ' . $user;
}
Also, get rid of the Slash after "index.php". That´s not i folder (i hope/guess).