Facebook API: send message to friends works for only test/admin users - facebook

I'm working on a website, as part of that we are allowing users to invite their friends. Using following code
// assume we are already logged in
FB.init({appId: 'xxxxxxxx49030', xfbml: true, cookie: true});
function sendInvites(fb_ids) {
var link_url = "<?php echo base_url()?>invited/?code=100627510332492694";
FB.ui({
method: "send",
to: fb_ids,
name: "Join example.com",
description: "hello",
picture: "http://www.example.com/images/logo-orange.png",
link: link_url
}, function(response) {
alert(response)
if (!response){
return;
}
$.post("/invite/new/", {
fb_ids: fb_ids
}, function(data) {
if (data.status == "success") {
alert(data.status);
} else {
alert(data.message);
}
}, "json");
});
}
Don't understand what's missing.

Fbids should be an array and i missed it. It's working now.

Related

API Error Description: Session key invalid or no longer valid (error 102)

I cant figure out why facebook publish is not working on this site
I get
API Error Code: 102
API Error Description: Session key invalid or no longer valid
Error Message: Iframe dialogs must be called with a session key
While calling the stream.publish method as iframe
<script type="text/javascript">
(function() {
var e = document.createElement('script'); e.async = true;
e.src = document.location.protocol
+ '//connect.facebook.net/fr_FR/all.js';
document.getElementById('fb-root').appendChild(e);
}());
window.fbAsyncInit = function()
{
FB.init({ appId: '303380259758621',
status: true,
channelUrl: 'http://www.crabegame.com/channel.php',
cookie: true,
xfbml: true,
oauth: true});
}
function PublishStream(score)
{
FB.ui(
{
method: 'stream.publish',
display : 'iframe',
message : '',
attachment:
{
name: 'CrabeGame',
caption: 'Essaye de battre mon score sur le Crabe Game !',
description: "J'ai réalisé un score de " + score + "points au Crabe Game !",
href: 'http://www.crabegame.com',
media:
[
{
type: 'image',
src: 'http://crabegame.com/media/crabe_fb.png',
href: 'http://www.crabegame.com'
}
]
},
action_links:
[
{
text: 'Play Crabe Game',
href: 'http://www.crabe-game.com'
}
],
user_message_prompt: 'Publier sur votre mur'
},
function (response)
{
if (response && response.post_id)
{
}
}
);
}
</script>
This means that you have no current user. Try this in order to call PublishStream:
FB.login(function(response) {
if (response.authResponse) {
PublishStream();
} else {
console.log('Not logged in');
}
});
you don´t need a logged in user, just remove display: "iframe", worked for me. it will still show in an iframe, but without error. don´t ask me why, with 1:1 the same app settings it works WITH the display value in another app of mine...at least that worked for me with the apprequest dialog, might be the same here.
and i would use "feed" instead of "stream.publish".
see here: https://developers.facebook.com/docs/reference/dialogs/feed/
also: Using Like Button and FB.ui (apprequests) On the Same Page Conflicts

FB.api('/me/likes/124745260879931', onMyLikesResponse); not working on IE 9

any reason why the below code would not work in IE9 yet work in every other browser i try?:
FB.init({appId: '######', status: true, cookie: true, xfbml: true});
FB.api('/me/likes/######', onMyLikesResponse);
function onMyLikesResponse(response)
{
console.log("length" + response.data.length);
console.log(response);
if(response.data.length==1){
$('#like').show();
}
}
FB.login(function(response) {
if (response.status == 'connected') {
var page_id = "40796308305";
FB.api('/me/likes/'+page_id, function(response) {
if (response.data[0]) {
$('#like').show();
}
});
} else {
// user is not logged in
}
});
I think length will not work on json response. Make sure developer tools are open in IE9, cause you are using console.log.

Check if Facebook JS-API app already has been given permission

