I've got 2 tabs open in Firefox, one is my website and the other is Facebook. Without the app authorised I get a login link, I click this which I am then prompted to auth the app which I do so.
I've now got on screen the logout URL provided by getLogoutUrl() - when I click this it redirects back to my site which I am happy with however it is still classing me as logged in. If I refresh the other tab with Facebook on I am now logged out. It seems there's some hangover on the session??
<?php
require_once("fb/facebook.php");
$config = array();
$config['appId'] = '...';
$config['secret'] = '...';
$config['fileUpload'] = false; // optional
$facebook = new Facebook($config);
$user_id = $facebook->getUser();
if($user_id) {
//Got an ID - Facebook says we're logged in
try {
$user_profile = $facebook->api('/me','GET');
echo "<img src=\"http://graph.facebook.com/".$user_profile['username']."/picture\">" . $user_profile['name']."";
$logout_url = $facebook->getLogoutUrl();
echo "Logout<br><br><br>";
}
catch(FacebookApiException $e) {
$login_url = $facebook->getLoginUrl();
echo 'Please login.';
error_log($e->getType());
error_log($e->getMessage());
}
}
else {
// Not logged in - give URL to login
$login_url = $facebook->getLoginUrl();
echo 'Please login.';
}
?>
You need to destroy the session.
$config = array();
$config['appId'] = 'xxxx';
$config['secret'] = 'xxxxx';
$facebook = new Facebook($config);
if(isset($_GET['act']) && $_GET['act'] == "logout") {
$facebook->destroySession();
}
$user = $facebook->getUser();
echo $user;
if ($user) {
try {
// Proceed knowing you have a logged in user who's authenticated.
$user_profile = $facebook->api('/me');
} catch (FacebookApiException $e) {
echo '<pre>'.htmlspecialchars(print_r($e, true)).'</pre>';
$user = null;
}
echo "<pre>";
print_r($user_profile);
echo "</pre>";
$logout = $facebook->getLogoutUrl();
//echo $logout;
echo "<a href='test.php?act=logout'>Logout</a>";
} else {
$login = $facebook->getLoginUrl(array("scope"=>"email","display"=>"popup","redirect_uri"=>"http://domain.com/test.php"));
echo "<a href='".$login."'>Login</a>";
}
Related
My PHP Facebook SDK is not working and always returns $user=0 Why?
It is my first time to deal with facebook sdk
my code:
require './facebook-php/src/facebook.php';
$facebook = new Facebook(array(
'appId' => 'xxxxxxxxxxxxxxx',
'secret' => 'xxxxxxxxxxxxxxxxxxxxxx'
));
// 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) {
$logoutUrl = $facebook->getLogoutUrl(); echo 'there is a user';
} else {
$loginUrl = $facebook->getLoginUrl();
echo 'there is noo user <a href='.$loginUrl.'>login</a>';
}
below is the url return code :
http://localhost/e-commerce/testit.php?code=AQA8DckDMqNarptbCUIfAmn2tIyn1mjVBmHTjOMaQ4GqsFW_6vqemGW35TC07Kxq7kuB4QkKibHJ2-m9GntIx0biWEiR_FUuozsONvgc_tNmtGURpcm78DnIE26vT6hbjz10eZECuh-GLec9tGplhLrDNbLO-aX2Hv2Mu-8CQhAdiFQ6AYW3Nt09a8Rzahbn35Jz0cNyB_G7Ksjorq3iSs6Bpa6Tk0d7Vx0A0f1Mg77PgD6IuN_grjkNHDWmb_ywoqW48IqoxJUhnRmL3qi1G-CT7ft_f77rRr-VP-yQ-LTkdNja0rBzoFnFGccMuQ86u8tk7nfnx5qrSLBpHLEIBqQT&state=84ff9f36e8de53d13c96bcf7cb2da3b3#=
I have uploaded Facebook setting image
Facebook settings images
I am creating a login function for my website, using the FB login API.
I use https://graph.facebook.com/oauth/access_token?client_id=ID&redirect_uri=URL&client_secret=SECRET&code=CODE
I am able to retrieve name and basics. But email I can not get.
In my app, I have set permissisions, including "email". But still, when the APP says "WEBSITE wants access to your information and friend list" the email option is not mentioned.
What am I doing wrong?
Best regards,
Rasmus
You must include the scope parameter and the redirect parameter.
$config = array();
$config['appId'] = 'xxx';
$config['secret'] = 'xxx';
$facebook = new Facebook($config);
if(isset($_GET['act']) && $_GET['act'] == "logout") {
$facebook->destroySession();
}
$user = $facebook->getUser();
echo $user;
if ($user) {
try {
// Proceed knowing you have a logged in user who's authenticated.
$user_profile = $facebook->api('/me');
} catch (FacebookApiException $e) {
echo '<pre>'.htmlspecialchars(print_r($e, true)).'</pre>';
$user = null;
}
echo "<pre>";
print_r($user_profile);
echo "</pre>";
$logout = $facebook->getLogoutUrl();
//echo $logout;
echo "<a href='test.php?act=logout'>Logout</a>";
} else {
$login = $facebook->getLoginUrl(array("scope"=>"email","display"=>"popup","redirect_uri"=>"http://domain.com/test.php"));
echo "<a href='".$login."'>Login</a>";
}
I'm trying to add Facebook PHP SDK on my website but, if nobody is connected to facebook, getLoginUrl() give me a wrong url (Internal Server Error).
He is my code :
include("facebook/src/facebook.php");
$config = array();
$config['appId'] = 'myAppId';
$config['secret'] = 'mySecretKey';
$facebook = new Facebook($config);
$user_id = $facebook->getUser();
if($user_id)
{
// We have a user ID, so probably a logged in user.
// If not, we'll get an exception, which we handle below.
try
{
$user_profile = $facebook->api('/me','GET');
echo "Name: " . $user_profile['name'];
}
catch(FacebookApiException $e)
{
// If the user is logged out, you can have a
// user ID even though the access token is invalid.
// In this case, we'll get an exception, so we'll
// just ask the user to login again here.
$login_url = $facebook->getLoginUrl();
echo 'Please login.';
error_log($e->getType());
error_log($e->getMessage());
}
}
else // The problem is below \\
{
// No user, print a link for the user to login
$login_url = $facebook->getLoginUrl(array('scope' => 'email,publish_stream,user_location,user_birthday'));
echo 'Please login.';
}
Any idea ?
I am using Codeignitor .
I want to post to facebook through my application.
the problem is it always returns you are not logged in.
here what i tried ..
function postTofb()
{
require_once("pocket/fb/facebook.php");
$fbconfig = array();
$fbconfig['appId'] = 'app_id';
$fbconfig['secret'] = 'app_secret';
$fbconfig['baseurl'] = "site_name";
$facebook = new Facebook($fbconfig);
$user = $facebook->getUser();
if($user)
{
try {
$user_profile = $facebook->api('/me');
$ID = $user_profile['id'];
echo $ID;
}
catch (FacebookApiException $e)
{
echo $e;
}
}
else
{
//else starts
$loginUrl = $facebook->getLoginUrl(
array(
'scope'=> 'email,offline_access,publish_stream,user_birthday,user_location,user_work_history,user_about_me,user_hometown',
'redirect_uri'=>$fbconfig['baseurl']
)
);
$data["status"] = FALSE;
$data["url"] = $loginUrl;
echo json_encode($data);
//else ends
}
}
now i am calling the function through ajax...but it always response with FALSE...i mean the else part.
here what i am getting
{"status":false,"url":"https:\/\/www.facebook.com\/dialog\/oauth?client_id=281255791959236&redirect_uri=http%3A%2F%2Fwww.MY_SITE_NAME.com%2F&state=3d19a96f48022344222f518609f30537&scope=email%2Coffline_access%2Cpublish_stream%2Cuser_birthday%2Cuser_location%2Cuser_work_history%2Cuser_about_me%2Cuser_hometown"}
looks like i missed some points...please help me..
Thank you.
Offline access is no longer supported by facebook, only thing you can do is generate a token with max 60 days.
The problem was associated with LoginUrl
I removed some of the permission parameters and it worked.
loginUrl = $facebook->getLoginUrl(array("scope")=>"publish_stream");
Thank you.
When the user authorize my app, he is redirected to my site and not to facebook with my app in the iframe. Is there a way to stay in facebook ?
My code :
require './src/facebook.php';
$config = array();
$config['appId'] = 'XXXXX';
$config['secret'] = 'XXXXX';
$config['fileUpload'] = false; // optional
$facebook = new Facebook($config);
$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) {
echo "blah";
} else {
$params = array(
'scope' => 'read_stream, friends_likes',
'redirect_uri' => 'https://xxxxx'
);
$loginUrl = $facebook->getLoginUrl($params);
echo("<script> top.location.href='" . $loginUrl . "'</script>");
}
?>
Try putting your app's url (http://apps.facebook.com/your_app_namespace) in the redirect_uri param. This should fix the issue you are facing.