FB.login callback not called in javascript - javascript

I have a facebook app that is going to be installed on a facebook page. When an end user visits this app, he'll be asked to authorize the app. I'm using FB.login in JS to authorize and I do get facebook dialog asking for permission. But the control does not come to callback of FB.login at all. What am I missing here?
FB.init({
appId: <app_id>
});
FB.login(function(response){
console.log(response); //control never comes here even after user authorizing the app
}, {scope:"email,userlikes"});

You are adding a wrong permission here.
Replace userlikes with user_likes
(I dont think even the permissions were asked with your code since the permission is incorrect)
Check this Demo. When you close the login dialog, check the console of the browser, you'll see the response.

Related

How to get Facebook Graph API user_posts permission

I'm using facebook JS SDK version 2.11. For Login I've added following code
FB.login(function(response) {
// Handling Code here
}, { scope: "user_posts" });
But Login Dialog doesn't have permission for user_posts. I also checked using me/permissions and user_posts is not there.
How can I get access_token to get user_posts on FB.Login. App is still in developer mode and facebook review team is also facing the same issue.
Please Help!
I think you need to let Facebook review your app.
From the docs:
Which permissions require review?
Review is not required to ask for the three basic permissions: public_profile, user_friends and email.
Review is required to ask for any other permissions when people log into your app
You can try to get user feed at facebook graph api explorer - this is the best testing environment.
Then try
GET https://graph.facebook.com/v2.12/me/feed?access_token=<ACCESS_TOKEN>
this is request u are in need of use.
Also try
FB.login(function(response) {
// handle the response
console.log(response.grantedScopes); // should work as well
}, {
scope: 'publish_actions,email,user_likes,user_posts',
return_scopes: true
});
By setting the return_scopes option to true in the option object when calling FB.login(), you will receive a list of the granted permissions in the grantedScopes field on the authResponse object.
This will definitely help you with debugging your app. If #BigJ is right, you won't do much without app reviewed. Hope my answer is helpful.

Facebook javascript sdk asking for birthday permission randomly

I need to get user_birthday and email along with some of the basic profile info for a facebook user.
I am not using XFBML in my view but an HTML button that calls the following javascript function on click
function loginToFacebook(){
FB.login(function(response){
console.log(JSON.stringify(response));
checkLoginState('login');
}, {scope: 'email,user_birthday'});
return false;
}
The issue is a little strange, When I go to login and the permission dialog pops up. basic profile and email permission is always asked but user_birthday is random. for some id`s I can see this being asked for others not.
Am I missing something? any guidance? If you need other part of code, please let me know.
https://developers.facebook.com/docs/apps/changelog
Apps requesting more than public_profile, email and the user_friends permission must be reviewed by Facebook before those permissions can be requested from people.
Meaning, user_birthday needs to get approved before it can be used by users, else it will only work for users with a role in the App (Admin, Developer, Tester). There should be a very visible message telling you about that when you open the login dialog as Admin/Developer of the App.
More information: https://developers.facebook.com/docs/apps/review/login
I had a similar issue with user_email
While logging in a user has the choice to cherry pick their information sharing and logging into the application.
The best way to ensure data is captured accurately is to have an in-app form to ask them for what you need.

Get the Facebook fan pages the user owns/administrates

I'm using the Facebook JavaScript SDK to get the all the fan pages that the logged in user owns/administrates.
FB.api('/me/accounts', function(response){
console.log(response);
});
but this shows only the Facebook apps that I own. Am I using the wrong link?
Edit
I added this to my login button :
<fb:login-button perms="email,user_birthday,manage_pages" id="auth-loginlink"></fb:login-button>
and in the Auth Dialog (in the dashboard) i added manage_pages to extended permissions. In the authentication dialog i don't see the manage_pages permission and I still not see the fan pages in the response.
The answer is :
FB.login(function(){}, {perms:'manage_pages'});

Final re-direct for FB.login of Facebook's JavaScript SDK fails, displaying and returning an error

Users have been able to log into my website using their Facebook account, but then it suddenly stopped working properly.
I use the following standard Facebook JavaScript SDK code:
window.fbAsyncInit = function() {
FB.init({
appId : '<MY_APP_ID>',
status : true, // check login status immediately
cookie : true, // enable cookies to allow the server to access the session
xfbml : false // because I don't use XFBML
});
FB.login(function(response) {
// code that deals with `response`, whether null or not
});
}
But if I cleared the browser cache, and triggered this code (after the Facebook library had loaded), the following would happen:
Facebook's login dialog would pop up.
After entering credentials of a user that has access to this Facebook app, a dialog would ask whether I want to register a new login location.
Regardless of the action taken in the previous step, the dialog box displays the following error message:
An error may have occurred as part of the login process. You can close this window and try returning to the application, though it may ask you to log in again. This is probably due to a bug in the application.
FB.login's response contains an error message. Inspecting the browser's state, I can see that login information is stored within a Facebook cookie. Triggering the above code again, without clearing the cache, now succeeds.
Why doesn't it work the first time around?
Due to Facebook's OAuth 2.0 and HTTPS Migration, not using OAuth 2.0 after October 1, 2011 within JavaScript SDK will not work (properly).
To make the above example work, make sure that:
your FB.init call sets oauth to true (see example usage):
in the code that deals with the response:
you are reading the authResponse (not session) field of the FB.login's response;
you are calling FB.getAuthResponse (not FB.getSession), and reading signedRequest from its response.

Does Facebook FB.logout() still have a dialog box?

Using the new JS SDK from FB, I notice that there is no dialog telling the user they will be logged out from Facebook when logging out from my app.
What happens now is somewhat of a UI/UX problem: the user logs out from my app but also automatically logged out from Facebook without warning, which can be annoying.
Has anyone resolved this using FB SDK methods or some other function within FB.logout();?
Thanks for helping.
You will have to make your own UI dialog for this (or use the deprecated connect javascript sdk). You could either pop up a UI dialog warning that they will be logged out of both your app & out of facebook, or specify a callback method in the FB.logout function which tells them afterwards that they have been logged out of both.
Unfortunately this is as designed as noted here: http://developers.facebook.com/docs/reference/javascript/FB.logout/. As this is as designed in the Javascript SDK, I'm fairly confident in making an assumption that a server-side library will yield the same results.
I found a trick, that logs the user out of your application just client side, but leaves him logged in on facebook:
FB._authResponse = null;
FB._userStatus = null;
After that, calls to FB.api will return the proper error:
>>> FB.api('me', log)
{"error":{"message":"An active access token must be used to query information about the current user.","type":"OAuthException"}}
Also FB.getLoginStatus and FB.getAuthResponse are returning null or behave like the user is not logged in:
>>> FB.getLoginStatus(log)
{"status":null,"authResponse":null}
You can even log in the User again with FB.login()
But after a reload, the User will be logged in again automatically, if you have status : true in your FB.init config:
FB.init({
appId : 'yourappid',
status : false, // will not load the user automatically on pageload/refresh
cookie : true, // will leave the userid in FB._userID
oauth : true,
xfbml : true
});
Hope that helps a bit.

Categories