Facebook app permission request before using my app [duplicate] - facebook

I have a facebook app installed and working great.
What I would like to do is add in the functionality of requesting user permission data.
such as - read_friendlists,user_groups,email,user_birthday,status_update,publish_stream,
Firstly here is the code that checks if a fan or none fan is on our facebook timeline app.
// If a fan is on your page
if ($like_status) {
$a = file_get_contents("http://www.domain.com/home.php");
echo ($a);
}
else {
// If a non-fan is on your page
$a = file_get_contents("http://www.domain.com/home.php");
echo ($a);
}
What I would like to do, is, if a none fan is on our page, open a facebook request for permission and set a number of permissions. Then redirect them to the // if a fan is on our page.
I have had a good look around here but I am still not clear how to setup the "request" for permission page.
Any help would be greatly appreciated

you can do this in two ways..
1) Open oauth dialog in page by setting the url
var oauth_url = 'https://www.facebook.com/dialog/oauth/';
oauth_url += '?scope=email,user_birthday,user_location,user_about_me,user_likes';
oauth_url += '&client_id=' + appid;
oauth_url += '&redirect_uri=' + encodeURIComponent('https://www.facebook.com/pages/' + appname + '/' + pageId + '/?sk=app_' + appid);
window.top.location = oauth_url;
2) Or you can open the oauth dialog in popup:
FB.login(function(loginResponse) {
if (loginResponse.authResponse) {
var userId = loginResponse.authResponse.userID;
}
else {
//'User cancelled login or did not fully authorize.
}
}, { scope: 'email,user_birthday,user_location' });

Related

Get my Facebook wall posts

I'm using Hello.Js to get my Facebook wall posts. To doing so I have registered my demo app on developer.facebook.com (using a localhost url).
My hello.js setup is the following:
hello.init({
facebook: '<mykey>'
},
{
redirect_uri: 'redirect.html',
});
and this is the Facebook login:
var onFacebookError = function (e) {
$facebookEl.text(e.error.message);
};
// Facebook instance
var facebook = hello('facebook');
// Login
facebook.login().then(function (r) {
// Get Profile
facebook.api('me').then(function (p) {
$facebookEl.html('<span><img src="' + p.thumbnail + '" width=50 class="roundedAvatar" /><span style="margin-left: 15px;">Connected to Facebook as ' + p.name + '</span></span>');
}, onFacebookError);
}, onFacebookError);
The OAUth connection is working fine, but when I try to get my wall post, using:
// Facebook instance
var facebook = hello('facebook');
facebook.api('facebook:/me/share', function (r) {
var posts = [];
for (var i = 0; i < r.data.length; i++) {
var o = r.data[i];
posts.push({ social: 'facebook', text: o.text });
};
});
I get and empty r.data result.
What can the issue be? Do I have to enable other permissions on the Facebook app?
To get the user's timeline posts, you need to query the /feed edge with the user_posts permission.
I would recommend that you always try your calls in the Graph API Explorer first. This way you can be sure if this is an issue with the API or your implementation.

Facebook login on cordova app shows blank white screen

This problem has been bugging me for a long time and I can't seem to find the solution, all the settings in my Facebook Developer panel are configured correctly, Site URL, App Domain and OAuth URLs.
When I run my app on my iPhone (I have it installed through iTunes) and click the authentication button I am successfully prompted with this screen:
However, after logging in, I am faced with a blank white screen instead of being redirected to my main.html page.
I am using the OpenFB plugin along with Parse and the Facebook Graph API to authenticate and store my users data, here is my login code:
login.html:
$('.facebookLogin').click(function(){
Parse.User.logOut(); // log current user out before logging in
login();
});
function login() {
openFB.login(function(response) {
if(response.status === 'connected') {
console.log('Facebook login succeeded');
Parse.FacebookUtils.logIn("email", { // permission request to use email
success: function(user) {
if (!user.existed()) {
FB.api('/me', function(response) {
var firstName = response.first_name;
var lastName = response.last_name;
var email = response.email;
var user_id = response.id;
user.set("firstName",firstName);
user.set("lastName",lastName);
user.set("email",email);
user.save();
});
window.location.href= "main.html";
}
else {
window.location.href= "main.html";
}
},
error: function(user, error) {
alert("User cancelled the Facebook login or did not fully authorize.");
}
});
}
else {
alert('Facebook login failed: ' + response.error);
}
}, {scope: 'email'});
}
oauthcallback.html:
<html>
<body>
<script>
// redirects to main page
window.location.href= "main.html";
</script>
</body>
</html>
Note: I have added main.html, login.html and oauthcallback.html to the Valid OAuth redirect URIs list on my panel.
Check that your site is using SSL with a valid certificate.
There might be an error trying to redirect from a non-secure site to an encrypted site.

