When I visit index.php, everything works well and I see the acces_token appear in the session variable.
However, when I click on a link to go to a new page, and I click (on the new page) on a link that brings me back to index.php, the access_token is changed to a much shorter token, starting with my appId.
(edit: I just discovered that this shorter token is this: appid|appsecret)
I've tried linking to an exact copy of index.php, and the same thing happens. It's like there isn't a new facebook instance created, or something... Why isn't a new access_token created?
Below is my code. You'll notice that I don't log the user in on my app. That's because this part is done on another page, via the Javascript SDK.
I've been looking for hours... thanks in advance for your advice.
index.php
<?php
include "config.php";
require "ok.php";
?>
<br/><br/>
Link.
<br/><br/>
<?php
$user = $facebook->getUser();
$userInfo = $facebook->api("/$user");
?>
<?php d($user); ?>
<strong>PHP Session</strong>
<?php d($_SESSION); ?>
?>
ok.php
<?php
$fbconfig['appid' ] = "###";
$fbconfig['secret'] = "###";
$user = null;
try{
include_once "facebook.php";
}
catch(Exception $o){
echo '<pre>';
print_r($o);
echo '</pre>';
}
$facebook = new Facebook(array(
'appId' => $fbconfig['appid'],
'secret' => $fbconfig['secret'],
'cookie' => true,
));
function d($d){
echo '<pre>';
print_r($d);
echo '</pre>';
}
?>
config.php
<?php
session_start();
//We log to the DataBase
mysql_connect('...', '...', '...');
mysql_select_db('...');
?>
Related
Currently I have the facebook api working. I have included scope string to grab extra permissions.
I am lacking the ability to authorize access to restricted pages on my site. How can I set it up so when facebook logs the user in they are also logged in to my site?
<?php
include 'src/facebook.php';
// Create our Application instance (replace this with your appId and secret).
$facebook = new Facebook(array(
'appId' => '****************',
'secret' => '****************',
'cookie' => true
));
// Get User ID
$user = $facebook->getUser();
// We may or may not have this data based on whether the user is logged in.
//
// If we have a $user id here, it means we know the user is logged into
// Facebook, but we don't know if the access token is valid. An access
// token is invalid if the user logged out of Facebook.
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();
echo "<a href='$logoutUrl'>Logout</a>";
} else {
$statusUrl = $facebook->getLoginStatusUrl();
$loginUrl = $facebook->getLoginUrl(array(
'scope' => 'user_checkins, user_location, friends_checkins')
);
echo "<a href='$loginUrl'>Login</a>";
}
?>
<h3>Welcome to your profile</h3>
<?php
echo $user_profile['first_name'];
echo ' <span> </span> ';
echo $user_profile['last_name'];
?>
This is the basic php api from facebook.
There are endless ways to do this, but you should just be able to place the private content inside of an IF block checking for the logged in Facebook user:
<?php if ($user): // Show the private content ?>
<h3>Welcome to your profile</h3>
<?php
echo $user_profile['first_name'];
echo ' <span> </span> ';
echo $user_profile['last_name'];
?>
<?php endif; ?>
On my facebook app, I'm logging a user in using the Javascript SDK.
If OK, I redirect him to this PHP page (designed to illustrate my problem):
test.php:
<?php
include('config.php');
require('phpsdk.php');
$access_token = $facebook->getAccessToken();
echo $access_token;
echo "<br /><br />";
$user = $facebook->getUser();
echo $user;
?>
(phpsdk.php handels the PHP SDK initiation)
Accessing test.php the FIRST time, returns a valid access token and the ID of the logged user.
However, when I refresh the page, the access token becomes of the type appID|appSecret.
The user is still logged in: getUser still returns the correct user ID.
Anyone knows what's causing this?
Clearing the Facebook Session with php sdk in redirect when user logs out.
Logout.php redirect user and use sdk to clear session data.
example redirect url. yourdomain.com/Logout.php?destroy=true
refer to: https://developers.facebook.com/docs/reference/php/facebook-destroySession/
<?php
session_start();
require './src/facebook.php';
$facebook = new Facebook(array(
'appId' => 'appID',
'secret' => 'appSecret',
'cookie' => true, // enable optional cookie support
));
if(isset($_GET['destroy'])){
$facebook->destroySession();
}
?>
It is not recommended to call getAccessToken(); over and over the way you described.
You should try first to see if the token is in the browser as a session and if not then try to use the SDK to find or request it.
Check browser for a user, if no user call sdk for user, if still null or 0 auth or reauth.
If user, check browser for access_token, if no token call sdk for token.
<?php
session_start();
include('config.php');
require('phpsdk.php');
// need to replace Your_App_ID and YourAppId with your applications id.
$facebook = new Facebook(array(
'appId' => 'Your_App_ID',
'secret' => 'Your_App_Secret',
'cookie' => true, // enable optional cookie support
));
if(isset($_SESSION['fb_YourAppId_user_id'])){
$user = $_SESSION['fb_YourAppId_user_id'];
}else{
$user = $facebook->getUser();
}
if($user){
if(isset($_SESSION['fb_YourAppId_access_token'])){
$access_token = $_SESSION['fb_YourAppId_access_token'];
}else{
$access_token = $facebook->getAccessToken();
}
}
echo $user;
echo "<br /><br />";
echo $access_token;
?>
i have created a facebook app and it works well for new users if they start the app with the direct server link (https://www.mydomain.com/app/index.php) and use the login button.
But if they try to use the app within facebook (https://apps.facebook.com/myapp) as a canvas app(for the first time) then they see the "landing" page of my app and the log in button. But if they click on it, an errormessage appears, that the content of the iframe couldn't be displayed. If they start the app in a new tab, then the login process works. (see the error below, sorry it's german, maybe you are familiar with this type of error message?)
It would be desirable if the new user could use the login link in the canvas app.
How can this be achieved? My index.php looks like this: (i am using the facebook SDK)
<?php
require_once('php-sdk/facebook.php');
$config = array(
'appId' => 'YOUR_APP_ID',
'secret' => 'YOUR_APP_SECRET',
);
$facebook = new Facebook($config);
$user_id = $facebook->getUser();
?>
<html>
<head></head>
<body>
<?php
if($user_id) {
try {
$user_profile = $facebook->api('/me','GET');
echo "Name: " . $user_profile['name'];
} catch(FacebookApiException $e) {
$login_url = $facebook->getLoginUrl();
echo 'Please login.';
error_log($e->getType());
error_log($e->getMessage());
}
} else {
$login_url = $facebook->getLoginUrl();
echo 'Please login.';
}
?>
</body>
</html>
i have created a facebook app and it works well for new users if they start the app with the direct server link (https://www.mydomain.com/app/index.php) and use the login button.
But if they try to use the app within facebook (https://apps.facebook.com/myapp) as a canvas app(for the first time) then they see the "landing" page of my app and the log in button. But if they click on it, an errormessage appears, that the content of the iframe couldn't be displayed. If they start the app in a new tab, then the login process works. (see the error below, sorry it's german, maybe you are familiar with this type of error message?)
It would be desirable if the new user could use the login link in the canvas app.
How can this be achieved? My index.php looks like this: (i am using the facebook SDK)
<?php
require_once('php-sdk/facebook.php');
$config = array(
'appId' => 'YOUR_APP_ID',
'secret' => 'YOUR_APP_SECRET',
);
$facebook = new Facebook($config);
$user_id = $facebook->getUser();
?>
<html>
<head></head>
<body>
<?php
if($user_id) {
try {
$user_profile = $facebook->api('/me','GET');
echo "Name: " . $user_profile['name'];
} catch(FacebookApiException $e) {
$login_url = $facebook->getLoginUrl();
echo 'Please login.';
error_log($e->getType());
error_log($e->getMessage());
}
} else {
$login_url = $facebook->getLoginUrl();
echo 'Please login.';
}
?>
</body>
</html>
I am new to creating a facebook application and hence started out with a simple Hello World Application. But When I try to display my Name, it is not displayed.
This is my code:
<?php
require_once 'facebook.php';
$api_key = 'mykey';
$secret = 'mysecretkey';
$facebook = new Facebook($api_key, $secret);
$user = $facebook->require_login();
$fbml = "<h2>Hello world, my name is <fb:name uid='<?php echo $fb_user;?>' useyou='false' />,"
. "welcome to my first facebook application!</h2>";
echo $fbml;
?>
The output in my facebook app is,
Hello world,my name is,welcome to my first facebook application!
My name is missing here.
And If I add the code given below,to display the friend's list,
$friends = $facebook->api_client->friends_get();
echo "<pre>Friends:" . print_r($friends, true). "</pre>";
I get the error message,
Warning: fopen() [function.fopen]: php_network_getaddresses: getaddrinfo failed:
No such host is known.
in C:\Documents and Settings\256148\My Documents\ide\xampplite\htdocs\facebookApp\facebookapi_php5_restlib.php on line 1755
Warning: fopen(http://api.facebook.com/restserver.php) [function.fopen]:
failed to open stream: php_network_getaddresses: getaddrinfo failed:
No such host is known. in C:\Documents and Settings\256148\My Documents\ide\xampplite\htdocs\facebookApp\facebookapi_php5_restlib.php on line 1755
Please guide me.
I have found a solution for my problem. I had to set the connect url in the application setting to my canvas callback url and created a cross domain communication channel file as given in this tutorial. This tutorial is really very helpful and explains in detail about creating a simple facebook app.
But still I get the Network error when I use $facebook->api_client->friends_get();
FBML Facebook Application
First, try using the following code for authentication process (applogin.php):
<?php
require_once "facebook.php";
require_once "config.php";
$facebook = new Facebook(array(
'appId' => FB_APP_ID,
'secret' => FB_API_SECRET,
'cookie' => true
));
$session = $facebook->getSession();
$params = array(
'canvas' => 1,
'fbconnect' => 0,
'next' => URL_CANVAS,
'cancel_url' => 'http://www.facebook.com/',
'req_perms' => 'publish_stream, status_update, offline_acces'
);
$login_url = $facebook->getLoginUrl($params);
// if not logged in, redirec to login page
if (!$session) {
echo '<fb:redirect url="' . $login_url . '" />';
exit;
} else {
try {
$uid = $facebook->getUser();
$me = $facebook->api('/me');
$access_token = $session['access_token'];
} catch (FacebookApiException $e) {
echo 'Error';
exit;
}
}
?>
Here's the config.php:
<?php
// Facebook
define('FB_APP_ID', '');
define('FB_API_SECRET', '');
define('URL_CANVAS', 'http://apps.facebook.com/YOURAPP/');
define('URL_CALLBACK', 'http://FB.YOURCALLBACKURL.COM/YOURAPP/');
?>
And finally your index.php
<?php
require_once dirname(__FILE__) . "/applogin.php";
// using Graph API
echo $me['name'];
?>
<!--// some FBML //-->
<fb:bookmark />
<h2>Hello world, my name is fb:name uid='<?php echo $uid;?>' useyou='false' />
welcome to my first facebook application!</h2>
Hope this will help you write your own Facebook application.