Ask for Facebook permissions with dialog box only - php

This code is used to get facebook data from users of my codeigniter application that I would be launching in a facebook page tab.
require_once('facebook-php-sdk/src/facebook.php');
$facebook= new Facebook(array(
'appId' => $this->config->item('app_id'),
'secret' => $this->config->item('app_secret'),
'allowSignedRequest' => false,
));
$_REQUEST += $_GET;
$user = $facebook->getUser();
if ($user) {
try {
$user_profile = $facebook->api('/me?fields=picture.width(100).height(100),first_name,last_name,username,email');
} catch (FacebookApiException $e) {
$user = null;
}
} else {
$login_url = $facebook->getLoginUrl(array(
'scope' => 'email'
));
redirect($login_url, 'refresh');
}
I want it so that if the user is already logged into facebook that when they open the tab to use the app, instead of being asked to log in, that the dialog box come up asking for permissions. I need email permissions.
How do i skip the login to get permissions step if a user is already logged into Facebook, and just have the dialog box appear asking for permissions?

Related

Facebook app doesn't pop up for permissions

Everything works fine with test users but when I publish my app and user clicks on app the permission popup dialog box does not appear. I guess there is some oauth problem but not sure. Please help me out
require 'facebook-files/facebook.php';
$app_id='xxxxxxxxxx';
$secret='xxxxxxxxxxxxx';
$facebook = new Facebook(array(
'appId' => $app_id,
'secret' => $secret,
'cookie'=> true,
'grant_type' => 'client_credentials'
));
// Get User ID
$user = $facebook->getUser();
if ($user) {
try {
//$access_token = $facebook->getAccessToken();
// Proceed knowing you have a logged in user who's authenticated.
$user_profile = $facebook->api('/me');
$friends = $facebook->api('/me/friends?fields=id,name,birthday,picture');
$event = $facebook->api('/me/?fields=events.fields(id,name)');
} catch (FacebookApiException $e) {
echo "Exception".error_log($e);
$user = null;
exit;
}
}
// Login or logout url will be needed depending on current user state.
if ($user) {
$logoutUrl = $facebook->getLogoutUrl();
} else {
$loginUrl = $facebook->getLoginUrl(array(
'scope' => 'read_stream,publish_actions,publish_stream,user_birthday,friends_birthday', 'display'=>'popup')
);
}
Make sure to add your site to the "valid oauth redirect uris" input field in your app's settings.

Ask permission for Facebook user

I am using the following code to get Data on the logged Facebook user for my codeigniter application.
require_once('facebook-php-sdk/src/facebook.php');
$facebook= new Facebook(array(
'appId' => $this->config->item('app_id'),
'secret' => $this->config->item('app_secret'),
'allowSignedRequest' => false,
));
$_REQUEST += $_GET;
$user = $facebook->getUser();
if ($user) {
try {
$user_profile = $facebook->api('/me?fields=picture.width(100).height(100),first_name,last_name,username,email');
} catch (FacebookApiException $e) {
$user = null;
}
} else {
$login_url = $facebook->getLoginUrl(array(
'scope' => 'email'
));
echo 'Please login.';
}
This works and returns me the user's Facebook data, and if no user is logged in it provides the login link.
In the login link I ask for email permission as I need to collect these for communication. When the user logs in with the login link in the else clause, I successfully get the user's email address.
However I want to launch this application in a Facebook page tab using Woobox. Therefore the user would usually always be logged into their account. But the login to get email permissions is in the else statement if the user isn't logged in.
How do I ask for email permissions for an already logged in user, without haveing them login again?
How can I make it so that if the user is already logged into facebook, that the auth box asking the user for permissions comes up, where they can click ok to continue and authorize, rather than the login button.

Facebook App canvas Page not loading