Facebook API 2.2 - me/accounts - returns array(0)

I'm actually having troubles with the graph API :
https://developers.facebook.com/tools/explorer?method=GET&path=me%2Faccounts&version=v2.2&
I've been generating an access token with the extended permission 'manage_pages' and i'm trying a request on the edge 'me/accounts'.
The result is always :
{
"data": [
]
}
But I wished to get a page access token instead.
Is this a normal behavior, or did I miss something?
I also tried with the php SDK 4.0 with a short-lived and a long-lived token and got the same result...
My code is here:
$app_id = '-hidden-'; //Facebook App ID
$app_secret = '-hidden-'; //Facebook App Secret
$long_lived_token = '-hidden-'; // tested at https://developers.facebook.com/tools/debug/
//and giving - Expires :1429438313 (in about 2 months)
FacebookSession::setDefaultApplication($app_id , $app_secret);
$session = new FacebookSession($long_lived_token);
if ($session) {
try {
$user_permissions = (new FacebookRequest($session, 'GET', '/me/permissions'))
->execute()->getGraphObject(GraphUser::className())->asArray();
$found_permission = false;
foreach($user_permissions as $key => $val){
if($val->permission == 'manage_pages'){
$found_permission = true;
}
}
// if we got manage_pages
if($found_permission){
$user_token = (new FacebookRequest($session, 'GET', '/me/accounts'))
->execute()->getGraphObject(GraphUser::className())->asArray();
var_dump($user_token); //array(0) { } - Why?? Is this normal??
} else {
echo "Manage pages not granted!";
}
} catch(FacebookRequestException $e) {
echo "Exception occured, code: " . $e->getCode();
echo " with message: " . $e->getMessage();
}
}
Thanks for your help!
My user didn't have any pages to admin, this is why the array is empty.
I guessed page access token could be use to manage profile but I was wrong.
For anyone who had this problem and still couldn't solve, my problem is that I had generated a access_token before I was granted the admin privilege in the page I was looking for and because of that, I don't know why, I couldn't retrieve the page. I then deleted the access from my facebook page and when I generate a new token, it worked.

phonegap - using external site as app - facebook login