I have a small script that unlocks some content on a page when a user logs in via Facebook and posts some information on their wall.
I want to avoid any duplicate posts (spam) in the event the user visits the page a second time and repeats the process.
Is there a way of (when asking for the permissions) to tell if they have already been given and therefore skip the sharing part of the script?
Here's my code:
$('.preview a').click(function(e) {
e.preventDefault();
FB.login(function(response) {
if (response.session) {
FB.api('/me/feed', 'post', {
message: 'Just unlocked the exclusive preview of...',
picture: 'http://website.com/share.png',
link: 'http://www.website.com',
name: 'Name goes here',
description: 'Description here'
},
function(response) {
if (!response || response.error) {
alert(response.error);
} else {
$('.preview').hide();
$('.clip').show();
}
});
}
},
{perms:'read_stream,publish_stream'});
});
From 2011 to 2014 a lot of things change..
I made this solution to check for "user_friends" and "publish_actions" permissions, if not allowed the both, force the user to "re-autenticate". The callback method will only be called when all permissions are given.
function login(cb,forceAuth) {
FB.getLoginStatus(function (response) {
if (response.status !== 'connected' || forceAuth==true){
FB.login(function (response) {
checkPermissions(cb,false);
}, {scope: 'publish_actions,user_friends'});
} else {
checkPermissions(cb);
}
});
}
function checkPermissions(cb,forceAuth){
FB.api(
"/me/permissions",
function (response) {
if (response.data[0]['publish_actions']==undefined || response.data[0]['publish_actions']==0 ||
response.data[0]['user_friends']==undefined || response.data[0]['user_friends']==0) {
if (forceAuth!=false)
login(cb,true);
} else {
cb();
}
}
);
}
How to use:
login(function () {
myLogedAndAllowedFunction();
});
I've written a tutorial about this here, and here's an example:
FB.api({ method: 'fql.query', query: 'SELECT read_stream,offline_access,publish_stream FROM permissions WHERE uid=me()' }, function(resp) {
for(var key in resp[0]) {
if(resp[0][key] === "1")
console.log(key+' is granted')
else
console.log(key+' is not granted')
}
});
There's also a JS REST example there in the article just for reference.

Facebook authorization in C#

How do I implement a Facebook authorization? I have no idea where to start. I have seen numerous examples in PHP but none in C#.
Start here: http://developers.facebook.com
If you can use the Facebook javascript sdk (Much easier IMO, and you have asp.net as a tag so i am making assumptions) you could try something like this:
//Facebook iFrame include
window.fbAsyncInit = function () {
FB.init({ appId: YourID, status: true, cookie: true, xfbml: true });
FB.Canvas.setAutoResize();
authorize();
}
/*
* Facebook Authorization
*/
function authorize (){
FB.getLoginStatus(function (response) {
if (response.session) {
// logged in and connected user, carry on
} else {
// no user session available, Lets ask for perms
FB.ui(
{
method: 'permissions.request',
perms: your permissions
},
function (response) {
if (response && response.session != null) {
//User accepted permissions
} else {
//User did not accept permissions
}
});
}
});
}

Publish to Current user's wall using FBJS in FBML application

I need to publish some message to the current user's wall using FBJS in FBML application.
When I use
window.fbAsyncInit = function() {
FB.init({appId: 'MY_APP_ID', status: true, cookie: true, xfbml: false});
};
i am getting an error :- FB is not defined.
and
window is not defined.
For publishing i am using this code
function graphStreamPublish(){
var body = document.getElementById("txtTextToPublish").value;
FB.api('/me/feed', 'post', { message: body }, function(response) {
if (!response || response.error) {
// alert('Error occured');
} else {
// alert('Post ID: ' + response.id);
}
});
}
(I cannot use alert in facebook..)
Thanks in advance..
If you're using FBML, you should be using FBJS instead of the Facebook Connect API.
var body = document.getElementById('txtTextToPublish').getValue();
Facebook.streamPublish(body);
Note the getValue() call instead of .value due to use of FBJS.