I keep getting this error when I try to use my app I created for Facebook. When I go the the url for the app it take me to the login page, but when I log in I get a pop up box with the error.
App Not Setup: The developers of this app have not set up this app
properly for Facebook Login.
Every time I click ok it just reloads the popup again.
I have already changes the satatus to Live under status and review in my dashboard.
When I log into my account on facebook, it works fine, but I suppose this is because I am set as an admin for the app on the dashboard.
But other users get the problem above.
The code used:
require_once('facebook-php-sdk/src/facebook.php');
$facebook= new Facebook(array(
'appId' => $this->config->item('app_id'),
'secret' => $this->config->item('app_secret'),
'allowSignedRequest' => true,
));
$_REQUEST += $_GET;
$user = $facebook->getUser();
if ($user) {
try {
$user_profile = $facebook->api('/me?fields=picture.width(100).height(100),first_name,last_name,username,email');
} catch (FacebookApiException $e) {
$user = null;
}
} else {
$login_url = $facebook->getLoginUrl(array(
'scope' => 'email'
));
redirect($login_url, 'refresh');
}
This code is in my controller of my codeigniter application which I built the app.
How do I fix this?
UPDATE:
After I added the app as a canvas app this error seemed to go away, however there is still a problem.
I added the Facebook app platform and entered the canvas url, secure canvas url etc.
When I go to the app at the canvas page https://apps.facebook.com/MyApp/ - it work fine as I am a app administrator I guess.
However when an external user goes to the canvas page it just remain blank, but if a user goes to the canvas url http://apps.mydomain.com/MyAPP the app asks for their permissions and works fine as a website.
It just remains blank to users on the canvas page. Why doesn't the canvas page ask for permission?
Help Please.
EDIT:
require_once('facebook-php-sdk/src/facebook.php');
$facebook= new Facebook(array(
'appId' => $this->config->item('app_id'),
'secret' => $this->config->item('app_secret'),
'allowSignedRequest' => true,
));
$_REQUEST += $_GET;
$user = $facebook->getUser();
if ($user) {
try {
$user_profile = $facebook->api('/me?fields=picture.width(100).height(100),first_name,last_name,username,email');
} catch (FacebookApiException $e) {
$user = null;
}
} else {
$login_url = $facebook->getLoginUrl(array(
'scope' => 'email'
));
require_once('facebook-php-sdk/src/facebook.php');
$facebook= new Facebook(array(
'appId' => $this->config->item('app_id'),
'secret' => $this->config->item('app_secret'),
'allowSignedRequest' => true,
));
$_REQUEST += $_GET;
$user = $facebook->getUser();
if ($user) {
try {
$user_profile = $facebook->api('/me?fields=picture.width(100).height(100),first_name,last_name,username,email');
} catch (FacebookApiException $e) {
$user = null;
}
} else {
$login_url = $facebook->getLoginUrl(array(
'scope' => 'email'
));
echo("<script> top.location.href='" . $login_url . "'</script>");
}
Why doesn't the canvas page ask for permission?
Because you are redirecting inside of the iframe, but the FB login dialog does not want to be displayed in (i(frames) – for the obvious reason, that that would lead to a lot of phishing, since users are supposed to see that the page they are entering their login credentials too is actually the real FB site.
So you have to redirect within the top window instance, via JavaScript:
top.location.href = '…';

Facebook authentication fails

I'm about to give up. for about a week i'm trying the simplest thing, authentication via facebook. every time I authenticatiom (via facebook login url) , getUser() and api('me'
) returns me null.
why?
the code:
$facebook = new Facebook(array(
'appId' => '306597542762982',
'secret' => '88XXXXXX7f1',
));
$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) {
$loginUrl = $facebook->getLoginUrl(array('scope'=>'user_actions.news,publish_stream,user_about_me,email,publish_actions'));
print $loginUrl; exit;
}
var_dump($facebook->api('me'));
SOS.
EDIT: POST :
$params = array (
'article' => 'http://fb.raal.co.il/content/view/$id/$title',
'access_token' => $facebook->getAccessToken()
);
$out = $facebook->api( '/me/news.reads','post', $params);
var_dump($out); exit;
Continuing what NullPointer quoted earlier, from user ifaour:
Just check for the current Facebook user id $user and if it returned null then you need to reauthorize the user (or use the custom $_SESSION user id value - not recommended)
require 'facebook/src/facebook.php';
// Create our Application instance (replace this with your appId and secret).
$facebook = new Facebook(array(
'appId' => 'APP_ID',
'secret' => 'APP_SECRET',
));
$user = $facebook->getUser();
$photo_details = array('message' => 'my place');
$file='photos/my.jpg'; //Example image file
$photo_details['image'] = '#' . realpath($file);
if ($user) {
try {
// We have a valid FB session, so we can use 'me'
$upload_photo = $facebook->api('/me/photos', 'post', $photo_details);
} catch (FacebookApiException $e) {
error_log($e);
}
}
// login or logout url will be needed depending on current user state.
if ($user) {
$logoutUrl = $facebook->getLogoutUrl();
} else {
// redirect to Facebook login to get a fresh user access_token
$loginUrl = $facebook->getLoginUrl();
header('Location: ' . $loginUrl);
}
I've written a tutorial on how to upload a picture to the user's wall.
With the additional information, does this help? There is additional information I've found related to this, stating to attempt
$facebook->getAccessToken();
Apparently, that call can give either an app access token or a user access token. Depending on what you are trying to do with the access token, you will need the appropriate kind.
Alternatively, you may need to request certain permissions for what you are trying to do, which you can find here.

facebook not giving email

I am implementing the facebook login using php-facebook sdk. but i am not getting the user's email address from facebook, with newly created applications on facebook.
as per attached images we have make the changes in permissions settings below for email. when i preview auth dialog, facebook is showing email permission in dialog. but while running the site auth dialog is not showing the email permission and also not giving the email returned.
thanks in advance for any help.
set your scope (permission) in the app settings
also refer this :
$facebook = new Facebook(array(
'appId' => 'xxxxxxxxxxxxxxxxxxxxxx',
'secret' => 'xxxxxxxxxxxxxxxxxxxxx',
));
$facebookUser = $facebook->getUser();
$fbLogoutUrl = $facebook->getLogoutUrl();
if ($facebookUser) {
try {
$fbUserProfile = $facebook->api('/me');
} catch (FacebookApiException $e) {
error_log($e);
$user = null;
}
if (!empty($fbUserProfile)) {
$fbUserId = $fbUserProfile ['id'];
$fbUserEmail = $fbUserProfile ['email'];
}
} else {
$fbLoginUrl = $facebook->getLoginUrl(array('scope' => 'email,publish_stream,user_photos,photo_upload,offline_access'));
header("Location: " . $fbLoginUrl);
}
Regards : TechNew.In
That's not how you configure permissions for use with the PHP SDK.
Those settings are for the App Center only (Authenticated Referrals have been removed)
This is one way you can add permissions within the SDK,
$params = array(
'scope' => 'email',
'redirect_uri' => 'https://www.myapp.com/post_login_page'
);
$loginUrl = $facebook->getLoginUrl($params);
See more at: http://developers.facebook.com/docs/reference/php/facebook-getLoginUrl/
And: https://developers.facebook.com/docs/opengraph/authentication/#referrals

Categories