facebook redirect - php

I have a problem when using facebook authentication.
<?php
$user = null;
try{
include_once "fb/facebook.php";
}
catch(Exception $o){
error_log($o);
}
$facebook = new Facebook(array(
'appId' => $fbconfig['appid'],
'secret' => $fbconfig['secret'],
'cookie' => true
));
$loginUrl = $facebook->getLoginUrl(array(
'scope' => 'publish_stream,email',
//'redirect_uri' => 'https://www.facebook.com/pages/MediaEngine/304221192963546?id=304221192963546&sk=app_143090482526977'
));
$user = $facebook->getUser();
if($user){
try{
$user_profile = $facebook->api('/me');
$permissions = $facebook->api('/me/permissions');
}catch(FacebookException $e){
}
}else{
echo "<script type='text/javascript'>top.location.href = '$loginUrl';</script>";
}
?>
When the user accepted the app, it will redirect user to tab page(http://apps4you.hu), user wont stay on facebook, but only 1st time will do that if the user close and come back to app on facebook(already authenticated) it works fine in iframe.
If I use the redirect_uri => my_app , it will redirect and redirect and redirect but never will work the app, the browser just loading and loanding but nothing happends.
I spent days to fix this problem but I could not find any solution.
Any idea whats wrong?

Change $loginUrl as below and try:
$_SESSION['state'] = md5(uniqid(rand(), TRUE)); // CSRF protection
$loginUrl _url = "https://www.facebook.com/dialog/oauth?client_id="
. APP_ID . "&redirect_uri=" . urlencode($next) . "&state="
. $_SESSION['state'] . "&scope=".$permissions;

Related

Not getting any information from Facebook login

I'm implementing Facebook login on my website, it's working without error but ultimately I'm not able to get any information of a user.
I've following code.
<?php
include 'library.php';
include 'facebook.php';
$app_id = "XXXXXXXXX";
$app_secret = "XXXXXXXXX";
$site_url = "XXXXXXXXX";
$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 = NULL;
}
}
if($user){
$logoutUrl = $facebook->getLogoutUrl();
}else{
$loginUrl = $facebook->getLoginUrl(array(
'scope' => 'email',
'redirect_uri' => $site_url,
));
}
if($user){
Echo "Email : " . $user_profile['email'];
}
$user_fbid = $fbuser;
$user_email = $user_profile["email"];
$user_fname = $user_profile["first_name"];
$user_image = "https://graph.facebook.com/".$user_fbid."/picture?type=large";
echo $loginUrl = $facebook->getLoginUrl( array('scope' => 'email,read_stream'));
What's wrong with this? Can anyone point out? I've gone though a number of questions but none of them seemed to work for me & I finally ended up posting a question myself.
EDIT
When asking for login, it displays permissions correctly like email, friend list, your likes etc. So basically it's asking correctly & even though I approve, information is not coming up. Tried with a couple of users.
EDIT
Try use this code, and work from there ... That's how I figured it out
<HTML>
<BODY>
<?php
require_once("fb/facebook.php");
$config = array(
'appId' => 'XXXXX',
'secret' => 'XXXXXXX',
'fileUpload' => false, // optional
'allowSignedRequest' => false, // optional, but should be set to false for non-canvas apps
);
$facebook = new Facebook($config);
$uid = $facebook->getUser();
$loginParams = array('scope' => 'email');
$loginUrl = $facebook->getLoginUrl($loginParams);
$logoutParams = array('next' => 'http://fb_logouturi');
$logoutUrl = $facebook->getLogoutUrl($logoutParams);
echo $uid . '</br>';
echo 'Login:' . $loginUrl . '</br>';
echo 'Logout:' . $logoutUrl . '</br>';
if($uid) {
// 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 "</br>First Name: " . $user_profile['first_name'];
echo "</br>Last Name: " . $user_profile['last_name'];
echo "</br>Email: " . $user_profile['email'];
echo "</br>Gender: " . $user_profile['gender'];
echo "</br><img src='http://graph.facebook.com/" . $uid ."/picture?type=large'>";
} 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 {
// No user, print a link for the user to login
$login_url = $facebook->getLoginUrl();
echo 'Please login.';
}
?>
</BODY>
</HTML>

Fceabook PHP SDK valid access token

