Facebook re-authentication - php

I'm using PHP SDK to authentication and requesting permissions.
$facebook = new Facebook(array(
'appId' => '7xxx349xx',
'secret' => 'x0b360034c9exxxx4f',
'cookie' => true
));
$params = array(
'scope' => 'publish_actions, read_friendlists',
'redirect_uri' => 'example.com',
);
$user = $facebook->getUser();
$usertoken = $facebook->getAccessToken();
if ($user) {
try {
$user_profile = $facebook->api('/me');
} catch (FacebookApiException $e) {
error_log($e);
$user = null;
}
}
if ($user) {
$logoutUrl = $facebook->getLogoutUrl();
header("Location: xxx.php");
}
if(!$user) {
//$statusUrl = $facebook->getLoginStatusUrl();
$loginUrl = $facebook->getLoginUrl($params);
}
It works well but if user delete app in privacy settings and try to login again I have errors:
CSRF state token does not match one provided.
OR
OAuthException: Error validating access token: The user has not authorized application [APPID]
How to fix it? I need additional code?
Thanks for help

Related

Php Facebook login script ends up in a redirect loop and does not login

This code always runs the last else part and redirects to the login URL provided by facebook api. If i print the $user variable, it just shows 0 everytime.
$facebook = new Facebook(array(
'appId' => 'app id',
'secret' => 'app secret',
));
$user = $facebook->getUser();
$loginUrl = $facebook->getLoginUrl();
if ($user) {
try {
$user_profile = $facebook->api('/me');
} catch (FacebookApiException $e) {
error_log($e);
$user = null;
}
}
if ($user) {
$logoutUrl = $facebook->getLogoutUrl();
$_SESSION['logouturl'] = $logoutUrl;
$_SESSION['user'] = $user_profile['name'];
$_SESSION['id'] = $user_profile['id'];
$_SESSION['type'] = 'facebook';
header('Location: index.php');
} else
{
$loginUrl = $facebook->getLoginUrl();
header('Location: '.$loginUrl);
}
?>
$facebook = new Facebook(array(
'appId' => 'xx',
'secret' => 'xx',
'scope' => 'read_stream, email'
));
if ($user) {
$logoutUrl = $facebook->getLogoutUrl();
} else {
$params = array(
'scope' => 'read_stream, email'
);
$loginUrl = $facebook->getLoginUrl($params);
make sure your app setting on Facebook is good, rest is looking fine and if you have minor problem in you app setting on Facebook developer console it wont work specially domain name and domain url settings

Facebook redirect url to index.php

index.php
<?php
//facebook application
$fbconfig['appid'] = "32##########";
$fbconfig['secret'] = "ca2dc#############";
$fbconfig['baseurl'] = "http://localhost/sbs/fblogin/index.php";
//
if (isset($_GET['request_ids'])) {
//user comes from invitation
//track them if you need
}
//facebook user uid
try {
include_once "src/facebook.php";
}
catch (Exception $o) {
error_log($o);
}
// Create our Application instance.
$facebook = new Facebook(array(
'appId' => $fbconfig['appid'],
'secret' => $fbconfig['secret'],
'baseurl' => $fbconfig['baseurl'],
'cookie' => true
));
//Facebook Authentication part
$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.
$loginUrl = $facebook->getLoginUrl(array(
'scope' => 'email,offline_access,publish_stream,user_birthday,user_location,user_work_history,user_about_me,user_hometown,user_photos ,user_work_history'
));
$logoutUrl = $facebook->getLogoutUrl();
if ($user) {
try {
// Proceed knowing you have a logged in user who's authenticated.
$user_profile = $facebook->api('/me');
}
catch (FacebookApiException $e) {
//you should use error_log($e); instead of printing the info on browser
// d is a debug function defined at the end of this file
$user = null;
}
}
//if user is logged in and session is valid.
if ($user) {
//get user basic description
$userInfo = $facebook->api("/$user?fields=picture,name,email,gender,birthday");
//$pic = $facebook->api("/$user/pictures");
$profile = json_encode($userInfo);
$res = json_decode($profile, true);
$_SESSION['name'] = $res['name'];
$_SESSION['email'] = $res['email'];
$_SESSION['id'] = $res['id'];
$_SESSION['gender'] = $res['gender'];
$_SESSION['birthday'] = $res['birthday'];
$_SESSION['img'] = $res['picture']['data']['url'];
$_SESSION['auth_type'] = "facebook";
if (isset($_COOKIE['registration']) && $_COOKIE['registration'] == true) {
header("location:../sbs/registration.php");
} else {
header("location:../sbs/sbs_login.php");
}
}
?>
For the 1st time I am working on the Facebook app. I have made app on the Facebook developer. It's working properly but it is not redirecting me in the index.php. I want to redirect it in this page only so all the values are stored in the session and I am checking it if the cookies is made then this value is going to registration.php and if not then its going to sbs_login.php. Please can anybody tell where to give the redirect url?
$loginUrl = $facebook->getLoginUrl(array(
'baseurl' => $fbconfig['baseurl'],
'scope' => 'email,offline_access,publish_stream,user_birthday,user_location,user_work_history,user_about_me,user_hometown,user_photos ,user_work_history'
));
I find the solution of my problem . so this is the solution
$loginUrl = $facebook->getLoginUrl(array(
'baseurl' => $fbconfig['baseurl'],
'scope' => 'email,offline_access,publish_stream,user_birthday,user_location,user_work_history,user_about_me,user_hometown,user_photos ,user_work_history'

$facebook->api('/me') is returning NULL

I am trying to use Facebook connect in Yii framework.
The problem is I want to access user's email address and
$facebook->api('/me') is returning NULL.
How to fix that problem?
Here is my code:
<?php
Yii::import("ext.fconnect.*");
$app_id = "xxx";
$app_secret = "xxxx";
Facebook::$CURL_OPTS[CURLOPT_SSL_VERIFYPEER] = false;
$facebook = new Facebook(array(
'appId' => $app_id,
'secret' => $app_secret,
));
$user = $facebook->getUser();
if ($user) {
try {
$user_profile = $facebook->api('/me');
} catch (FacebookApiException $e) {
$user_profile = $e;
$user = NULL;
}
}
if ($user) {
$d["logoutUrl"] = $facebook->getLogoutUrl(array(
'fblogout' => 'true',
'next' => $this->createAbsoluteUrl("main/signout")
));
$d["user_info"] = $facebook->api('/' . $user);
} else {
$d["loginUrl"] = $facebook->getLoginUrl(array(
'scope' => 'email, read_stream, publish_stream, user_birthday, user_location, user_work_history, user_hometown, user_photos',
'redirect_uri' => $this->request->hostInfo . $this->request->url,
));
}
$d["user"] = $user;
$d["userprofile"] = $user_profile;
The line:
$d["user_info"] = $facebook->api('/' . $user);
Looks wrong to me. I don’t know what data type $user would be; I’d just use $facebook->api('/me'); and that will give you the profile of the currently logged in and authenticated user.

$user = $facebook->getUser() returns 0 with SDK 3.2.2

I m using the facebook sample javascript code that come with sdk but no luck
i am using the latest 3.2.2 SDK
require 'facebook.php';
$facebook = new Facebook(array(
'appId' => '---------',
'secret' => '----------',
));
// See if there is a user from a cookie
echo $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) {
echo '<pre>'.htmlspecialchars(print_r($e, true)).'</pre>';
$user = null;
}
}
Use this code:
header('P3P: CP="CURa ADMa DEVa PSAo PSDo OUR BUS UNI PUR INT DEM STA PRE COM NAV OTC NOI DSP COR"');
require 'facebook.php';
$facebook = new Facebook(array(
'appId' => '---------',
'secret' => '----------',
));
$querystr=http_build_query($_REQUEST);
// See if there is a user from a cookie
$user = $facebook->getUser();
if(isset($_REQUEST['code'])) {
if(!empty($_REQUEST['code'])) {
setcookie("access_token", $_REQUEST['code'], time()+120);
}
echo ("<script> top.location.href='<YOUR APP URL>'</script>");
exit;
}
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;
}
}else {
$scope='email';
$params = array(
'redirect_uri' => 'YOUR SITE URL(WHere app is hosted)',
'display' => 'page',
'scope' => $scope
);
$loginurl = $facebook->getLoginUrl($params);
echo ("<script> top.location.href='".$loginurl."'</script>");
exit;
}

