How to request permissions for facebook app with JavaScript - javascript

How can I check the current user login status and ask for permissions for my app? This recently changed and now my app is not working anymore. Thanks for your help!

I always use this setup to handle login status and permissions:
function getPerms() {
FB.login(function(response) {
if (response.authResponse) {
//user has granted permissions
}
}, {scope:"email, ..."});
}
function getStatus() {
FB.getLoginStatus(function(response) {
if (response.status === "connected") {
//user has granted permissions
} else {
getPerms();
}
});
}
You still have to include the all.js and call FB.init(...)

Here is how you can do that:
FB.init(
{
"appId" : xxxxxxxxxx,
"status" : true,
"cookie" : true,
"xfbml" : true,
"oauth" : true
});
FB.Event.subscribe('auth.authResponseChange', function(response)
{
// need to wait few seconds for fb login status to work in FireFox
setTimeout('window.location.reload()', 3000);
});
function fbConnect(){
FB.getLoginStatus(function(response) {
if (response.authResponse){
FB.login(function(response)
{
if (response.authResponse) { }
else
{
//console.log('User cancelled login or did not fully authorize.');
}
}, {"scope":"email,manage_pages"});
}
});
}
You need to call fbConnect() function.
Also make sure that you have these below <body> tag on your page:
<div id="fb-root"></div>
<script src="//connect.facebook.net/en_US/all.js"></script>

You need to provide some code to get better/accurate answers
Most likely you didn't upgrade your code to use OAuth 2.0, see here
Based on the link above, use scope instead of perms
Use response.authResponse instead of response.session
Welcome to Stackoverflow!

Related

FB.ui feed or share not returning post_id response

I am trying to use FB.ui share or feed on my test web site and want to get post_id on response message. App has permissions from my test user (user_posts, public_profile) and also i am checking if login status is "connected".
this is javascript code that i am trying to check login and let user to share link. shareOnFacebook() is triggered by a simple html button;
<script async defer src="https://connect.facebook.net/en_US/all.js"></script>
<script type="text/javascript">
window.fbAsyncInit = function () {
FB.init({
appId: '****************',
cookie: true,
xfbml: true,
version: 'v5.0'
});
};
function shareOnFacebook() {
FB.getLoginStatus(function (response1) {
console.log(response1)
if (response1.status === 'connected') {
FB.ui({
method: 'feed',
link: 'http://www.linktoshare.com',
picture: 'http://www.linktoshare.com/images/imagethumbnail.png'
}, function (responseEnd) {
if (responseEnd) {
console.log(responseEnd);
alert('Success');
} else {
alert('Fail');
}
});
}
else {
FB.login(function (response2) {
console.log(response2)
FB.ui({
method: 'feed',
link: 'http://www.linktoshare.com',
picture: 'http://www.linktoshare.com/images/imagethumbnail.png'
}, function (responseEnd) {
if (responseEnd) {
console.log(responseEnd);
alert('Success');
} else {
alert('Fail');
}
})
});
}
});
}
</script>
As you can see from below image, if i close pop-up, i can get response image like that;
But when i share, reponse returns as an empty array like that;
I know the error about "Https" is not important because my app is on development status.
What should i do to get post_id on response here?
Thank you very much.
What should i do to get post_id on response here?
You can’t get the post ID any more, Facebook has removed that completely.
It was getting abused by shady developers and website owners to try and force users to share stuff, to get some kind of reward - which is absolutely not allowed. To stop that kind of spam, Facebook has removed this a while ago already.

Facebook connect: can retrieve the id, the username but cannot retrieve the email

