iFrame app no longer working - facebook

I have a simple Facebook App that used to work in the old API. It's an iFrame app and I can't get the user session with the new API no matter what I try. Here is the code I have now.
error_reporting(E_ALL);
require 'facebook.php';
$facebook = new Facebook(array(
'appId' => 'xxxx',
'secret' => 'xxxx',
'cookie' => true,
));
$session = $facebook->getSession();
if ( !$session )
{
//echo "Failed. No session.<br />";
$url = $facebook->getLoginUrl(array(
'canvas' => 1,
'fbconnect' => 0,
'next'=>'http://www.facebook.com',
'cancel_url' => 'http://www.facebook.com'
));
//echo "<script type='text/javascript'>top.location.href = '$url';</script>";
echo "<a href='".$url."' target='_blank'>First time using this App? Click here to connect it to your Facebook.</a>";
}
else {
try {
$uid = $facebook->getUser();
$me = $facebook->api('/me');
$updated = date("l, F j, Y", strtotime($me['updated_time']));
echo "Hello " . $me['name'] . "<br />";
echo "You last updated your profile on " . $updated;
} catch (FacebookApiException $e) {
echo "Error:" . print_r($e, true);
}
}
I've tried various things but $session is never returned (after clicking the link). I am using the correct appID/secret/facebook.php but it always falls into the !$session if path. What am I doing wrong?

Use new version of php-sdk (3.1.1) :)
Your code uses old sdk (in new there is no getSession method) and you probably have migration setting "OAuth Migration" set to true.

Related

Facebook. Fatal error: Uncaught OAuthException: An active access token must be used to query information about the current user