I have search over and over again and I can't seem to find an answer. What am I doing wrong?
My error is FacebookApiException [ 0 ]: An active access token must be used to query information about the current user.
EDIT Just to add. This worked once or twice, but never in succession.
require_once('media/fb/facebook.php');
$app_id = '123456';
$app_secret = '123456';
$my_url = 'mysiteurl';
$config = array(
'appId' => $app_id,
'secret' => $app_secret,
'fileUload' => 'false');
$facebook = new Facebook($config);
$user = $facebook->getUser();
if($user) {
try {
$user_profile = $facebook->api('/me','GET');
} catch(FacebookApiException $e) {
error_log($e->getType());
error_log($e->getMessage());
}
} else {
$login_url = $facebook->getLoginUrl(array('redirect_uri' => $my_url));
header("Location:" . $login_url);
$user_profile = $facebook->api('/me','GET');
}
Your help is greatly appreciated.
Does your error come from this line: $user_profile = $facebook->api('/me','GET'); at the bottom?
The header function will re-direct the user to Facebook login page which will cause the browser to refresh. You won't get the user's access token immediately after that. You need to wait until the user is redirected back to your website to get the access token. You need to set $my_url to point to the same page of the codes you wrote here.

Php with Facebook SDK don't load page

I use this code to take information about people friends with Facebook
:
require 'facebook-php-sdk-master/src/facebook.php';
$app_id = 'xxxxxxx';
$app_secret = 'xxxxxxxx';
$facebook = new Facebook(array(
'appId' => $app_id,
'secret' => $app_secret,
));
$user = $facebook->getUser();
if ($user) {
try {
$user_profile = $facebook->api('me/friends');
print_r($user_profile);
} catch (FacebookApiException $e) {
error_log($e);
$user = null;
}
} else {
$params = array(
'scope' => 'user_birthday',
);
$loginUrl = $facebook->getLoginUrl($params);
echo("<script> top.location.href='" . $loginUrl . "'</script>");
}
?>
But now, a lot of time, when i try to load this , i see that in the url the param "code" start to change a lot of time and the page don't load. What kind of problem is that?
$user is false in your code for some reason (always). May be the facebook user didn't accept your app. $facebook->getLoginUrl($params); redirects to the request uri, with $user false the request uri redirects to $loginUrl. This cause an endless loop.

Uncaught OAuthException: Error validating application

