Facebook application instance creation not working for every users - facebook

I have the following issue that drives me crazy. I use the sample code published with the Facebook PHP SDK (https://github.com/facebook/php-sdk/) for identifying a user. It works fine for most of my test users but for some users, the instance creation does not work. I’ve tried with the same machine and different machines, it does not change anything. Each user is correctly set as test user in my dev account.
What could be the cause?
Many thanks in advance.
require '../lib/facebook.php';
// Create our Application instance (replace this with your appId and secret).
$facebook = new Facebook(array(
'appId' => 'XXXXXXXXXX',
'secret' => 'XXXXXXXXXXX',
));
// Get User ID
$user = $facebook->getUser();
if ($user) {
try {
// Proceed knowing you have a logged in user who's authenticated.
$user_profile = $facebook->api('/me');
} catch (FacebookApiException $e) {
error_log($e);
$user = null;
}
}
// Login or logout url will be needed depending on current user state.
if ($user) {
$logoutUrl = $facebook->getLogoutUrl();
} else {
$loginUrl = $facebook->getLoginUrl(array(
'scope' => 'publish_stream, publish_actions',
'redirect_uri' => 'http://apps.facebook.com/xxxxx/'
));
}
?>
<!doctype html>
<html xmlns:fb="http://www.facebook.com/2008/fbml">
<head>
<title>php-sdk</title>
</head>
<body>
<h1>php-sdk</h1>
<?php if ($user): ?>
Logout
<?php else: ?>
<div>
Login using OAuth 2.0 handled by the PHP SDK:
Login with Facebook
</div>
<?php endif ?>
<h3>PHP Session</h3>
<pre><?php print_r($_SESSION); ?></pre>
<?php if ($user): ?>
<h3>You</h3>
<img src="https://graph.facebook.com/<?php echo $user; ?>/picture">
<h3>Your User Object (/me)</h3>
<pre><?php print_r($user_profile); ?></pre>
<?php else: ?>
<strong><em>You are not Connected.</em></strong>
<?php endif ?>
</body>
</html>

I finally found the answer. The problem was not related to the code. It was due to the fact that the test users bypassed the notification requesting to accept the invitation and instead entered directly the app url.
You have to delete the test user and recreate it. Then the test user will recieve a new notification and will then have to accept it.

Related

Facebook login - php sdk - prob

I am in a total mess with facebook-login. The code is given below. You can see the output at http://beta.jokesnfunnypics.com/login. In the scope, I have given as email but when I click the link and is redirected to facebook, it says that the app asks for only the basic permissions. Moreover, I have mentioned the redirect_uri as http://beta.jokesnfunnypics.com/add but I am instead redirected to the same page only. More over again, after getting the permission, no info is being displayed at the page also. Please help me. I am ready to give a few dollars to anyone who gives me the correct code.
<?php
//Application Configurations
$app_id = "XXXXXXXXXX";
$app_secret = "XXXXXXXXXXXXX";
$site_url = "http://www.beta.jokesnfunnypics.com/add";
try{
include_once "src/facebook.php";
}catch(Exception $e){
error_log($e);
}
// Create our application instance
$facebook = new Facebook(array(
'appId' => $app_id,
'secret' => $app_secret,
));
// Get User ID
$user = $facebook->getUser();
if($user){
try{
// Proceed knowing you have a logged in user who's authenticated.
$user_profile = $facebook->api('/me');
}catch(FacebookApiException $e){
error_log($e);
$user = NULL;
}
}
if($user){
// Get logout URL
$logoutUrl = $facebook->getLogoutUrl();
}else{
// Get login URL
$loginUrl = $facebook->getLoginUrl(array(
'scope' => 'email',
'redirect_uri' => $site_url,
));
}
if ($user) {
try {
// Proceed knowing you have a logged in user who's authenticated.
$user_profile = $facebook->api('/me');
} catch (FacebookApiException $e) {
error_log($e);
$user = null;
}
}
// Login or logout url will be needed depending on current user state.
if ($user) {
$logoutUrl = $facebook->getLogoutUrl();
} else {
$loginUrl = $facebook->getLoginUrl();
}
?>
<!doctype html>
<html xmlns:fb="http://www.facebook.com/2008/fbml">
<head>
<title>php-sdk</title>
</head>
<body>
<h1>php-sdk</h1>
<?php if ($user): ?>
Logout
<?php else: ?>
<div>
Login using OAuth 2.0 handled by the PHP SDK:
Login with Facebook
</div>
<?php endif ?>
<h3>PHP Session</h3>
<pre><?php print_r($_SESSION); ?></pre>
<?php if ($user): ?>
<h3>You</h3>
$_SESSION['user']="abc;
<img src="https://graph.facebook.com/<?php echo $user; ?>/picture">
<h3>Your User Object (/me)</h3>
<pre><?php print_r($user_profile); ?></pre>
<?php else: ?>
<strong><em>You are not Connected.</em></strong>
<?php endif ?>
<h3>Public profile of Naitik</h3>
<img src="https://graph.facebook.com/naitik/picture">
<?php echo $naitik['name']; ?>
</body>
</html>
From the code, $loginUrl variable is defined 2 times, scope and redirect_uri is missing in the second time . try to remove those lines from your code
<?php
$app_id = "XXXXXXXXXX";
$app_secret = "XXXXXXXXXXXXX";
$site_url = "http://www.beta.jokesnfunnypics.com/add";
try{
include_once "src/facebook.php";
}catch(Exception $e){
error_log($e);
}
// Create our application instance
$facebook = new Facebook(array(
'appId' => $app_id,
'secret' => $app_secret,
));
// Get User ID
$user = $facebook->getUser();
if($user){
try{
// Proceed knowing you have a logged in user who's authenticated.
$user_profile = $facebook->api('/me');
}catch(FacebookApiException $e){
error_log($e);
$user = NULL;
}
}
if($user){
// Get logout URL
$logoutUrl = $facebook->getLogoutUrl();
}else{
// Get login URL
$loginUrl = $facebook->getLoginUrl(array(
'scope' => 'email',
'redirect_uri' => $site_url,
));
}
?>
<!doctype html>
<html xmlns:fb="http://www.facebook.com/2008/fbml">
<head>
<title>php-sdk</title>
</head>
<body>
<h1>php-sdk</h1>
<?php if ($user): ?>
Logout
<?php else: ?>
<div>
Login using OAuth 2.0 handled by the PHP SDK:
Login with Facebook
</div>
<?php endif ?>
<h3>PHP Session</h3>
<pre><?php print_r($_SESSION); ?></pre>
<?php if ($user): ?>
<h3>You</h3>
$_SESSION['user']="abc";
<img src="https://graph.facebook.com/<?php echo $user; ?>/picture">
<h3>Your User Object (/me)</h3>
<pre><?php print_r($user_profile); ?></pre>
<?php else: ?>
<strong><em>You are not Connected.</em></strong>
<?php endif ?>
<h3>Public profile of Naitik</h3>
<img src="https://graph.facebook.com/naitik/picture">
<?php echo $naitik['name']; ?>
</body>
</html>
Refer these tutorials .
Login with Facebook using PHP SDK
Login with facebook using PHP ( Demo and Download )
Well usually I don't use the php sdk to generate my login url, instead I do it manually, try changing this line:
Login with Facebook
To this:
Login with Facebook
The correct code in PHP - 3 steps in 1 form - is explained by iZend - The web engine, including the configuration on Facebook: http://www.izend.org/en/identification-by-facebook.
Opening a connection with Facebook using the SDK.
Obtaining a URL for a Connect with Facebook button.
Retrieving a connected user's email address (and other information like firstname and lastname for a register form).

Facebook php sdk - info not being shown

I downloaded the php sdk from github (link provided from developers.facebook.com). I uploaded the src folder at the root. Now, I pasted the code and replaced the appId and secret with mine. But the page is showing a server error. The following is my code :-
<?php
require 'src/facebook.php';
$facebook = new Facebook(array(
'appId' => 'xxxxxxxxxxxxxxxx',
'secret' => 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
));
$user = $facebook->getUser();
if ($user) {
try {
// Proceed knowing you have a logged in user who's authenticated.
$user_profile = $facebook->api('/me');
} catch (FacebookApiException $e) {
error_log($e);
$user = null;
}
}
// Login or logout url will be needed depending on current user state.
if ($user) {
$logoutUrl = $facebook->getLogoutUrl();
} else {
$loginUrl = $facebook->getLoginUrl();
}
$naitik = $facebook->api('/naitik');
?>
<!doctype html>
<html xmlns:fb="http://www.facebook.com/2008/fbml">
<title>php-sdk</title>
<body>
<h1>php-sdk</h1>
<?php if ($user): ?>
Logout
<?php else: ?>
<div>
Login using OAuth 2.0 handled by the PHP SDK:
Login with Facebook
</div>
<?php endif ?>
<h3>PHP Session</h3>
<pre><?php print_r($_SESSION); ?></pre>
<?php if ($user): ?>
<h3>You</h3>
<img src="https://graph.facebook.com/<?php echo $user; ?>/picture">
<h3>Your User Object (/me)</h3>
<pre><?php print_r($user_profile); ?></pre>
<?php else: ?>
<strong><em>You are not Connected.</em></strong>
<?php endif ?>
<h3>Public profile of Naitik</h3>
<img src="https://graph.facebook.com/naitik/picture">
<?php echo $naitik['name']; ?>
</body>
</html>
Please help me. I badly need some help. I cannot find any mistake nor have I modified any part of the code except the app id and the secret. The facebook.php file is in the src folder only. You can find my output at http://beta.jokesnfunnypics.com/login.
the issue is possibly that you are missing php Curl extension
Facebook PHP SDK Use's Curl To Send Requests.
so First you need to check if the CURL extension is installed at your server
like this
function _isCurl(){
return function_exists('curl_version');
}
if not install it and then it will all work

Can't get fb user's information

I'm getting started with Fb app development. I tried to deploy the example app given in the facebook php api but don't seem to get it working
Here is the code:-
<?php
require '../src/facebook.php';
// Create our Application instance (replace this with your appId and secret).
$facebook = new Facebook(array(
'appId' => '**************',
'secret' => '**************************',
));
// Get User ID
$user = $facebook->getUser();
if ($user) {
try {
// Proceed knowing you have a logged in user who's authenticated.
$user_profile = $facebook->api('/me');
} catch (FacebookApiException $e) {
error_log($e);
$user = null;
}
}
// Login or logout url will be needed depending on current user state.
if ($user) {
$logoutUrl = $facebook->getLogoutUrl();
} else {
$loginUrl = $facebook->getLoginUrl();
}
$naitik = $facebook->api('/naitik');
?>
<!doctype html>
<html xmlns:fb="http://www.facebook.com/2008/fbml">
<head>
<title>php-sdk</title>
<style>
body {
font-family: 'Lucida Grande', Verdana, Arial, sans-serif;
}
h1 a {
text-decoration: none;
color: #3b5998;
}
h1 a:hover {
text-decoration: underline;
}
</style>
</head>
<body>
<h1>php-sdk</h1>
<?php if ($user): ?>
Logout
<?php else: ?>
<div>
Login using OAuth 2.0 handled by the PHP SDK:
Login with Facebook
</div>
<?php endif ?>
<h3>PHP Session</h3>
<pre><?php print_r($_SESSION); ?></pre>
<?php if ($user): ?>
<h3>You</h3>
<img src="https://graph.facebook.com/<?php echo $user; ?>/picture">
<h3>Your User Object (/me)</h3>
<pre><?php print_r($user_profile); ?></pre>
<?php else: ?>
<strong><em>You are not Connected.</em></strong>
<?php endif ?>
<h3>Public profile of Naitik</h3>
<img src="https://graph.facebook.com/aitik/picture">
<?php echo $naitik['name'];
?>
</body>
</html>
But $user is always null, and it shows "You are not connected".
Is it because I'm using a free webhost and not having any secured URL?
Url of my app, if you want to see: http://apps.facebook.com/iiiimags

facebook user & friend permission not showing up on scopes

I have a simple login app to utilize oAuth for facebook.
I will need to access a few things about the user so i added a few parameters in theUser & Friend Permissions
I waited over 24 hours for the servers to propagate but when i debug the token, i dont see these permissions added to the scope
I grabbed the token by:
<?php
require '../src/facebook.php';
$facebook = new Facebook(array(
'appId' => '**************',
'secret' => '**********************************'
));
// Get User ID
$user = $facebook->getUser();
$token = $facebook->getAccessToken();
echo $token;
if ($user) {
try {
$facebook->setFileUploadSupport(true);
$album = $facebook->api('/me/albums ');
print_r($album);
} catch (FacebookApiException $e) {
error_log($e);
$user = null;
}
}
?>
<!doctype html>
<html xmlns:fb="http://www.facebook.com/2008/fbml">
<head>
<title>php-sdk</title>
<style>
body { font-family: 'Lucida Grande', Verdana, Arial, sans-serif; }
h1 a { text-decoration: none; color: #3b5998; }
h1 a:hover { text-decoration: underline; }
</style>
</head>
<body>
<h1>php-sdk</h1>
<?php if ($user): ?>
Logout
<?php else: ?>
<div>
Login using OAuth 2.0 handled by the PHP SDK:
Login with Facebook
</div>
<?php endif ?>
<h3>PHP Session</h3>
<pre><?php print_r($_SESSION); ?></pre>
<?php if ($user): ?>
<h3>You</h3>
<img src="https://graph.facebook.com/<?php echo $user; ?>/picture">
<h3>Your User Object (/me)</h3>
<pre><?php print_r($user_profile); ?></pre>
<?php else: ?>
<strong><em>You are not Connected.</em></strong>
<?php endif ?>
<!--h3>Public profile of Naitik</h3>
<img src="https://graph.facebook.com/naitik/picture"-->
<?php //echo $naitik['name']; ?>
</body>
</html>
What am I missing?
any help please, thank you.
Changing needed permission after user installed make you need to ask for the new permission.
So you can uninstall and install again, or make a JS to check if your user have the necessary permission and, if not, ask using FB.ui

facebook connect api

Is it possible to use the facebook connect api to just grab some information and use it with a customized form?
Because facebook register api shows a pre-filled form, how to get these data and use them with another form
Thanks
You should use the Facebook PHP SDK (see on github). Here is how you would do that.
require "facebook.php";
$facebook = new Facebook(array(
'appId' => '...',
'secret' => '...',
));
$user = $facebook->getUser();
if ($user) {
try {
$user_profile = $facebook->api('/me');
} catch (FacebookApiException $e) {
$user = null;
}
}
Here, if $user is not null, the user is logged in and you have his personal data in the array $user_profile.
Here is an example of the form you can generate pre-filled with some Facebook data :
<?php if ($user): ?>
<form action="#" method="get">
<input type="text" name="name" value="<?php echo $user_profile['name'] ?>">
<input type="submit" value="Continue →">
</form>
Logout of Facebook
<?php else: ?>
Login with Facebook
<?php endif ?>
See the example of the Facebook PHP SDK for more detail on the flow.