FB API not returning albums - facebook

This is how I create the login Button (with photo_upload permission):
<fb:login-button autologoutlink="true" perms="publish_stream, offline_access, photo_upload, share_item"></fb:login-button>
Then, I call the following function to retrieve the albums:
function getAlbum()
{
console.log("retrieving the albums");
FB.api('/me/albums', function(response){
console.log(response);
});
return true;
}
but the response object is as follows:
Object
Data: Array[0]
length: 0
I am having 2 public albums, but I cant able to retrieve them with above code. Can someone please tell me what I am doing wrong.
Thanks in advance :)

found the solution. the problem is that I missed the 'user_photos' permission.
Now, everything works fine :)

Related

Get the location id of a fan page

I have a fan page with an address.
Also, I can get the location details using the facebook API.
However, to create a new post with a review button I need the location id instead of the details.
I am using the following lines to create a share button:
var data = {
message: 'Hi !',
place: location_id
};
FB.api('/me/feed', 'post', data, function(response) {
if (!response || response.error) {
console.error(response.error);
} else {
console.log(response.id);
}
});
And it works if I use a specific location id. But I can't get the location id from a specific page.
Please give me some ideas. Thank you!
I was falling asleep, but I got one idea.
And finally, that idea was the solution:
It is possible to use the fan page id directly in the place parameter. It can be used like a place id.

facebook api not returning email

