I'm using https://github.com/pocesar/facebook-kohana for facebook login. I have problem with facebook logout. It doesn't destroy facebook session. I've tried so many things and I've read many questions. I tried this in my logout method, but no result:
$this->redirect('https://www.facebook.com/logout.php?
next=mysite.dev
&access_token=USER_ACCESS_TOKEN');
My logout method is:
public function action_logout(){
$facebook = new Facebook(array(
'appId' => 'appId',
'secret' => 'mySecret',
));
$user = $facebook->getUser();
$facebook->destroySession();
Session::instance()->delete('user');
$this->redirect('/');
}
How to destroy session, so that user can log in my site with another facebook account? Thanks!
My method login is:
public function action_fbLogin(){
$facebook = new Facebook(array(
'appId' => 'appId',
'secret' => 'Secret',
));
$user = $facebook->getUser();
if ($user) {
$user_profile = $facebook->api('/me', array('fields' => 'id,email,name,first_name,last_name,picture'));
$user_id = Model_UserFunctions::checkIfUserExist($user_profile['email']);
if($user_id > 0)
{
Session::instance()->set('user', array(
'fb_id' => $user_profile['id'],
'user_id' => $user_id,
'pic' => $user_profile['picture'],
'email' => $user_profile['email'],
'first_name' => $user_profile['first_name'],
'last_name' => $user_profile['last_name'],
));
//var_dump($_SESSION);
$this->redirect('profile');
exit;
}
$values = array(
'email' => $user_profile['email'],
'username' => $user_profile['email'],
'password' => '12345678'
);
$user = ORM::factory ( 'User' );
$user->values($values);
try
{
if($user->save()){
$user_new_id = $user->as_array();
$user_new_id = $user_new_id['id'];
Session::instance()->set('user', array(
'fb_id' => $user_profile['id'],
'user_id' => $user_new_id,
'pic' => $user_profile['picture'],
'email' => $user_profile['email'],
'first_name' => $user_profile['first_name'],
'last_name' => $user_profile['last_name'],
));
$this->redirect('profile');
}
}
catch (ORM_Validation_Exception $e)
{
$result = $e->errors('models');
echo '<pre>';
print_r($result);
exit;
}
}
else
{
$this->redirect($facebook->getLoginUrl(array('id,email,name,first_name,last_name,picture')));
}
exit;
}
Edited: My goal to use Facebook logout is because in my site I only use Facebook login, there isn't another way to log in my site, that's idea of it. And I should have logout method in my site, so when user wants to logout, he could do it.
This logouts me from facebook but facebook login page is shown. How to redirect next to my site. I've set it as next but it doesn't redirect to it:
<a href="https://www.facebook.com/logout.php?
next=http://mysite.dev
&access_token=Null">Logout</a>
You can get facebook logout url using facebook-php-sdk API.
When user will click on that url he will be offline from his facebook account.
if ($user) {
$logoutUrl = $facebook->getLogoutUrl();
} else {
$loginUrl = $facebook->getLoginUrl();
}
For more info you can check this url.
https://github.com/facebookarchive/facebook-php-sdk
Then you don't have a problem with facebook
Is only the session that needs to be destroyed...
On this page of PHP manual there is an example showing you might need aditional steps to destroy a session beyond unset($_SESSION)
http://php.net/manual/en/function.session-destroy.php
Forcing the users to log off from Facebook does not seem a good move for me.
Millions of FB users have intentionally asked to stay logged in and if they visit your site and logoff from there they might think it wasn't very friendly of you to log them off the FB accounts. many don't even remember their pwd... so you would be creating a problem for them and they might never comeback to your site... Just saying
Related
I have a facebook login implementation which takes user permission for user_likes and we check if the user has liked a certain page or not. The code has been working without any problem for last 6 months or so.
Suddenly about 2 weeks back it stooped working and was not giving any values for the user_likes. The facebook app was created before April 2014 so API V2.0 constrains does not apply.
My base code for facebook access is this
$facebook = new Facebook(array(
'appId' => $app_id,
'secret' => $app_secret,
));
$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();
}else{
// Get login URL
$loginUrl = $facebook->getLoginUrl(array(
'scope' => 'user_likes, user_birthday, user_location, user_work_history, user_hometown, user_photos,email',
'redirect_uri' => $site_url,
));
}
if($user){
// Save your method calls into an array
$fields='id,name,first_name,last_name,email,username,gender,location,birthday,link,locale';
$queries = array(
array('method' => 'GET', 'relative_url' => '/me?fields='.$fields),
array('method' => 'GET', 'relative_url' => '/'.$user.'/friends'),
array('method' => 'GET', 'relative_url' => '/'.$user.'/likes'),
array('method' => 'GET', 'relative_url' => '/'.$user.'/interests'),
);
// POST your queries to the batch endpoint on the graph.
try{
$batchResponse = $facebook->api('?batch='.json_encode($queries), 'POST');
}catch(Exception $o){
error_log($o);
}
It was showing "csrf state token does not match one provided" error in the logs so I updated the PHP-SDK hoping it will fix things for me, and it did. Everything started working fine, however after 3-4 days it stopped working again, now I cant even get users to login with facebook.
Does anyone has any idea why is this happening ?
I am trying to publish a checkin using facebook api. Sometimes, it works well and posts the checkin, but mostly 99% of the times it produces the error: "This webpage has a redirect loop"
checkin.php:
<?php
require("../src/facebook.php");
// construct the object with your facebook app data
$facebook = new Facebook(array(
'appId' => 'XXXXX',
'secret' => 'XXXX',
'cookie' => false
));
$token = $facebook->getAccessToken();
//echo $token;exit())
try {
// to get the id of the currently logged in user
// if, you want you can manually set a user id here like this:
//$uid = '[FB USER ID]';
$uid = $facebook->getUser();
$facebook->setAccessToken($token);
$facebook->api('/'.$uid.'/checkins', 'POST', array(
'access_token' => $facebook->getAccessToken(),
'place' => '101697613295949',
'message' => 'Enjoying Chill Beer with Team',
'picture' => 'http://test.com/someplace.png',
'coordinates' => json_encode(array(
'latitude' => '28.541203543000023',
'longitude' => '77.15503053709995',
'tags' => 'XXXX'))
));
echo 'You are checked in';
} catch (Exception $e){
// No user found - ask the person to login
$login_url = $facebook->getLoginUrl();
header("Location: ".$login_url);
}
?>
Thanks in advance.
I have the follow code to login with Facebook:
// Include Facebook SDK
$this->facebook = new Facebook(array(
'appId' => 'APP ID',
'secret' => 'APP SECRET',
'cookie' => true
));
$uid = $this->facebook->getUser();
if($uid) {
try {
echo 'Done!';
} catch (FacebookApiException $e) {
echo $e;
error_log($e);
}
} else {
$loginUrl = $this->facebook->getLoginUrl(array(
'redirect_uri' => 'http://ogabrielsantos.com.br/dev/index.php?route=account/connect/facebook',
'scope' => 'user_birthday'
));
echo('<script>top.location.href = "' . $loginUrl . '";</script>');
}
UPDATE 1
I can authorize the App, but, after authorization, I can't get the logged user, EVER redirecting to
http://ogabrielsantos.com.br/dev/index.php?route=account%2Fconnect%2Ffacebook&state=RANDOM STATE&code=RANDOM CODE#_=_
I have tried:
Put the APP in a server;
Verify APP settings. All is correct;
Verify if url is the same at settings and code. All is ok;
My url is properly written: http://ogabrielsantos.com.br/dev/index.php?route=account/connect/facebook;
Reset APP SECRET.
Nothing worked.
You can check what happens going to:
http://ogabrielsantos.com.br/dev/index.php?route=account/connect/facebook
Facebook ask you to authorize the app to get your website, which is a safe information.
UPDATE 2
The follow code:
$this->facebook = new Facebook(array(
'appId' => $this->connect->config->get('facebook_api_key'),
'secret' => $this->connect->config->get('facebook_app_secret'),
'cookie' => true
));
$uid = $this->facebook->getUser();
echo'<pre>';
print_r($this->facebook);
echo'</pre>';
exit;
returns:
Facebook Object
(
[sharedSessionID:protected] =>
[appId:protected] => APP ID
[appSecret:protected] => APP SECRET
[user:protected] => 0
[signedRequest:protected] =>
[state:protected] =>
[accessToken:protected] => APP ID|APP SECRET
[fileUploadSupport:protected] =>
[trustForwarded:protected] =>
)
I think the accessToken is wrong, returning my APP ID and SECRET with a pipeline separator.
UPDATE 3:
Now, I verify the code, but, always get Fatal error: Uncaught OAuthException: Error validating verification code
if(isset($_GET['code'])) {
$api = $this->facebook->api('/oauth/access_token', array(
'client_id' => APP ID,
'redirect_uri' => 'http://ogabrielsantos.com.br/dev/index.php?route=account/connect/facebook',
'client_secret' => APP SECRET,
'code' => $_GET['code']
));
echo'<pre>';
var_dump($api);
print_r($this->facebook->getUser());
echo'</pre>';
exit;
}
I have read some questions, which is similar but can't help-me.
Are you working on localhost? if yes it probably is the reason why.
If not, check your app settings in developers.facebook.com/apps/APP_ID
1. You have to compare the urls written in your code with the ones you have in your app settings.
2. Another reason could be the that the urls in you app settings are not properly written, I remember having to add index.php at the end of my urls in Page Tab settings. Worked for me
3. Another option is to get a new APP_SECRET and update your code
These options are from my personal experience, you issue could also be something else but you lose nothing trying
The Facebook Sharing and Facebook Sign In functions are completely separate and each require their own app.
Are you using facebook sharing in the page you want the login working on?
Ref: http://www.ning.com/help/?p=8523
I know this error has been bitten to death over here, and I read every single one to have better understanding of my problem.
But my issue is a bit different, and I wonder if anyone can give me good suggestion where to look.
I'm using wordpress + wordpress social login. This plugin authenticates user and stores name/age/email/fbID/fbProfilePic in the DB.
I've a little feature on my website where users registered through facebook can click and post message to the their wall.
My code looks like this:
<?php
//IF user is registered via facebook, when he clicks INTERESTED message will appear on his/her wall to spread the news
$user = get_user_meta($current_user->ID,'Facebook',true);
if ($user && $_GET['commentstab'] == 1 && !$_POST['delmycom']) {
require ('Facebook/facebook.php');
//Access details
$facebook = new Facebook(array(
'appId' => 'XXX',
'secret' => 'XXX'
//'cookie' => true
));
//try {
$params = array(
'message' => "Hurray! This works :)",
'name' => "This is my title",
'caption' => "My Caption",
'description' => "Some Description...",
'link' => "http://stackoverflow.com",
'picture' => "http://i.imgur.com/VUBz8.png",
);
$post = $facebook->api("/$user/feed","POST",$params);
echo "Your post was successfully posted to UID: $user";
//} //try
//catch (FacebookApiException $e) {
// $result = $e->getResult();
//} //catch
} //master if
?>
I read in various topics that I need publish_stream permission from user to perform this action. But since I store user info separately in my own DB, how do I get this publish stream permission? Do I need to modify wordpress plugin to store some kind of access token? and utilize this token to post to wall?
you generate loginurl with publish_stream permission
if user is not logged in with permission, you have to make sure he does
redirect user to proper page
you can do it like this,
<?php
include_once("facebook.php");
$facebook = new Facebook(array(
'appId' => APP_ID,
'secret' => APP_SECRET,
'cookie' => true,
));
$user = $facebook->getUser();
if ($user) {
try {
// Get the user profile data you have permission to view
$user_profile = $facebook->api('/me');
} catch (FacebookApiException $e) {
$user = null;
}
} else {
$loginUrl = $facebook->getLoginUrl(array('scope' =>'publish_stream','redirect_uri'=>'example.com'));
die('<script> top.location.href="'.$loginUrl.'";</script>');
}
$params = array(
'message' => "Hurray! This works :)",
'name' => "This is my title",
'caption' => "My Caption",
'description' => "Some Description...",
'link' => "http://stackoverflow.com",
'picture' => "http://i.imgur.com/VUBz8.png",
);
$post = $facebook->api("/$user/feed","POST",$params);
?>
I am trying to schedule wall posts to be added to the Facebook business page in the future.
As far as I can see Facebook does not recommend to use "offline_access" anymore.
How would you do that?
This is my code so far. It works if I am already logged into Facebook.
EDIT: Naturally I will create some code that check the schedule that I pull from the database. And use a cron job to regulary check that schedule.
require_once('src/facebook.php');
$config = array(
'appId' => 'xxxxxxx',
'secret' => 'xxxxxxx',
);
$facebook = new Facebook($config);
$user_id = $facebook->getUser();
$page_info = $facebook->api("/PAGE_ID?fields=access_token");
try {
$ret_obj = $facebook->api('/PAGE_ID/feed', 'POST',
array (
'link' => 'http://www.example.com/',
'message' => 'This is a test',
'access_token' => $page_info['access_token']
));
} catch(FacebookApiException $e) {
$login_url = $facebook->getLoginUrl( array(
'scope' => 'publish_stream'
));
print_r($e->getType());
print_r($e->getMessage());
}
You have to extend access_token periodically now. All other code should be working fine as previously