I can get the id and username from facebook connect, but I cannot retrieve the email !
Here is the JS script:
function connectionFacebook()
{ console.log('connectionFacebook called');
FB.api('/me?fields=email,name', { fields: 'name, email' }, function(response)
{
console.log(response);
response gives me:
Object {name: "John Doe ", id: "11112222333344445555"}
But no email !
PS. I guess it uses some old FB connect js since I work on an quite old site.
I have no idea what version of FB it uses, but I guess an old one !
ex: of code found in site;
FB.Event.subscribe('auth.login', function(response)
{
connectionFacebook();
;
});
FB.getLoginStatus(function(response)
{
if (response.authResponse)
{
connectionFacebook();
}
else
{
// no user session available, someone you dont know
//alert("getLoginStatus:deconnecté");
}
});
$.fn.connexionFacebook = function( ) {
return this.each(function () {
FB.init({
appId : xxxxxxxxx,
status : true,
cookie : true,
xfbml : true
});
});
}
})( jQuery );
<script src="http://connect.facebook.net/fr_FR/all.js"></script>
<fb:login-button show-faces="false" width="450" perms="email,user_birthday,user_location" size="medium">FaceBook connect</fb:login-button>
I'd guess that you don't have a permission to access the user's email. Facebook requires you to set the scope that determines which information you need to access.
In you case you need to specify the scope as public_profile,email to access the email. you can do that when your user logs in. Either with the API call:
FB.login(callback, { 'scope': 'public_profile,email' });
or with the button:
<fb:login-button scope="public_profile,email"></fb:login-button>
Specifying the email in the scope will ask the user to share her email address with your application when she logs in:

FB.Login opening in new window

I have this code
FB.login(function(response) {
if (response.authResponse) {
self.location.href='javascript:postToWall()';
} else {
window.top.location='https://www.facebook.com/dialog/oauth?client_id=XXXXX&redirect_uri=XXXXX&state=5140df0126eae59fbc72ac733383bda3&canvas=1&fbconnect=0&scope=email+publish_stream';
}
}, {scope: 'email,publish_stream'});
it's working but opened in pop up, i need to open it in full window. How can i do ?
You are looking at the wrong API calls. See Facebook OAuth Dialog. It is impossible to do it the way you are currently trying to.
Try this
FB.getLoginStatus(function(response)
{
if (response.status === 'connected')
{
self.location.href='javascript:postToWall()';
}
else
{
window.location.href = 'https://www.facebook.com/dialog/oauth?client_id=xxxx&redirect_uri=xxxx';
}
}, {scope: 'email,publish_stream',
return_scopes: true
});

Prevent blocking popups that appear with facebook SDK

A have trouble with blocking popups which are showed by Facebook JS SDK. After reading documentation, I understand that the call of JS SDK must be after the user's click.
This code works correctly and browser don't block the popup:
function Login() {
FB.login(function (response) {
if (response.authResponse) {
DoSmth(response.authResponse.accessToken);
}
}, { scope:'user_birthday, publish_stream' });
}
But if I will use this code anywhere (for ex. post to wall, get user data), the popup will be displayed in all cases, even if user already logged in in facebook. I consider, it's not fine.
To determine, that user already logged in, I use this code:
function CheckOrLogin() {
FB.getLoginStatus(function (response) {
if (response.status === 'connected') {
DoSmth(response.authResponse.accessToken);
}
else {
FB.login(function (response) {
if (response.authResponse) {
DoSmth(response.authResponse.accessToken);
}
}, { scope:'user_birthday, publish_stream' });
}
}, true);
}
In this case popup will not be shown, if user already logged in. But if user hasn't been logged in facebook, this condition (if (response.status ===) returns false. And browser will be BLOCK popup, which will be shown by FB.login. Is there any way to prevent this?
Try to check login status at once when page loaded, and store the result and accessToken in some variable.
var userData = {isConnected: false, token: null};
FB.getLoginStatus(function (response) {
userData.isConnected = (response.status === 'connected');
if (userData.isConnected) {
userData.token = response.authResponse.accessToken;
}
}, true);
When user clicks some button – just check variable's value.
function CheckOrLogin() {
if (userData.isConnected) {
DoSmth(userData.token);
} else {
FB.login(function (response) {
if (response.authResponse) {
DoSmth(response.authResponse.accessToken);
}
}, { scope:'user_birthday, publish_stream' });
}
}

Facebook connect fbs_[app_id] cookie not being set from JavaScript SDK

I'm using the Graph API via JavaScript SDK like this (this is basic example straight from documentation):
<html>
<head>
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.6.2/jquery.min.js" type="text/javascript"></script>
<script type="text/javascript">
$(document).ready(function() {
$('#login').click(function() {
FB.login(function(response) {
if (response.authResponse) {
console.log('Welcome! Fetching your information.... ');
FB.api('/me', function(response) {
console.log('Good to see you, ' + response.name + '.');
});
} else {
console.log('User cancelled login or did not fully authorize.');
}
});
})
})
</script>
</head>
<body>
Hellow!
Login
<div id="fb-root"></div>
<script src="http://connect.facebook.net/en_US/all.js"></script>
<script>
window.fbAsyncInit = function() {
FB.init({
appId: '249274258430595',
cookie: true,
status: true,
xfbml: true,
oauth: true
});
};
</script>
</body>
</html>
It's also possible to test it live here:
http://bitcells.com
This cookie I need for backend access to API (fbs_249274258430595) is not being set.
Only something called fbsr_249274258430595 is present and this is not what I need. I tested this with FireCookie extension for Firebug.
I really don't understand how this basic example is not working right - meaning that I want to use API from the backend code (PHP, Ruby etc.).
Any ideas?
Thank you!
David
I, on the other hand ended up setting my own cookie which server side can read:
I have one checkbox which asks user if he wants to share on fb, here is the code:
function setFbCookies(response) {
Cookie.set('fb_access_token', response.authResponse.accessToken, {path: '/'})
Cookie.set('fb_user_id', response.authResponse.userID, {path: '/'})
}
when('#share_on_fb', function(checkbox) {
checkbox.observe('change', function(ev) {
if(checkbox.getValue()) {
FB.getLoginStatus(function(response) {
if (response.authResponse) {
setFbCookies(response)
} else {
FB.login(function(response) {
if (response.authResponse) {
setFbCookies(response)
} else {
checkbox.checked = false
}
}, {scope: 'publish_stream'});
}
})
}
})
})
And here is the reason why fbs_xxxxx cookie doesn't get set:
https://developers.facebook.com/blog/post/525/
It looks like if you use oath=false, then it does get set, but this is only valid until 1st of october. There is still no documentation on how to get encrypted access token from the new fbsr_xxxxxx cookie.

Categories

Resources