How to get publish_stream permissions for facebook app?

I have an app which has been running for a while which has only requested some account information for login purposes. Now I want to use it to publish to the stream. I have added publish_stream to the req_perms but, it still doesn't ask for that permission.
Am I missing something?
<?php
# CREATE FACEBOOK BUTTON
$facebook = new Facebook(array(
'appId' => FACEBOOKAPPID,
'secret' => FACEBOOKSECRET,
'cookie' => false,
));
$fb_session = $facebook->getUser();
$fb_me = null;
// Session based API call.
if ($fb_session) {
try {
$fb_uid = $fb_session;
$fb_me = $facebook->api('/me');
$fb_me['photo'] = 'http://graph.facebook.com/'.$fb_uid.'/picture?type=large';
$_SESSION['login_api'] = 1;
$_SESSION['login_api_details'] = $fb_me;
$_SESSION['login_api_user_id'] = $fb_uid;
# WE ARE GOOD TO GO, LETS GET THE ACCESS TOKEN
$_SESSION['access_token'] = $facebook->getAccessToken();
#header_redirect(SITEURL.'/login');
} catch (FacebookApiException $e) {
error_log($e);
}
}
else{
# LOGIN URL FOR FACE BOOK & request extra stuff
$fb_login_url = $facebook->getLoginUrl(array('req_perms'=>'publish_stream,email,user_about_me,user_birthday,user_website'));
header_redirect($fb_login_url);
}
?>
Try this in your else
$params = array(
'canvas' => 1,
'scope' => 'publish_stream,email,user_about_me,user_birthday,user_website',
'fbconnect' => 1,
'redirect_uri' => 'https://apps.facebook.com/YOURAPP',
);
$fb_login_url = $facebook->getLoginUrl($params);
header_redirect($fb_login_url);

Categories