Facebook Javascript SDK - callback on successful permission request - facebook

I am just getting acquainted with Facebook javascript SDK.
What is the proper way to check if a permission request was successful then call a function? In my case the user is being requested additional permissions. They have already given basic permissions.
Right now I have this.. but it fires the function regardless of success or not
FB.login(function (publishAuth) {
if (publishAuth.authResponse) {
authSuccess();
} else { }
}, { scope: 'publish_stream' });
}
Thanks for the help
Mark

What is the proper way to check if a permission request was successful then call a function?
Since the user can accept/deny each (extended) permission individually, there is no simple true/false response to a permissions request.
To check, which permissions the user has actually granted and which not, you have to make a call to /me/permissions – see Graph API Explorer example.
(With field expansion you can do it also in one call while requesting other infos about the user – f.e. /me?fields=id,name,permissions)

You will always have an authResponse, but that doesn't mean you have the permissions you need, for that you need to check the properties IN the authResponse.
From Facebooks developers page:
FB.getLoginStatus(function(response) {
if (response.status === 'connected') {
// the user is logged in and has authenticated your
// app, and response.authResponse supplies
// the user's ID, a valid access token, a signed
// request, and the time the access token
// and signed request each expire
var uid = response.authResponse.userID;
var accessToken = response.authResponse.accessToken;
} else if (response.status === 'not_authorized') {
// the user is logged in to Facebook,
// but has not authenticated your app
} else {
// the user isn't logged in to Facebook.
}
});

Related

Share an image to Faceboook