I am developing my first facebook app, which includes creating a new album and posting photos to the user wall. By learning through facebook documentation and few tutorial and came up this code, but I am getting following error with it.
Fatal error: Uncaught OAuthException: Error validating application. Thrown in /home/base_facebook.php on line 1106
Plus: I don’t know if it matters or not, but when I am printing out the
echo $facebook->getUser();
variable it giving me ‘0’ in return even though user (which in this case is myself) is logged in.
Kindly help me through this.
Code:
$config = array(
'appId' => '3663250024856',
'secret' => 'b14ac6d2b7dbdtyb259599b06983e881',
'fileUpload' => true,
'cookie' => true
);
$facebook = new Facebook($config);
echo "USER STATUS:".$facebook->getUser();
$facebook -> setFileUploadSupport(true);
$album_details = array('message' => 'Album desc', 'name' => 'Album name');
$create_album = $facebook -> api('/me/albums', 'post', $album_details);
$album_uid = $create_album['id'];
$photo_details = array('message' => 'Photo message');
$file = "temp/".$imageName;
$photo_details['image'] = '#' . realpath($file);
$upload_photo = $facebook -> api('/' . $album_uid . '/photos', 'post', $photo_details);
The best working solution I've found in the web. Add the below code at the top of your php page:
$app_id = "YOUR_APP_ID";
$app_secret = "YOUR_APP_SECRET";
$my_url = "YOUR_URL";
$code = $_REQUEST["code"];
if(empty($code)) {
$_SESSION['state'] = md5(uniqid(rand(), TRUE)); //CSRF protection
$dialog_url = "http://www.facebook.com/dialog/oauth?client_id="
. $app_id . "&redirect_uri=" . urlencode($my_url) . "&state="
. $_SESSION['state'];
echo("<script> top.location.href='" . $dialog_url . "'</script>");
}
if($_REQUEST['state'] == $_SESSION['state']) {
$token_url = "https://graph.facebook.com/oauth/access_token?"
. "client_id=" . $app_id . "&redirect_uri=" . urlencode($my_url)
. "&client_secret=" . $app_secret . "&code=" . $code;
$response = #file_get_contents($token_url);
$params = null;
parse_str($response, $params);
$graph_url = "https://graph.facebook.com/me?access_token="
. $params['access_token'];
}
Even though the user is logged in, the user needs to give permission to your app before you can make api calls.
Basically the flow for user authentication using facebook php sdk should be like this:
Detect whether the user is logged in and given permission to your app.
If he hasn't given permission, redirect him to the login and application permission page. After logging in and giving permission, he'll be redirected back again to your application page.
Otherwise, show the content of your app, make api calls,...
Here is a modified sample code from the php sdk's documentation page to handle authentication that should work if you just modify the appId, appSecret and redirect_uri:
<?php
require_once 'fb_php_sdk/facebook.php';
$appId = 'your_app_id';
$appSecret = 'your_app_secret';
// Create your Application instance (replace this with your appId and secret).
$facebook = new Facebook(
array(
'appId' => $appId,
'secret' => $appSecret,
'fileUpload' => true,
'cookie' => true
));
// 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;
}
}
// Login or logout url will be needed depending on current user state.
if ($user)
{
$logoutUrl = $facebook->getLogoutUrl();
}
else
{
$loginUrl = $facebook->getLoginUrl(
array(
'canvas' => 1,
'fbconnect' => 0,
'scope' => '',//these are the extended permissions you will need for your app, add/remove according to your requirement
'redirect_uri' => 'http://apps.facebook.com/test_app_azk/',//this is the redirect uri where user will be redirected after allow,
));
}
if ($user)
{
//show the content of your app
//make api calls
var_dump($user_profile);
}
else
{
//redirect user to loginUrl
echo "<script>parent.location = '".$loginUrl."';</script>";
}
Hope this helps.
The problem might lie within the else statement. Instead of using else use if(!user). That should fix the issue.

Simple Facebook App With Php Sdk

I just need simple and working facebook app. I bought ssl for this and i dont want too much expenses.
If user allowed app it will write his name and gender and his/her friends gender.
my app doesnt redirect after user allow the app. I cant fix it so just need basic app with out url redirecting.
ı used this code but it doesnt work. it need to be corrected:
require_once("facebook.php");
$facebook = new Facebook(array(
'appId' => '***',
'secret' => '***',
'scope' => 'manage_pages,offline_access,publish_stream,user_photos'
));
$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) {
$user = null;
}
}
if ($user) {
$logoutUrl = $facebook->getLogoutUrl();
} else {
$loginUrl = $facebook->getLoginUrl();
}
Here is the simple one page application full fills your needs
make the file with name index.php
download the facebook library download here
<?php
require_once 'library/facebook.php';
// Create our Application instance.
$facebook = new Facebook(array(
'appId' => 'appid',
'secret' => 'secret',
'cookie' => true,
));
$app_id = 'appid';
$canvas_page = "canvas_page_link";
$auth_url = "http://www.facebook.com/dialog/oauth?client_id="
. $app_id . "&redirect_uri=" . urlencode($canvas_page) . ("&scope=email,read_stream&response_type=token");
$signed_request = $_REQUEST["signed_request"];
list($encoded_sig, $payload) = explode('.', $signed_request, 2);
$data = json_decode(base64_decode(strtr($payload, '-_', '+/')), true);
if (empty($data["user_id"])) {
echo("<script> top.location.href='" . $auth_url . "'</script>");
} else {
//getting the userid and some other data for verification
//get the user id
$UserId = $data["user_id"];
echo 'UserId;' . $UserId;
//get the user access token
$token = $facebook->getAccessToken();
echo "</br>" . 'Access_Token:' . $token;
//set default access token and profile
$facebook->setAccessToken($token);
$user_profile = $facebook->api('/me');
//get the user name
$user_id = $facebook->getUser();
$user_profile = $facebook->api('/me','GET');
$user_name = $user_profile['name'];
echo "Name: " . $user_name;
$user_gender = $user_profile['gender'];
echo "Gender: " . $user_gender;
}
?>

Categories