So I am successfully getting some info from FB, like Likes, and Friends, but I cannot figure out what is the 'code' for 'About Me'?
Here is my code for Fb Likes and Friends:
$.getJSON('https://graph.facebook.com/' + authData.facebook.id + '/friends?access_token=' + authData.facebook.accessToken +'', function (data) {
console.log(data);
var fbFriends = data;
});
$.getJSON('https://graph.facebook.com/' + authData.facebook.id + '/likes?access_token=' + authData.facebook.accessToken +'', function (data) {
console.log(data);
var fbLikes = data;
});
So, what should I put instead of "/friends?" or "/likes?" to get About Me? Btw, is there any list of those things?
Thanks,
uksz
EDIT:
I permission that I ask user for are the following: user_friends,user_about_me,user_likes,public_profile,publish_actions
You should have a look at the official Facebook docs:
https://developers.facebook.com/docs/graph-api/reference/user
Try /me?fields=friends,likes,about
Alright, I managed to work-around to this problem by simply:
$.getJSON('https://graph.facebook.com/' + authData.facebook.id + '/?access_token=' + authData.facebook.accessToken +'', function (data) {
var about = data.bio;
})
Related
I have a web application that allows users to post custom stories to their Facebook timeline with the list of fitness exercises they have performed.
The first version is looping through the exercises and calling FP.api for each exercise and it works fine.
Now I would like to make a single call to FB.api with a batch request to speed up the posting and that's where I'm having trouble.
Here is the code with the loop that works fine (exids is an array of numbers):
function postToFB(exids)
{
fbi = 0;
fblength = exids.length;
for (var i = 0; i < fblength; i++)
{
FB.api(
'me/' + vitNamespace + ':perform',
'post',
{
exercise: "http://www.vitalclub.net/scripts/getExforFB.php?exid=" + exids[i],
'fb:explicitly_shared': true
},
function(response) {
...
});
}
}
and here is the code with the batch request that returns an error:
function postToFB(exids)
{
var batcharr = [];
for (var i = 0; i < exids.length; i++)
batcharr.push({ method: 'post', relative_url: 'me/' + vitNamespace + ':perform', body: "exercice=http://www.vitalclub.net/scripts/getExforFB.php%3Fexid%3D" + exids[i] + "&fb:explicitly_shared=true" });
FB.api(
'/',
'post',
{ batch: batcharr, include_headers: false },
function(response) {
...
});
}
The error I get (for each exercise) is the following: The action you're trying to publish is invalid because it does not specify any reference objects. At least one of the following properties must be specified: exercise.
I presume this has to do with the way the body in the batch request is formatted but I cannot find the right way to format it. I have tried using encodeURIComponent on the URL representing the exercise but the error is the same.
Anybody has an idea of what the problem is?
Thanks,
Jean
OK, my bad. It was really a stupid error. I had written the parameter "exercise" in French instead of English (so "exercice" instead of "exercise") and that's where the problem was.
I'm also now using $.param to format the parameters so I now have:
var batcharr = [];
var opts;
for (var i = 0; i < exids.length; i++)
{
opts = { exercise: "http://www.vitalclub.net/scripts/getExforFB.php?exid=" + exids[i], 'fb:explicitly_shared': true };
batcharr.push({ method: 'post', relative_url: 'me/' + vitNamespace + ':perform', body: $.param(opts) });
}
before calling FB.api and it works like a charm!
I am creating a facebook application generator. And I need to check if the user has currently added the application on this facebook page or not.
In order to do that, i first request facebook api to give a list of his pages. Then i loop through all the pages. And request for apps on each of these pages.
Lastly i compare the appId with the one user just created and displays the display the warning accordingly.
The problem is , when i loop through each of the pageId and request FbApi for subpages, the request response is delayed and the for each loop completes its cycle before the results are fetched from facebook.
Here is my code, which is quite complex... Any ideas to fix the issue is highly appreciated.
FB.login(function (response) {
FB.api('/me/accounts', function (apiresponse) {
var totalPages = apiresponse.data.length;
var pageIndex = 0;
$.each(apiresponse.data, function (pageNumber, pageData) {
var pageAccessToken = pageData.access_token;
var tabPageName = pageData.name;
var tabPageId = pageData.id;
FB.api("/" + tabPageId + "/tabs", function (response) {
var foundApp = false
$.each(response.data, function (index, value) {
var exsistingAppId = (value.id).split("app_").pop();
if (exsistingAppId == fbAppId) {
foundApp = true;
}
});
if (foundApp === true) {
var data = {
PageId: tabPageId,
Url: window.location.href.split("/").pop()
}
$.ajax({
type: "POST",
url: '/facebook/Match',
contentType: "application/json",
data: JSON.stringify(data),
success: function (data) {
if (data == "True") {
$("#addToFacebookModal ul").append("<li><span class='pageTab'><a class='pageTabName' target='_blank' href='https://facebook.com/profile.php?id=" +tabPageId + "'>" +tabPageName + "</a></span><a class='deleteAppFromPageTab' data-id='" +tabPageId + "' data-accessToken='" +pageAccessToken + "'>[x]</a></li>");
alreadyAdded.push(true);
} else {
alreadyAdded.push(false);
}
pageIndex++;
if (pageIndex == totalPages) {
console.log("Total Pages = " + totalPages + ", Looped through = " + alreadyAdded.length);
if (alreadyAdded.indexOf(true) >= 0) {
$("#addToFacebookModal").modal();
} else {
addToFacebook();
}
}
}
});
}
else {
pageIndex++;
}
});
});
});
}, { scope: 'manage_pages' });
Here is pseudocode of what i am doing
var myVariable
-Fb.Api Callback function returns response array
-Loop through the response array
-Get new Response array based on the previous response in that array
-Loop through each item of the new response array and compare it with myVariable.
The problem is that responses are delayed while the loop finishes up before the responses arrive. As i result i cant compare the nested Item with myVariable.
If by "facebook page" you mean a business page / organization page (not a profile), you can get the same information more easily by checking the endpoint '/{{page_id}}/tabs/{{app_id}}.
Replace page_id with the ID of the page you want to check and app_id similarly with your app ID. I don't have working code at the moment, but something like this:
FB.api(
'/' + checkPageID + '/tabs/{{app_id}}',
function (response) {
// Do console.log(response) to figure out how to see if installed or not
}
)
YOu can use fields expansion:
https://developers.facebook.com/docs/graph-api/using-graph-api/v2.3#fieldexpansion
FB.api('/me/accounts', {fields: 'name, address{city}'},function (response)
{
//do something here.
}
I'd like to (asynchronously) fetch model-data each time a page is shown. The path may have additional parameters telling the withOnShow-handler what fields to fetch, for example:
..#!/navigation/href_with_params/users/123?fields=a,b,c,d
How would you access the "fields"-param inside the withOnShow handler?
Cool.loadUser = function(callback, page) {
$.get('users/' + page.currentId + "?fields=" + <fields>, function(data) {
callback(ko.mapping.fromJSON(data));
});
};
you could do something like
var fields = page.ctx.field();
inside your withOnShow. Hope this helps.
when i want to get to
http://www.koran-auf-deutsch.de/koran-deutsch/23-die-glaubigen-al-mominun/
and just enter
http://www.koran-auf-deutsch.de/koran-deutsch/23
i get directly to the url. i would like to get a similar behaviour
in my angular app, where would you inject that functionality? any ideas?
angular.module('app', []).
config(['$routeProvider', function($routeProvider) {
$routeProvider.
when('/:id', {controller: RedirectCtrl}).
when('/:id/:title', {templateUrl: 'partials/post.html', controller: PostCtrl}).
otherwise({redirectTo: '/404'});
}]);
function RedirectCtrl($routeParam, $http) {
var post_id = $routeParams.id;
// get title by id
$http.get('/api_url_to_get_title_by_id').success(function (data) {
window.location = "/" + post_id + "/" + data.title;
});
}
RedirectCtrl.$inject = ['$routeParams', '$http'];
I'm having a little trouble here. I'm not good at javascript and all.
The problem is that I'm trying to post to some Facebook users using FB.api. However, it only works if it's only one friend at a time.
Here's my code:
FB.api({ method: 'friends.get' }, function(result) {
var user_ids="" ;
var totalFriends = result.length;
var randNo = Math.floor(Math.random() * totalFriends);
var numFriends = result ? Math.min(1,totalFriends) : 1;
if (numFriends > 0) {
for (var i=0; i<numFriends; i++) {
user_ids+= (',' + result[randNo]);
randNo ++;
if(randNo >= totalFriends){
randNo = 0;
}
}
}
FB.api(user_ids + '/feed', 'post', { message: txt2send },function(response) {
if (!response || response.error) {
alert('Error occured');
} else {
alert('Post ID: ' + response.id);
}
});
});
The output of user_ids is looking like this: ,10083461349,100082391,19293822
Hope you can help me solve this. And please don't refer me any links to help, trust me, I've tried everything.
Hope you can help me solve this. And please don't refer me any links to help, trust me, I've tried everything.
Yeah, sure. But instead of sticking to what the docs say, you’re trying to invent your own “syntax” – you really think that’s helpful …?
Accessing the Graph API generally works by making HTTP requests to /someid/someendpoint/.
But what you are trying, is to (eventually) make a request to
/,10083461349,100082391,19293822/feed
– which is just complete and utter nonsense. You just can’t access the Graph API listing multiple ids at once.
If you want to post to several user’s feeds, you have to make one API call for each one of these users.