I need to share an image to facebook. For this I am trying the API call suggested here
FB.login(function(response) {
if (response.authResponse) {
FB.api(
"/{page-id}/photos",
"POST",
{
"url": "https:\/\/www.facebook.com\/images\/fb_icon_325x325.png",
"published": "false"
},
function (response) {
if (response && !response.error) {
console.log(JSON.stringify(response))
/* handle the result */
}
else
{
console.log("ERROR: " + JSON.stringify(response))
}
}
);
} else {
console.log('User cancelled login or did not fully authorize.');
}
});
But I am getting an error (#803) Some of the aliases you requested do not exist: {page-id}
Is page-id the same as the userID returned in the authResponse? Because I also tried hardcoding that id, but I got a deprecated API message.
I need to allow the user to share the photo directly to his feed.
Thanks
Page ID and User ID are not the same. You get a User ID after authorization, you get a Page ID by using the /me/accounts endpoint with a User Token. You can also get Page Tokens with /me/accounts?fields=access_token. You need to use a Page Token with the publish_pages and manage_pages permissions to create posts or photos on a Page you manage.
As WizKid commented, you can also just use /me/photos to upload photos. Just use a Page Token.
More information: https://developers.facebook.com/docs/facebook-login/access-tokens/

Facebook birthday is not coming when logging in

I am using Facebook login button with JavaScript. Everything is working fine but my client needs to fetch "birthday" when any user log-in with Facebook.I have written the code but for some fb ids it is giving me the Birhtday while in some cases it is not giving.
I have checked answers on google already and have done almost everything but no success.
Here is my code I am using
HTML CODE
<a onclick="fb_login();" href="javascript:void(0);"><i class="fa fa-facebook"></i> <span>Log in with Facebook</span></a>
JavaScript Code
function fb_login(){
FB.login(function(response) {
if (response.authResponse) {
//console.log('Welcome! Fetching your information.... ');
//console.log(response); // dump complete info
access_token = response.authResponse.accessToken; //get access token
console.log(access_token);
user_id = response.authResponse.userID; //get FB UID
FB.api('/me', function(response) {
console.log(response);
user_email = response.email; //get user email
first_name = response.first_name; //get first name
last_name = response.last_name; //get last name
full_name = response.name; //get last name
fb_id = response.id; //get id
birthday = response.birthday; //get id
alert(birthday);return false;
// you can store this data into your database
var data = {
action: 'mb_facebook_login',
user_email:user_email,
first_name:first_name,
last_name: last_name,
full_name:full_name,
fb_id:fb_id,
birthday:birthday
};
jQuery.post(ajaxurl, data, function(ajaxresponse) {
console.log(ajaxresponse);
if(ajaxresponse=='loggedin')
{
//location.reload();
}
});
});
} else {
//user hit cancel button
console.log('User cancelled login or did not fully authorize.');
}
}, {
scope: 'user_birthday,email,user_location'
});
}
I have also made my birthday to public also set the permission here but it´s not working.
Please help me in this.Thanks in advance!
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.
That is one reason why it does not work for some people, without review it will only work for users with a role in the App (developer, tester, admin).
Another reasons could be that some users did not set a birthday. Not sure if that is possible, but it is worth testing.

facebook /me response undefinded

There are many posts about "/me" returns undefined, but I couldn't find any solution.
The user is logged in, because response.status is connected. But calling /me returns "undefined".
Code snippet:
if (response.status === 'connected') {
// The response object is returned with a status field that lets the app know the current
// login status of the person. In this case, we're handling the situation where they
// have logged in to the app.
FB.api('/me', function(info) {
alert("facebook:"+info.id);
});

How to get user_id in facebook when has logged facebook but not authorized?

FB.getLoginStatus(function(response) {
if (response.status === 'connected') {
// the user is logged in and has authenticated your
// app, and response.authResponse supplies
// the user's ID, a valid access token, a signed
// request, and the time the access token
// and signed request each expire
var uid = response.authResponse.userID;
alert(uid);
} else if (response.status === 'not_authorized') {
// the user is logged in to Facebook,
// but has not authenticated your app
// logged in and connected user, someone you know
var uid = response.id;
alert(uid);
} else {
// the user isn't logged in to Facebook.
alert("the user isn't logged in to Facebook.");
}
});
How to get uid when not_authorized ?
How to get uid when not_authorized ?
Not at all. Facebook does not give you such information, unless the user has authorized your app.
It’s a matter of privacy – think about it, every site on the net could set up their own app, and would get your FB user ID once you browse it. Would you want f.e. a random p0rn site that you might get redirected to from somewhere else without you even wanting this to happen to get your Facebook user ID? Of course you would not.

JS SDK getLoginStatus doesn't return userID nor signedRequest

I'm using PhoneGap/Cordova with the facebook plugin. Everything seems to work except for getLoginStatus who is not working as defined here and here. When called, it returns some data, but not all: it doesn't return userID nor signedRequest.
Here is the code:
FB.getLoginStatus(function(response) {
if (response.status == 'connected') {
var fb_uid = response.authResponse.userID;
var fb_signedRequest = response.authResponse.signedRequest;
alert('logged in');
} else {
alert('not logged in');
}
});
userID is filled with ellipsis (...), while signedRequest is undefined.
I managed to get userID with a graph call to /me:
FB.api('/me', function(me){
if (me.id) {
var fb_uid = me.id;
}
});
I wasn't able to find any way in the documentation to get a signed_request, which I have to use to authenticate the facebook user to a remote service to whom the user already connected to with facebook (I already made a login call so user is OK).
Basically the problem is that my call to getLoginStatus returns
{
status: 'connected',
authResponse: {
session_key: true,
accessToken: 'a long string...',
expiresIn:'a number',
sig: '...', //exactly this string
userID:'...' //exactly this string
secret:'...' //exactly this string
expirationTime:'a long number'
}
}
instead of what documented
As a background, when authentication happens using the plugin then the JavaScript SDK API calls the iOS/Android SDK to handle the authorization and then pass response auth data back to the JS part. The native (iOS/Android) SDKs do not get signed requests back to be able to pass this on to the JS. This is why it's empty.
If you use the latest plugin you should at least now be seeing the user ID. The one from June likely did not pass this back. Otherwise as a work around, you could perform a call to the /me endpoint when authentication is successful in your JS code to get the user id. See the Hackbook example that does this.