facebook user authentication php - php

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.

Related

User_likes not shown SDK Facebook for PHP 3.2.3

I try to get my user_likes. I got an access token in the Graph API Explorer for user_likes. When I put hardly my PAGE_ID_WANT_TO_CHECK and my ACCESS_TOKEN_USER_LIKES in the url bar, my datas are good shown in my browser. But, in my code when I make the test if my $likes are empty or not, it still say it's empty.
var_dump($likes) is just empty.
Would like to know why and how to have a full array ?
Here my code.
Thank you.
require_once('fb-sdk/facebook.php');
require_once('config.php');
$facebook = new Facebook($config);
$user_id = $facebook->getUser();
if ($user_id) {
try {
$likes = $facebook->api('/me/likes/PAGE_ID_WANT_TO_CHECK?access_token=ACCESS_TOKEN_USER_LIKES');
if( !empty($likes['data']) ){
echo "I LIKE!!!!!!";
}else{
echo "not a fan!";
}
} catch (FacebookApiException $e) {
error_log($e);
}
}
var_dump($likes['data']);
//During the login process
$login_url = $facebook->getLoginUrl(array(
'scope' => 'user_likes'
));

Facebook login - can not retrieve email address

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

Can't logout on my site when using getLogoutUrl()

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

$facebook->api('/me') not giving the user data

require_once($_SERVER['DOCUMENT_ROOT'] . '/facebook-php-sdk/facebook.php');
$facebook = new Facebook(array(
'appId' => FB_MAIN_APP_API_ID,
'secret' => FB_MAIN_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;
}
}
Above is the code I'm using to get the Facebook details for my application. In my localhost this is working fine and it gives my Facebook email, user id. After I upload this in to live server this function is not working and it's not giving my Facebook details. I checked on the Facebook app id and it is the correct one.
I cannot figure out why it's not working in live site since it is working in my localhost. Please help me with this. Thank you.
I think problem is in your app which your are using on server. So try the same app which you are using for your localhost and in login and logout url instead of localhost use $_SERVER['HTTP_HOST'].
Example :
$session->fb_logout = $facebook->getLogoutUrl(array('next' => "http://" . $_SERVER['HTTP_HOST'] . "/path/to/logout"));
First check that what $user_profile = $facebook->api('/me'); showing. Just print_r the variable $user_profile.
Ex :
$user_profile = $facebook->api('/me');
print_r($user_profile);
exit;
if it showing blank then surely it will go in catch block. Then write following in catch block.
print_r($e);
exit;
For geting the user id, user need to grant permission
$user_id = $facebook->getUser();
echo $uid;
if($user_id){
try {
$user_profile = $facebook->api('/me');
} catch (FacebookApiException $e) {
error_log($e);
$user_id = null;
}
}else{
$login_url = $facebook->getLoginUrl();
echo("<br>login url=".$login_url);
};
After the user grant permission check the login_url, you may access user id.
Try this
Have you checked you have the correct URL in the configuration of your app in Facebook?
I have this piece of code to my FB apps and work properly, could you try it?
$session = $this->facebook->getUser();
if ($session) {
$logoutUrl = $this->facebook->getLogoutUrl();
try {
$data['uid'] = $this->facebook->getUser();
$me = $this->facebook->api($session);
$data['me'] = $me;
} catch (FacebookApiException $e) {
error_log($e);
}
} else {
$loginUrl = $this->facebook->getLoginUrl(
array(
'scope' => 'publish_stream, user_about_me, email',
'redirect_uri' => 'YOUR URL',
'canvas' => 1,
'fbconnect' => 0,
'display' => 'page',
));
}
if (!$session) {
echo "<script type='text/javascript'>top.location.href = '$loginUrl';</script>";
exit;
}
$this->load->view('MY VIEW', $data);

Direct access after authentication insteed iframe access

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.

Categories