Redirection issue with latest version of php-sdk - facebook

We have used latest version of php-sdk 3
<?php
try{
include_once "connect.php";
}
catch(Exception $o){
echo '<pre>';
print_r($o);
echo '</pre>';
}
if (isset($_GET['code'])){
header("Location: " . $appsurl);
exit;
}
//~~
//
if (isset($_GET['request_ids'])){
//user comes from invitation
//track them if you need
}
$user = null; //facebook user uid
try{
include_once "facebook.php";
}
catch(Exception $o){
echo '<pre>';
print_r($o);
echo '</pre>';
}
// Create our Application instance.
$facebook = new Facebook(array(
'appId' => $fbconfig['appid'],
'secret' => $fbconfig['secret'],
'cookie' => true,
));
//Facebook Authentication part
$uid = $facebook->getUser();
$loginUrl = $facebook->getLoginUrl(
array(
'scope' => 'publish_stream'
)
);
if ($uid) {
try {
// Proceed knowing you have a logged in user who's authenticated.
$user_profile = $facebook->api('/me');
} catch (FacebookApiException $e) {
//you should use error_log($e); instead of printing the info on browser
d($e); // d is a debug function defined at the end of this file
$user = null;
}
}
if (!$uid) {
echo "<script type='text/javascript'>top.location.href = '$loginUrl';</script>";
exit;
}
//get user basic description
$userInfo = $facebook->api("/$uid");
function d($d){
echo '<pre>';
print_r($d);
echo '</pre>';
}
?>
we allow the application( Go to app) and allow post to wall.
After allowing post to wall it will not redirecting the application url($appsurl).It will display like this
https://[hostingurl]/?state=7f76eb2ad732398827215d86f946a690&code=AQBZ2nsbbs3lfBSBwr_HzFpcKIIZlfjAijl9db7wyn1xEIZ4-W7BqQbFtnVZ8UM_4nQ9qIk57msjw6RlLb6VQqGrF3nupBFoEmrypAPmftAsS4ILW9LEJA5gzTMb2VJhwBHHqSpwGHUbh98bNQQQxkX50Ns5kVaffUUylXrIgy6MQcLxU66hQS7qMImHg5Mi2tUrpPqlWbZNQzlooMkv1WAt#_=_
We tried the redirection code in indexpage also.
if (isset($_GET['code'])){
header("Location: " . $appsurl);
exit;
}
but the redirection is not take place.
Could you please help me?

try this:
$loginUrl = $facebook->getLoginUrl(
array(
'scope' => 'publish_stream',
'redirect_uri' => 'https://apps.facebook.com/yourNameSpace'
)
);

Related

Get the facebook accounts value in a particular file

I have used the facebook php sdk and I want to get the value in another file, so I give the redirection path, after login it is going on the page but $user is blank, when I am refreshing the page then I got the values. my code is like below
From where the login page is calling :
$facebook = new Facebook(array('appId' =>FB_APP_ID ,'secret' => FB_APP_SECRET));
$scope='email,user_birthday,user_location,user_work_history,user_about_me,user_hometown';
$parameters=array("redirect_uri"=>"http://www.xyz.com//test.php","scope"=>$scope);
$login_url= $facebook->getLoginUrl($parameters);
and the test.php is like below
$facebook = new Facebook(array(
'appId' => '224402951020359' ,
'secret' => 'd2731260cdd7425dc11eee5f265a3dee'
));
$user = $facebook->getUser();
if ($user) {
try {
// Proceed knowing you have a logged in user who's authenticated.
$user_data = $facebook->api('/me');
} catch (FacebookApiException $e) {
error_log($e);
$user = null;
}
}
print_r($user_data);
How can I get the value in first time.
On your test.php page, put a test for !$user and if true then redirect again to the loginurl... something like the following is what I do in my code to avoid this same problem.
$loginUrl = $facebook->getLoginUrl(
array(
'scope' => 'email,publish_stream,user_birthday,user_location'
)
);
if ($user) {
try {
//get user basic description
$userInfo = $facebook->api("/$user");
$fb_access_token = $facebook->getAccessToken();
} catch (FacebookApiException $e) {
//you should use error_log($e); instead of printing the info on browser
error_log('Sharebox: '.$e); // d is a debug function defined at the end of this file
$user = null;
}
}
if (!$user) {
echo "<script type='text/javascript'>top.location.href = '$loginUrl';</script>";
exit;
}
Hope this helps.

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).

How to validate the Access Token and get new one if it's invalid?

My app is working just fine and everything is OK but when the user logged-out or come back after two hours or change his password or remove the app then try to add it again I get an error like this with the reason of each error :
Fatal error: Uncaught OAuthException: Error validating access token: The session is invalid because the user logged out. thrown in /home/user/public_html/new_fb_app/src/base_facebook.php on line 1028
Also, if I tried to refresh after this error came up the app will work just fine. So I guess it has something to do with the app tried to work with the last access token (which was invalid).
This is my code :
<?php
$user = null; //facebook user uid
try{
include_once "src/facebook.php";
}
catch(Exception $o){
echo '<pre>';
print_r($o);
echo '</pre>';
}
// Create our Application instance (replace this with your appId and secret).
$facebook = new Facebook(array(
'appId' => 'my appId',
'secret' => 'my app secret',
'fileUpload' => true
));
// Get User ID
$user = $facebook->getUser();
$loginUrl = $facebook->getLoginUrl(
array(
'scope' => 'read_stream, publish_stream, photo_upload, user_photos'
)
);
if ($user) {
try {
// Proceed knowing you have a logged in user who's authenticated.
$access_token = $facebook->getAccessToken();
} catch (FacebookApiException $e) {
//you should use error_log($e); instead of printing the info on browser
d($e); // d is a debug function defined at the end of this file
$user = null;
}
}
if (!$user) {
echo "<script type='text/javascript'>top.location.href = '$loginUrl';</script>";
exit;
}
function d($d){
echo '<pre>';
print_r($d);
echo '</pre>';
}
?>
Empty the cookies or sessions when the user logs out?

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;
}
}