how to get my facebook wall with JS SDK - facebook

I'm testing a simple code to retrieve the last 5 posts from my personal facebook wall, then add them to the html with my personal css style. But I get only empty data! I'm using Graph API 2.3, on a web page.
I've created an app, called "cambiamentico". So I got the appid 421429864732436 and the secure code. My personal id, istead, is 100000007731737. NB - the app is not yet submitted for review, since it's only a test and doesn't work. Is it a problem?
If I ask, for example, "cocacolait/posts?limit=5", I get the 5 complete posts of that Page, but when I ask "100000007731737/?limit=5" I get only an empty data.
I guess there's some issue with permissions, maybe something called "access token" which I really don't know how to "make"... or find.
Here's the code:
window.fbAsyncInit = function() {
FB.init({
appId : '421429864732436',
xfbml : true,
version : 'v2.3'
});
testGetPosts();
};
(function(d, s, id){
var js, fjs = d.getElementsByTagName(s)[0];
if (d.getElementById(id)) {return;}
js = d.createElement(s); js.id = id;
js.src = "//connect.facebook.net/it_IT/sdk.js";
fjs.parentNode.insertBefore(js, fjs);
}(document, 'script', 'facebook-jssdk'));
function testGetPosts(){
//this work
FB.api('/cocacolait/posts?limit=5', function(response){
console.log(response);
});
//this, my private page (with public posts), doesn't work
FB.api('/100000007731737/posts?limit=5', function(response){
console.log(response);
});
}

Since you are fetching your own posts your app doesn't need to go through review.
And as you mentioned you are indeed missing an access token. If you are to fetch your posts from your Facebook wall you will need to make request to graph.facebook.com/me/feed?access_token = "access_token with read_stream permission"
To get access token go through: https://developers.facebook.com/docs/facebook-login/access-tokens#usertokens

Related

FB.Event.subscribe("comment.create") fires but returns 500 when commenting first successful when replying

FB comment plugin gives 500 error. FB.Event.Subscribe("comment.create',callback) is fired but does not return successfully instead throws a 500 error.However,this only happens on the first comment.The post still goes through and appears as comment on page reload.
However,on the same post,when replying to the posted comment,it fires the event without any errors.
I am using facebook comment plugin and my setup is that i create dynamic url by getting data from the database and then create url based on that on front end.I am using knockout on the front end and flask at the backend.My goal here is to utilise FB comment plugin as threaded comment on each post.I am triggering push notifications when someone comments on the post by looking at the id of the post.
//HTML
<div data-bind="text:$data.location,
click:window.animateMarkerOnClick.bind($data), attr: { id:
$data.key,class:'search-list' }"></div>
<div id="listing-msg" >
</div>
<div class="fb-comments" data-bind='attr:{"href":
"http://localhost:8080/postit/get_share_listings/#" +
$data.key,"id":$data.key}' data-width="320" data-numposts="1" data-
colorscheme = "dark" notify = "true" data-order-by = "reverse_time"></div>
//JS EVENT SUBSCRIPTION
window.fbAsyncInit = function() {
FB.init({
appId : 'xxxxxxx',
autoLogAppEvents : true,
xfbml : true,
version : 'v3.2'
});
FB.Event.subscribe("comment.create", push)
};
(function(d, s, id){
var js, fjs = d.getElementsByTagName(s)[0];
if (d.getElementById(id)) {return;}
js = d.createElement(s); js.id = id;
js.src = "https://connect.facebook.net/en_US/sdk.js";
fjs.parentNode.insertBefore(js, fjs);
}(document, 'script', 'facebook-jssdk'));
//callback
function push(){
$.ajax({
type: "POST",
url: "http://localhost:8080/postit/push",
contentType: 'application/json;charset=UTF-8',
data:JSON.stringify({"data":document.activeElement.
parentElement.parentElement.parentElement.children[0].id})
})
}
As already stated,the event triggers callback but on first comment facebook
triggers url handler createComment which gives 500 but createReply successfully triggers the callback.
The comment.create event does not exist any more in the JS SDK.
https://developers.facebook.com/docs/reference/javascript/FB.Event.subscribe/v3.2 does not mention it any more, when you click the down arrow next to event, and https://developers.facebook.com/support/bugs/927463134113943/?comment_id=930637043796552 confirms it is gone: “comment.create is officially killed.”
You need to use server-side webhooks now, if you want your app to get notified about new comments made via the comments plugin, see https://developers.facebook.com/docs/graph-api/webhooks/reference/application/#plugin_comment

Facebook login returning nothing

I looked all over and I can't seem to find the solution to this. I am trying to implement Facebook login to my website and I am having one hell of a time doing so.
I have implemented the Facebook graph api and I have the login part built in but I can't get anything to return from it to handle it.
You can see what I mean on < phiride.com > (I don't know if that's bad netiquette to post the url on this website, if so sorry!)
I used the code given to get a return -->
<script>
FB.api('/me', function(response) {
console.log(JSON.stringify(response));
});
</script>
Absolutely nothing is printed to the screen and I can't find anyway to handle the information. I'm sure this is a simple beginner mistake but any help would be greatly appreciated.
Did you generate the html and script via their website.
https://developers.facebook.com/docs/plugins/login-button
This is what you need to do in order to make API calls work:
Initialize the JavaScript SDK
Authorize the user
Initialize the JavaScript SDK
<script>
window.fbAsyncInit = function() {
FB.init({
appId : 'your-app-id',
xfbml : true,
version : 'v2.1'
});
};
(function(d, s, id){
var js, fjs = d.getElementsByTagName(s)[0];
if (d.getElementById(id)) {return;}
js = d.createElement(s); js.id = id;
js.src = "//connect.facebook.net/en_US/sdk.js";
fjs.parentNode.insertBefore(js, fjs);
}(document, 'script', 'facebook-jssdk'));
</script>
Authorize the user
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 + '.');
console.log('Your Email: ' + response.email);
}, {scope: 'email'});
} else {
console.log('User cancelled login or did not fully authorize.');
}
});
Only call FB.login on user interaction (on click) to make sure it does not get blocked by the browser - it has to get called AFTER FB.init too. You can also check if a user is already authorized by using FB.getLoginStatus right after FB.init.
Sources:
https://developers.facebook.com/docs/javascript/quickstart/v2.1
https://developers.facebook.com/docs/reference/javascript/FB.login/v2.1
https://developers.facebook.com/docs/reference/javascript/FB.getLoginStatus