I'm building a app site running through phone gap. Phone gap simply checks the user has internet connection and loads an external web app into the frame. I can navigat through the site fine with no blibs but as soon as I try the login to Facebook (either PHP redirect or javascript SDK) the app suddenly gets its navbar back or opens a new window (javascript SDK).
Is there anyway I can prevent this?
regards
It took some doing but using the ChildBrowser plugin, I've managed to login! (this is for android) I've used some code from a facebook connect plugin which didnt work for me, re wrote some stuffs so I could understand it and now works. Chears Juicy Scripter!
var fb_success = 'https://www.facebook.com/connect/login_success.html';
var fb_logout = 'https://www.facebook.com/connect/login_failed.html';
var fb_logout_ = 'http://m.facebook.com/logout.php?confirm=1&next=' + fb_logout;
var authorize_url = '';
var my_client_id = '##################';
var my_secret = '######################';
var my_type = 'user_agent';
var my_display = 'touch';
var token = false;
var fb_code = false;
var device_ready = false;
var ajax_url = '';
function logged_in(){
// alert('do what you need to do!');
}
function fb_force_logout(){
}
function fb_auth_check(){
console.log('fb_auth_check()');
if( fb_code !== false ) {
console.log('ajax test instigated...');
ajax_url = 'https://graph.facebook.com/oauth/access_token?client_id=' + encodeURIComponent(my_client_id) + '&client_secret=' + encodeURIComponent(my_secret) + '&code=' + encodeURIComponent(fb_code) + '&redirect_uri=' + fb_success;
$.ajax({
url: ajax_url,
type: 'POST',
success: function(html){
token = html.split("=")[1];
console.log('success! token = ' + token);
window.plugins.childBrowser.close();
fb_init();
},
error: function(error) {
console.log('there was an error...' + ajax_url);
window.plugins.childBrowser.close();
}
});
}
}
function fb_track_redirects(loc){
console.log('redirect tracked... ' + loc);
if ( loc.indexOf(fb_success) >= 0 || loc.indexOf(fb_success) > -1 ) {
fb_code = loc.match(/code=(.*)$/)[1]
console.log('success redirect... fb_code=' + fb_code);
fb_auth_check();
window.plugins.childBrowser.close();
} else if ( loc.indexOf(fb_logout) >= 0 || loc.indexOf(fb_logout) > -1 ) {
window.plugins.childBrowser.close();
}
}
function inner_init(){
console.log('inner_init()');
if( token === false ) {
console.log('token was false...');
authorize_url += "https://graph.facebook.com/oauth/authorize?";
authorize_url += "client_id=" + encodeURIComponent(my_client_id);
authorize_url += "&redirect_uri=" + encodeURIComponent(fb_success);
authorize_url += "&display=" + encodeURIComponent(my_display);
authorize_url += "&scope=publish_stream,offline_access";
console.log('instigated location change...');
window.plugins.childBrowser.onLocationChange = function(loc){
fb_track_redirects(loc);
}
console.log('open Facebbok login window');
window.plugins.childBrowser.showWebPage(authorize_url);
}else{
logged_in();
}
}
function fb_init(){
console.log('fb_init()');
if( device_ready === false ) {
console.log('first device run...');
document.addEventListener("deviceready", function(){
device_ready = true;
console.log('device ready...');
inner_init();
}, false);
}else{
inner_init();
}
}
$(document).ready(function(){
$('#login').bind('click', function(){
fb_init();
return false;
})
});
</script>
This is how it works for all apps native or web without patching the SDK code.
This is probably can be done, but will require digging into code. The question is do you really need it? This is a desired behavior.
You can try to use PhoneGap Facebook plugin and enable Single Sign On so native Facebook App if exists will be opened instead of browser to authenticate the user.
BTW,
Apps that are just external sites wrapped mostly rejected in app store.
Update:
Where is some points that may be also helpful in answer (by Facebook employee) to similar question How can I use an access token to circumvent FB.login().
Also have a look on ChildBrowser PhoneGap plugin (and Example).

In Facebook FB.ui (method: permissions.request) user is not redirect to proper location

Hi i m trying to show permission dialog using following code:
$permission = $facebook->api(array('method' =>'users.hasAppPermission','ext_perm'=>'publish_stream','uid'=> $uid));
if($permission != '1')
{
echo "<script type='text/javascript'>
var dialog = {
method: 'permissions.request',
perms: 'publish_stream',
redirect_uri: 'http://apps.facebook.com/abcd/'
};
FB.ui(dialog,null);
</script>";
}
This code works fine but problem is that when user allow to permissions he is not redirected to redirect_uri location(that is my canvas page) instead it goes to canvas url (that is my server's url). how to solve this problem Help me.
You can add the following javascript code in your page:
FB.Event.subscribe('auth.sessionChange', function(response) {
if (response.session) {
window.location.href = '/whatever/here';
} else {
// The user has logged out, and the cookie has been cleared
}
});