I am trying to return user email address using facebook api. Few days ago it was working fine, but today stopped working, and don't know why.
So first I get access token:
FB.login(response => {
if (response.authResponse) {
resolve(response.authResponse.accessToken);
} else {
reject({
error: 'User cancelled login or did not fully authorize.'
});
}
}, {
scope: 'public_profile,email'
});
then I try to get user data
FB.api('/me', {fields: 'first_name,last_name,email'}, function(response) {
console.log(response);
});
but I only get data for first_name and last_name. There is no email.
Also when I ask for permissions, it gives me email as granted, but as I said, email is not returned.
FB.api('/me/permissions', function(response) {
console.log(response);
});
Does anyone knows what can be the problem?
BTW: I am using API v2.4.
FB.api('/me?fields=email,name', function(response) { /* stuff here */ });
Try adding your return fields to your request.
I know this is old question however if someone still seraching for answers then following workaround worked for me.
You need to add email in scope scope="public_profile,email".
<fb:login-button scope="public_profile,email" autologoutlink="false" onlogin="OnRequestPermission();"></fb:login-button>
A possible cause is that the user did not validate the email. Try with another user with validated email to make sure this is not the problem.
See also this answer
If you set all code correctly and still don't get an email from API. The user probably doesn't have an email bound to the account or the email not confirmed by the user.
I made it work using a promise and adding {scope:'email'} to the initial request then including 'email' in the fb.api fields.
fb.login({scope:'email'}).then(function (resp) {
if (resp.status === 'connected') {
fb.api('/me', 'get', {fields:'id,email,first_name,gender,...'}).then(function (FbUserData) {
console.log(FbUserData);
});
}
});
I spent a lot of time trying to figure out why the email is not returned on the FacebookLoginASPnetWebForms Asp.Net application (https://github.com/nickpinheiro/FacebookLoginASPnetWebForms).
I got it working, thanks to N Nem's post above (thanks!).
Open Default.aspx.cs
In HyperLink1.NaviagteUrl, add the query string parameter: &scope=public_profile,email
HyperLink1.NavigateUrl = "https://www.facebook.com/v2.4/dialog/oauth/?client_id=" + ConfigurationManager.AppSettings["FacebookAppId"] + "&redirect_uri=http://" + Request.ServerVariables["SERVER_NAME"] + ":" + Request.ServerVariables["SERVER_PORT"] + "/account/user.aspx&response_type=code&state=1&scope=public_profile,email";
I also had to fix some bugs to parse the access token correctly.
This time, the email address is available! I hope this helps?
N Nem above fixed for me. Had the code correct, but missing the scope on the login button. Code I use is below.
function statusChangeCallback(response){
if(response.status === 'connected'){
registerUser(response);
}
}
function checkLoginState() {
FB.getLoginStatus(function(response) {
statusChangeCallback(response);
});
}
function registerUser(statusResponse){
let userId = statusResponse.authResponse.userID;
FB.api('/'+userId+'/?fields=id,email,first_name,last_name',function(response){
if(response && !response.error){
console.log(response);
}
});
}
This is kinda old post but I am sharing the solution that worked for me.
FB Login SDK version: v6.0
First, make sure that the user (which is you for testing purposes) has a valid/verified email. Refer to the code below.
FB.login(response => {
// your code
}, {
scope: 'public_profile,email',
return_scopes: true,
auth_type: 'rerequest'
});
And in getting the user details
const response = await axios({
method: 'get',
url: 'https://graph.facebook.com/v6.0/me?fields=id,name,email&access_token=ACCESS_TOKEN&pretty=0&sdk=joey&suppress_http_code=1'
});
I've added the parameter fields=id,name,email

How to get the post of the user using javascript sdk

I am creating my first facebook app using javascript sdk. In that i can able to post to the user's wall using(FB.api('me/feed', 'post', )).
Now what I need is, when the user accessing my app, it has to show (or list) the post of that user. The code is
FB.api('/me/posts', function(response) {
for (var i=0, l=response.length; i<l; i++) {
var post = response[i];
alert('The value of post is:'+post);
if (post.message) {
alert('Message: ' + post.message);
} else if (post.attachment && post.attachment.name) {
alert('Attachment: ' + post.attachment.name);
}
}
});
But it is not working. If I remove l=response.length and change the condition as i<5 it is going inside the loop but it gives the value of post is undefined.
I didn't get why it is returning as undefined. It returns same for post.message also.
I am getting the access_token of the user also.
If I want to get the post of my user what code i have to use. Can anyone help me in this.
Thanks in advance
You can use the Facebook Graph API Explorer to better understand what data you will be receiving. https://developers.facebook.com/tools/explorer/
You should have l=response.data.length and var post = response.data[i];. Also, some posts will not have a "message", but will have a "story" instead (mainly posts like "Joe Smith and Tom Someone are now friends."). Make sure you also have the appropriate permissions (e.g. user_status) for the information you're trying to receive.

FB.api wallpost link URL changes when it contains Facebooks TLD

Hi Im using this script to post:
postToFb:function () {
//alert(window.FbPostParameters.link);
FB.api('/me/feed', 'post', window.FbPostParameters, function (response) {
if (!response || response.error) {
console.log('/me/feed - Error occured');
} else {
console.log('/me/feed - Published to stream!');
}
});
},
And alerted window.FbPostParameters.link is:
http://www.facebook.com/FBPAGENAME/app_APPID?app_data=forum
Its funny, cuz when I checked the wallpost the url has become this:
http://www.facebook.com/FBPAGENAME?ref=hl&sk=app_APPID%26app_data%3Dforum
The problem is that the posted url is fully functional (and fb passes app_data to application) but the latter one is not working. Not even renders the application tab until I remove the %26app_data%3Dforum part.
I could not find any resource to debug this problem. Can you help me?
ps: encodeURI or encodeURIcomponent results the same.
UPDATE: link to example: http://www.facebook.com/kenivajszmuller/app_238038449651632
Can anyone help? ty

FB.api links.get can't work with short name accounts

I use this code:
FB.api({
method: 'links.get',
uid: user_uid,
limit: 100
},function(response){
var response = response[0];
if(response){
console.log(response);
}
});
I can't get response from callback of this function, if people with mentioned use short name( instead of ?id=100000000).
I'm getting user id using this code.
But it works only if user doesn't have short name:
FB.Event.subscribe('auth.login',function(response){
user_id = response.authResponse.userID;
});
But it works only if user doesn't have short name.
How can i listen event from people with a short name?
Thank you.
Name has nothing to do with it. response.authResponse.userID will always return the user_id not user_name. You must be doing something else wrong.