facebook share dialog (v2.0) does not promote user to login?

I am implementing a custom facebook share button with the new Facebook javascript SDK (v2.0 according to the docs in the developer center)
window.fbAsyncInit = function () {
FB.init({
appId: 'xxxxxxx',
xfbml: true,
version: 'v2.0'
});
};
(function (d, s, id) {
var js, fjs = d.getElementsByTagName(s)[0];
if (d.getElementById(id)) { return; }
js = d.createElement(s); js.id = id;
js.src = "//connect.facebook.net/en_US/sdk.js";
fjs.parentNode.insertBefore(js, fjs);
}(document, 'script', 'facebook-jssdk'));
function shareToFacebook(url) {
var obj = {
method: 'share',
href: url,
};
function callback(response) { }
FB.ui(obj, callback);
}
$('#mb-facebook').click(function () {
shareToFacebook('http://some-url-to-share');
return false;
});
Now everything works fine exception one issue:
If I have logged into my facebook account before clicking the share to facebook button, then a popup window contains the expected facebook sharing form showed up, but if I do not log into my facebook account then click the share button, the a blank popup window contains nothing shows up, no promotion to log into facebook etc.
I do remember that I've done this before in another project that the share button will promote the user to log into facebook if he has not logged in yet.
So my question is : does facebook changed this behavior in the new SDK or I have missed something else?
Thanks in advance!

getLoginStatus always returns not_authorized

I am new to developing websites with Facebook SDK. So please bear with me.
Below is my simple code for verifying that I am logged into Facebook.
For some reason I always get the 'not_autherized' response, even though I am the only developer of my app. The app number I provide is correct.
<!DOCTYPE html>
<html lang="en">
<head>
</head>
<body>
<div id="fb-root"></div>
<script>
window.fbAsyncInit = function() {
// init the FB JS SDK
FB.init({
appId : 'censored app id', // App ID from the app dashboard
status : true, // Check Facebook Login status
cookies : true,
xfbml : true // Look for social plugins on the page
});
FB.getLoginStatus(checkLoginStatus);
function checkLoginStatus(response) {
if (response && response == 'connected') {
alert('User is authorized!');
} else {
alert('User not authorized!!!');
}
};
// Additional initialization code such as adding Event Listeners goes here
};
// Load the SDK asynchronously
(function(d, s, id) {
if (d.getElementById(id)) return;
var js = d.createElement(s); js.id = id;
js.src = "//connect.facebook.net/en_US/all.js";
var fjs = d.getElementsByTagName(s)[0];
fjs.parentNode.insertBefore(js, fjs);
}(document, 'script', 'facebook-jssdk'));
</script>
</body>
</html>
Is there anything I have overlooked?
Much appreciated :)
A friend helped me with this. He very helpfully pointed out something that Facebook has neglected to document.
When you create a new facebook application, then it is NOT automatically authorized by the admin and developers. An authorized application is one that exists in the "Account Settings->Apps" list. If it is not in this list, then it is not authorized.
This means that you need to call FB.login() at some point in your code in order to popup the user authorization window.
Mind you, this should be called from a button. Otherwise, the popup could be blocked.
Hope this helps someone else but me.
Cheers :)

Facebook: "An access token is required to request this resource" in canvas only

The Facebook Javascript API fails in the canvas but works fine when I access the app directly. I get the "An access token is required to request this resource" error whenever I try to make a facebook query. I tried getLoginStatus but the callback never gets called in the canvas. However it does get called in the direct webpage. See this code:
window.fbAsyncInit = function() {
FB.init({
appId : '{{ app_id }}', // App ID
status : true, // check login status
cookie : true, // enable cookies to allow the server to access the session
xfbml : true, // parse XFBML
frictionlessRequests : true
});
console.log("INIT");
FB.getLoginStatus(function(response) {
alert("GOT STATUS");
alert(response);
},true);
};
(function(d, s, id) {
var js, fjs = d.getElementsByTagName(s)[0];
if (d.getElementById(id)) return;
js = d.createElement(s); js.id = id;
js.src = "//connect.facebook.net/en_US/all.js";
fjs.parentNode.insertBefore(js, fjs);
}(document, 'script', 'facebook-jssdk'));
"INIT" is always output, but the alerts are only called when I access the page directly.
Edit: Here is the problematic app: https://apps.facebook.com/quirkyversaries/
The problem occurs when you try to create a new occasion and search for friends. The search fails due to the issue explained above. It also fails when you try to create a page occasion as the pages will not load.
1) the app will either invoke the callback function, or it will redirect to the redirect_url
so, in case you have specified the redirect_url, kindly remove
2) according to the docs
you must subsscribe to the event auth.statuschange
FB.Event.subscribe('auth.statusChange', function(response) {
// do something with response
});
it is a bad practice to include a true at the end of the getLoginStatus function as you are invoking it on every page load. this true forces a roundtrip call. Since you have status:true in fb.init, you may not need that