with my app's administrator acount on facebook my app work normally, but with other account I get error: Fatal error: Uncaught OAuthException: An active access token must be used to query information about the current user.
I had this problem before with other app (publish text on the user's wall), but fixed after i added
$user = $facebook->getUser(); What's wrong here? I have added offline_access permission... Help me, please if you can, Thank you very much.
<?php
require_once('images/Facebook.php');
$facebook = new Facebook(array(
'appId' => '456080124457246',
'secret' => 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
));
$user_profile = $facebook->api('/me','GET');
$accessToken = $facebook->getAccessToken();
# Get User ID
$user = $facebook->getUser();
$facebook->setAccessToken($accessToken);
$facebook->api(456080124457246);
if ($user) {
try {
# Photo Caption
$photoCaption = $user_profile['name'] . ' patarimų plaukams sužinojo čia http://goo.gl/otwhf';
# Absolute Path to your image.
$imageUrl = 'http://padekime.wu.lt/plaukai/images/PlaukaiNeuzvedus.jpg'; // Example URL
# Post Data for Photos API
$post_data = array(
'message' => $photoCaption,
'url' => $imageUrl
);
$apiResponse = $facebook->api('/me/photos', 'POST', $post_data);
} catch (FacebookApiException $e) {
error_log($e);
}
} else {
$loginUrl = $facebook->getLoginUrl( array(
'scope' => 'publish_stream,photo_upload'
));
echo("<script>top.location.href = '" . $loginUrl . "';</script>");
}
?>
In this case it's not even getting to your $facebook->getUser() call -- it's throwing an exception as soon as it reaches this line:
$user_profile = $facebook->api('/me','GET');
$facebook->api() is a bit of a tricky thing to work with because it throws an exception immediately if it doesn't know who "/me" is...even if you try to fix it later.
The trick, I think, is to wrap the entire thing in a try...catch block. Like so:
<?php
require_once('images/facebook.php');
$facebook = new Facebook(array(
'appId' => '456080124457246',
'secret' => 'xxxxx',
));
try {
$user_profile = $facebook->api('/me','GET');
# Get User ID
$user = $facebook->getUser();
if ($user) {
try {
# Photo Caption
$photoCaption = $user_profile['name'] . ' patarimų plaukams sužinojo čia http://goo.gl/otwhf';
# Absolute Path to your image.
$imageUrl = 'http://padekime.wu.lt/plaukai/images/PlaukaiNeuzvedus.jpg'; // Example URL
# Post Data for Photos API
$post_data = array(
'message' => $photoCaption,
'url' => $imageUrl
);
$apiResponse = $facebook->api('/me/photos', 'POST', $post_data);
} catch (FacebookApiException $e) {
error_log($e);
}
} else {
$loginUrl = $facebook->getLoginUrl( array(
'scope' => 'publish_stream,photo_upload'
));
echo("<script>top.location.href = '" . $loginUrl . "';</script>");
}
} catch (Exception $e) {
$loginUrl = $facebook->getLoginUrl( array(
'scope' => 'publish_stream,photo_upload'
));
echo("<script>top.location.href = '" . $loginUrl . "';</script>");
}
?>
That'll redirect the user to the login url pretty much immediately, then come back with everything in tow. You don't have to set the accessToken with this setup.
This may actually unnecessarily repeat some functionality, but hopefully it's something to start with.
By the way, for what it's worth the offline_access permission is being phased out.
I stopped using "me" keyword to get the logged in user's profile.
instead of $facebook->api('/me','GET'), I changed to $facebook->api('/the facebook ID of the user','GET');
this reduce the need to do second try catch
If you do
if (!empty($user)) {}
That should help...

Facebook API sometimes doesn't load

I'm having problem when using
$facebook->api('/me','GET');
When loading the page for the first time, nothing happen. I have to reload the page again to make the script work. Not sure where the problem is.
require_once('src/facebook.php');
$app_id = "APP_ID";
$app_secret = "APP_SECRET";
// Init facebook api.
$facebook = new Facebook(array(
'appId' => $app_id,
'secret' => $app_secret,
'cookie' => true,
));
//Facebook Authentication part
$user_id = $facebook->getUser();
$loginUrl = $facebook->getLoginUrl(
array(
'scope' => 'publish_stream, user_likes'
)
);
if (!$user_id) {
echo "<script type='text/javascript'>top.location.href = '$loginUrl';</script>";
exit;
}
$user_profile = $facebook->api('/me','GET');
$user_gender = $user_profile['gender'];
if($user_gender == 'male'){
echo "you are male";
} else {
if ($user_gender == 'female'){
echo "you are female";
} else{
echo "gender not specified";
}
}
I tried take the $facebook->api('/me','GET'); out and echo $user_id, for example, it works.

api call only delivers "1" instead of user data

if i use this peace of code:
$facebook = new Facebook(array(
'appId' => APP_ID,
'secret' => APP_SECRET,
'cookie' => true,
));
?>
<html>
<head></head>
<body>
<?
$user = $facebook->getUser();
if ($user) {
try {
// Proceed knowing you have a logged in user who's authenticated.
$userData = $facebook->api("/me");
echo "Name: " . $userData['name'];
echo "<br>userData: " . print_r($userData);
} catch (FacebookApiException $e) {
error_log($e);
$user = null;
}
}
// If a user haven't been loged in, so the user have to authorize
if (!$user) {
$loginUrl = $facebook->getLoginUrl(
array(
'scope' => 'publish_stream',
'redirect_uri' => APP_BASEHREF
)
);
echo("<script> top.location.href='" . $loginUrl . "'</script>");
}
i only get this responce:
Name: userData: 1
i don't know the issue. The user is correctly logged in.
i also try it with some test users with the same problem. it seems that the api call does not work correctly. but why.
please can you help me?
still no solution, can anybody help me?
thx
You need to do print_r($userData, true).

Simple Iframe based facebook authentication

I wrote a facebook application but authentication is not working properly. Despite of googling several hours it did not solve.
please please write the script which authenticates the user properly and if the user clicks the allow button simply display the text "Welcome" assuming that i have written all basic suff aas..
require 'sdk/src/facebook.php';
$app_id = 'MY APP ID';
$canvas_page = "MY CANVAS URL";
// Create our Application instance (replace this with your appId and secret).
$auth_url = "http://www.facebook.com/dialog/oauth?client_id="
. $app_id . "&redirect_uri=" . urlencode($canvas_page) ."&scope=email,read_stream&installed=1";
$facebook = new Facebook(array(
'appId' => $app_id ,
'secret' => MY APP SECRET,
'cookie' => true,
));
///... your code to solve !
Nobody provides the ultimate solution: Finally I solved it by myself.
what I do is. Facebook sends an "installed=1" parameter with the url when the user allows the application. SO I checked this parameter and redirected again to the application. I m extremely happy on finding extremely nice and efficient solution. Nobody answered me any votable answer.
if(isset($_REQUEST['installed']))
echo("<script>top.href.location='http://apps.facebook/myapp' </script>");
It seems that you are mixing the examples from the documentations and the PHP-SDK, dropping the example from the PHP-SDK should work.
Following the code in this answer:
<?php
require '../src/facebook.php';
$facebook = new Facebook(array(
'appId' => 'XXXXXXX',
'secret' => 'XXXXXXXX',
'cookie' => true,
));
$session = $facebook->getSession();
$loginUrl = $facebook->getLoginUrl();
$me = null;
if ($session) {
try {
$uid = $facebook->getUser();
$me = $facebook->api('/me');
echo "Welcome User: " . $me['name'] . "<br />";
} catch (FacebookApiException $e) {
error_log($e);
}
} else {
echo("<script> top.location.href='" . $loginUrl . "'</script>");
}
?>

How do I get Facebook Application Login to work

I just started making an application for Facebook, however I ran into problem early on. The first step I want people to do is to give permission to access their profile. All over the web are examples of how to do this with:
$user_id = $facebook->require_login();
However, this is the way it works using the Old PHP API. I have downloaded and installed the new one in my application folder and it is not working anymore.
My question is (and i really have been searching for an answer for a long time) what is the code to do this with the new API?
(and related question: is it better to use the old API, or learn to work with the new one when I am just starting making apps right now)
I have this code now;
<?php
// Awesome Facebook Application
//
// Name: -
//
require_once 'facebook-php-sdk/src/facebook.php';
$app_id = "-";
$app_secret = "-";
// Create our Application instance.
$facebook = new Facebook(array(
'appId' => $app_id,
'secret' => $app_secret,
'cookie' => true
));
$session = $facebook->getSession();
$loginUrl = $facebook->getLoginUrl(
array(
'canvas' => 1,
'fbconnect' => 0,
'req_perms' => 'email,publish_stream,status_update,user_birthday, user_location,user_work_history'
)
);
$fbme = null;
if (!$session) {
echo "<script type='text/javascript'>top.location.href = '$loginUrl';</script>";
exit;
}
else {
try {
$uid = $facebook->getUser();
$fbme = $facebook->api('/me');
} catch (FacebookApiException $e) {
echo "<script type='text/javascript'>top.location.href = '$loginUrl';</script>";
exit;
}
}
echo "<p>hello, <fb:name uid=\"$user_id\" useyou=\"false\" />!</p>";
?>
Download and use new code from github.
How to get user permission?
$loginUrl = $facebook->getLoginUrl(array(
'req_perms' => 'email,user_birthday,publish_stream,sms,status_update,user_location'
));
Or
<fb:login-button perms="email,user_birthday,publish_stream,sms,status_update,user_location"></fb:login-button>
Example how to login
Update
$session = $facebook->getSession();
$loginUrl = $facebook->getLoginUrl(
array(
'canvas' => 1,
'fbconnect' => 0,
'req_perms' => 'email,publish_stream,status_update,user_birthday, user_location,user_work_history'
)
);
$fbme = null;
if (!$session) {
echo "<script type='text/javascript'>top.location.href = '$loginUrl';</script>";
exit;
}
else {
try {
$uid = $facebook->getUser();
$fbme = $facebook->api('/me');
} catch (FacebookApiException $e) {
echo "<script type='text/javascript'>top.location.href = '$loginUrl';</script>";
exit;
}
}