Reputation: 3802
http://www.independent.co.uk/ and http://www.guardian.co.uk/ have an application where by if you read an article on their site, it writes to your profile/timeline that "X has read Y on Z".
Now I can write to the timeline with a button present. I can also do it automatically (but this happens each time they go to the article which I don't need). However what I need to add is some logic as shown below
Does this make sense? Any examples of this around? So basically pulishing to the timeline isn't an issue, it's just the various checks I need to put into place.
EDIT
Ok, for some reason this works (for checking of they are authorised or not) but if I take out the alert('test');
part, it doesn't work. Any ideas?
<script>(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_GB/all.js#xfbml=1&appId=123456789";
fjs.parentNode.insertBefore(js, fjs);
}(document, 'script', 'facebook-jssdk'));
alert('test');
FB.init({
appId:'123456789', cookie:true,
status:true, xfbml:true, oauth:true
});
FB.getLoginStatus(function(response) {
if (response.status === 'connected') {
var uid = response.authResponse.userID;
var accessToken = response.authResponse.accessToken;
alert('Logged in and connected');
$(read);
} else if (response.status === 'not_authorized') {
alert('Logged into FB but not authorised');
} else {
alert('Not logged into FB');
}
});
</script>
Ok, got it partly working. There are some slight niggles but main issue now is to check whether they have posted a specific action to the timeline already. If they have then don't post another e.g. So when they go to an article first time, the status "Jon has read an article on the Site" gets added. However if they go to it again, it doesn't get added
Upvotes: 2
Views: 1029
Reputation: 38046
Your code only works with the alert due to a race condition in your code.
You are loading the SDK asynchronously (which you should), but then try to use it synchronously by calling FB.init
immediately.
You need to define a function fbAsyncInit
, in which you call FB.init
. This will ensure that you don't try to call FB.init
before the SDK is loaded.
Other than that, this is all about getting the right permissions from the user, and following Facebooks policies. Technically it's all pretty basic stuff.
Upvotes: 0
Reputation: 2434
This is against the Facebook Platform Policy. See section IV part 3:
If a user grants you a publishing permission, you must still obtain consent from the user before taking any action on the user's behalf, such as publishing content or creating an event.
You can not post to the user's timeline\wall without first asking them permission. The Independent and The Guardian have a unique arrangement with Facebook that exempts them from these restrictions.
This is the kind fo thing that will get you a strongly worded email warning, or more severely, a developer account suspension.
Its always good to have a look at the Platform Policies and Promotions Checklist before starting a new project.
Upvotes: 1