Simple Iframe based facebook authentication - facebook

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>");
}
?>

Related

Facebook application : Doesn't redirect to OAuth in an iFrame

I am currently developing a facebook application using the php sdk and the js sdk. (I need both of them)
When a user has not yet authorized the application and loads the canvas page (https://apps.facebook.com/app_name/), nothing happens, he just gets a blank page with the facebook bar on top.
But if he loads the direct url of the application (https://my_app.mydomain.com), it works fine and he is redirected to the OAuth Dialog. If he authorizes the application, he is then redirected to https://apps.facebook.com/app_name/ and the iframe loads correctly.
If the user has already authorized the applications, both links are working (https://apps.facebook.com/app_name/ and https://my_app.mydomain.com).
Here is my authentication code :
<?php
require 'php-sdk/facebook.php';
$facebook = new Facebook(array(
'appId' => '327991330620101',
'secret' => '79asecretcc4300',
'cookie' => true
));
$user = $facebook->getUser();
if ($user) {
try {
$user_profile = $facebook->api('/me');
} catch (FacebookApiException $e) {
$user = null;
}
}
if ($user) {
$logoutUrl = $facebook->getLogoutUrl();
} else {
$params = array(
'scope' => 'publish_stream',
'redirect_uri' => 'https://apps.facebook.com/quiet_quies/'
);
$loginUrl = $facebook->getLoginUrl($params);
header('Location: ' . $loginUrl);
}
Does anybody have an idea about this ? It's really driving me mad...
Thanks for you help.
yes, #CBroe is right, in your php code try replacing header('Location: ' . $loginUrl); line with
echo "<script language=javascript>top.location.href ='".$loginUrl."'</script>";
it should work then.

Facebook application access for the public

Ive created my first facebook app. Basically the canvas/iframe version.
It all seems ok ..except:
I can only access the apps page: (http://apps.facebook.com/hoo_promo/) when im logged in as the administrator user of the app. It should be viewable by everone. Ive submiited it to the appstore...does this matter? is this the issue?
My like-gate doesnt seem to work, even though ive followed several examples. My admin user, who im logged in with, has the page liked, but the code
$signed_request = $facebook->getSignedRequest();
$like_status = $signed_request["page"]["liked"];
for page-like is never set in the signed_request obj.
Any help much appreciated....ive added my current code...maybe this can also help others...
$app_id = "secretblahblahhiddenthis";
$secret = 'secretblahblahhiddenthis';
$canvas_page = "http://www.houseofoak.co.uk/facebook_app/";
$auth_url = "http://www.facebook.com/dialog/oauth?client_id=" . $app_id . "&redirect_uri=" . urlencode($canvas_page);
$facebook = new Facebook(array(
'appId' => $app_id,
'secret' => $secret,
'cookie' => true,
));
$signed_request = $facebook->getSignedRequest();
$like_status = $signed_request["page"]["liked"];
$user = $facebook->getUser();
$page = 'unliked';
$loginUrl = $facebook->getLoginUrl(array('scope' => 'user_likes'));
$logoutUrl = $facebook->getLogoutUrl();
if (($user) && ($like_status)){
$page = 'liked';
}
For your problems:
Check to make sure you aren't in sandbox mode. Visit https://developers.facebook.com/apps/YOUR_APP_ID/advanced to check.
What you're trying to do is no longer allowed. Facebook has deprecated most of the methods that make a like gate possible. See Facebook Platform Policies Section IV. Point 1 says "You must not ... gate content behind ... Facebook social channels...."

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

Using Facebook Object in Multiple PHP Files

I am creating a Facebook Application containing multiple PHP pages. If I reinstantiate Facebook objects in all the files I get an Error Invalid OAuth access token signature.
I have tried a number of alternatives, also the $facebook->getSession() function doesn't work in the new framework and is deprecated.
So I tried to keep the access token in the session and use the same access token in my next PHP file's Facebook object.
On my first page i Have instantiated my App using:
$app_id = "107684706025330";
$app_secret = "__SECRET__";
$my_url = "http://www.sentimentalcalligraphy.in/wp-content/then_n_now/";
$config = array(
'appId' => $app_id,
'secret' => $app_secret,
'cookie' => true,
);
session_start();
$facebook = new Facebook($config);
$access_token = $facebook->getAccessToken();
echo $access_token;
$_SESSION['fob'] = $access_token;
On the next PHP file were I need to use the Facebook object for the second time:
$config = array(
'appId' => '107684706025330',
'secret' => '__SECRET__',
);
$facebook = new Facebook($config);
$access_tocken = $_SESSION['fob'];
echo $access_token;
$facebook->setAccessToken($access_token);
I still get an error that the access token is not valid. How am I supposed to use this. I need to use the following code in my second PHP file:
$friends = $facebook->api('/me/friends','GET');
Thanking you in Advance,
Nasir
I have a some code for Facebook API. I want to share with you.
// App user account and password
$app_id = 'blabla';
$app_secret = 'blabla';
// Create our Application instance (replace this with your appId and secret).
$facebook = new Facebook(array(
'appId' => $app_id,
'secret' => $app_secret,
));
// Get User ID
$user = $facebook->getUser();
// Login or logout url will be needed depending on current user state.
if ($user) {
$logoutUrl = $facebook->getLogoutUrl();
} else {
$loginUrl = $facebook->getLoginUrl();
}
$access_token = $facebook->getAccessToken();
if(isset($_GET["code"])){
// Get our app user feed
$user_profile_feed = $facebook->api('/me');
// Insert user id into users table while user login our app
// We have to control user who inserted our users table after before
$query_is_user = mysql_query("SELECT * FROM `users` WHERE `user_id` = ".$user_profile_feed['id']) or die("hata 1");
$count = mysql_num_rows($query_is_user);
if($count == 0) {
mysql_query("INSERT INTO `users`(`user_id`) VALUES (".$user_profile_feed['id'].")") or die("hata 2");
echo '<script language=Javascript>alert("Uygulamamiza Basariyla Giris Yaptiniz...");</script>';
}
$token_url = "https://graph.facebook.com/oauth/access_token?"
. "client_id=" . $app_id . "&redirect_uri=" . "http://apps.facebook.com/machine_learning/"
. "&client_secret=" . $app_secret . "&code=" . $_GET["code"];
$response = #file_get_contents($token_url);
$params = null;
parse_str($response, $params);
mysql_query("UPDATE users SET access_token='$params[access_token]' WHERE user_id='$user_profile_feed[id]'") or die("hata 4");
}
$url = "https://graph.facebook.com/oauth/authorize?"
."client_id=262609310449368&"
."redirect_uri=http://apps.facebook.com/machine_learning/&scope=read_stream";

iFrame app no longer working

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.