Send method of FB.ui does not show multiple recipients - facebook

I created an app in Facebook app center and wanted to use this code for sending a message to multiple users. I checked my code in Javascript test console and worked properly but when pasted in my page it just showed the first recipient.
<script>
var arr = [];
arr.push(4);
arr.push(5);
FB.getLoginStatus(function (response) {
FB.ui(
{
method: 'send',
to: arr ,
link: "http://en.wikipedia.org/wiki/Apadana",
picture: "http://upload.wikimedia.org/wikipedia/commons/thumb/5/55/Takhteh_Jamshid.jpg/200px-Takhteh_Jamshid.jpg",
description:"test me",
display: 'iframe'
},
function (param) {
if (!param.success){
// do smthing
}
else{
//throw Err
}
});
});
And also I initiated by these paramethers:
appid = '370960029693800';
channelurl = 'localhost';
window.fbAsyncInit = function () {
FB.init({
appId: appid,
channelUrl: channelurl,
status: true,
cookie: true,
xfbml: true
});
As I said it works in javascript test console and seems something is wrong in initialization part. Please advise me which part of codes is wrong.

Related

Cordova Facebook Connect - can't get userID

I added to phonegap app Facebook Connect,
It works fine and generates Access Token, But when I'm trying to get the userID the value is
"Undenified"
my function:
FB.init({
appId: "************",
nativeInterface:CDV.FB,
xfbml: true,
useCachedDialogs: false,
status: true,
cookie: true,
oauth: true
});
//alert("Init Done");
FB.getLoginStatus(function (response) {
if (response.authResponse) {
if (response.status == 'connected') {
Token = response.authResponse.accessToken;// This one is Good!
userid = response.authResponse.userID;// This one is Bad
}
I use the following and it works fine:
FB.api('/me', {}, function (response) {
fbuserid = response.userid;
});
This gives you the user data. You can also pass the scope just to get what you want like email, userid, etc...
Iterate through the keys to see what is stored
for (var key in response.authResponse) {
console.log(key);
console.log(response.authResponse[key]);
}

Facebook Feed Dialog does not open

I am building a custom multi friend selector in order to send posts to friend's walls on Facebook based on this tutorial
https://developers.facebook.com/docs/guides/games/custom-muti-friend-selector/
I have been able to load my list of friends and create a CSV list of people I want to post to but the last step of opening the feed dialog is failing and I don't understand why. The dialog does not open.
this is my sendRequest function
var sendUIDs = '';
$('.cbFriendId').each(function(index){
if($(this).prop('checked') == true){
var addId = $(this).attr('value');
sendUIDs += addId + ',';
}
});
console.log(sendUIDs);
openSendDialog(sendUIDs);
}
function openSendDialog(sendUIDs){
console.log('open send dialog');
FB.ui({
method: 'feed',
to: sendUIDs,
link: 'http://www.mydomain.com',
display: 'iframe',
message: 'Hello from My Domain',
name: 'Click to view my app',
caption: 'Hello my app'
},
function (response) {
if (response) {
alert('Post was published.');
console.log('Post was published.');
} else {
alert('Post was not published.');
console.log('Post was not published.');
}
}
);
}
The last console log entry is 'open send dialog'. I get no errors in IE9 dev tools but I do get an 'Unsafe Javascript attempt to access frame ...' error in chrome.
just for sanity sake here is my graph init
window.fbAsyncInit = function () {
FB.init({
appId: '462750593765445', // App ID
channelUrl: '//www.mydomain.com/channel.html', // Channel File
status: true, // check login status
cookie: true, // enable cookies to allow the server to access the session
xfbml: true, // parse XFBML
frictionlessRequests: false
});
};
Also I should mention that the app is in sandbox mode.
Thanks for the help
What you're trying to do is called Send Dialog, So you need to change your method from feed to send and it should work.
Check out their docs for more information: https://developers.facebook.com/docs/reference/dialogs/send/

Facebook FB.ui oauth dialog error

I'm having problems making a dialog box to ask for a user to log into my facebook tab to enter a contest.
We're trying to use the FB.ui oauth dialog box with the following code.
FB.ui({
client_id: '12345',
method: 'oauth',
scope: 'email,user_birthday',
response_type: 'token',
redirect_uri: "https://mydomain.com/test.php" // Change this to proper address
});
When we include the redirect_uri we get the following message
API Error Code: 100 API
Error Description: Invalid parameter
Error Message: The "redirect_uri" parameter cannot be used in conjunction
with the "next" parameter, which is deprecated.
We are not using the next parameter anywhere though, so not sure why it is saying that.
When we take away the redirect_uri we get the following message.
API Error Code: 191 API
Error Description: The specified URL is not owned by the application
Error Message: redirect_uri is not owned by the application.
I would use the FB.login method as opposed to this given you are operating within a tab application.
Within your FB.init:
window.fbAsyncInit = function(){
FB.init({
appId: 'your_app_id',
status: true,
cookie: true,
xfbml: true,
oauth: true
});
FB.Canvas.setAutoGrow(true);
};
FB.getLoginStatus(function(response){
if(response.status == 'connected'){
callyourfunctiontoentercompetition();
}else{
FB.login(function(response){
if(response.status == 'connected'){
callyourfunctiontoentercompetition();
}else{
handlethecancelpermissions();
}
});
}
});
Turns out there was a few things wrong, In my app settings I had to select "Website" and put in my site's URL, this allowed me to add the "app domain" and save it. This cleared up the 191 Error. Having website not checked off your App Domain will not save. After that it was rather simple.
Here the code that I used.
<script>
window.fbAsyncInit = function() {
FB.init({
appId : 'YOUR_ID',
status : true, // check login status
cookie : true, // enable cookies to allow the server to access the session
xfbml : true, // parse XFBML
channelURL : 'http://www.your_domain.ca/channel.html', // channel.html file
oauth : true // enable OAuth 2.0
});
// This should be fired on a user initiated action
FB.ui({ method: 'oauth', perms: 'email' }, function() { callback() });
};
(function(d){
var js, id = 'facebook-jssdk'; if (d.getElementById(id)) {return;}
js = d.createElement('script'); js.id = id; js.async = true;
js.src = "//connect.facebook.net/en_US/all.js";
d.getElementsByTagName('head')[0].appendChild(js);
}(document));
function callback() {
FB.getLoginStatus(function(response) {
if (response.authResponse) {
FB.api('/me', function(response) {
//Get user information from here
});
}
});
}
</script>
make sure the request that u r making is coming from the same domain as the one you have registered in facebook api
In a tab you don't need to pass any parameter other than method and perms(or scope when Facebook changes it)
FB.getLoginStatus(function(response){
if(response.status == 'connected'){
alert("User connected");
}else{
FB.ui({
method: 'oauth',
perms: 'email,user_birthday' //change perms to scope when Facebook supports it
}, function(response){
if(response.session){
alert("User connected");
}else if(response.installed != '1'){
alert("User canceled");
}
});
}
}
)

Facebook Connect does not process jQuery ajax call after successful login

I've got the following script:
<script>
window.fbAsyncInit = function () {
FB.init({ appId: '<%: Facebook.FacebookApplication.Current.AppId %>',
status: true,
cookie: true,
xfbml: true,
oauth: true
});
function FBLogin() {
FB.login(function (response) {
if (response.authResponse) {
ConnectFacebookUser(response.authResponse.accessToken);
} else {
alert('User cancelled login or did not fully authorize.');
}
}, { scope: 'email' });
}
$('#fb-button').live('click', function (e) {
FBLogin();
e.preventDefault();
});
function ConnectFacebookUser(at) {
var postdata = "at=" + at;
alert('This is the access token : ' + at);
$.ajax({
type: "POST",
dataType: "json", // what data type to expect from the server
url: "/FBConnect.ashx",
data: postdata,
success: function (data) {
// do nothing
alert('all good');
},
error: function (xhr, status, error) {
alert('error occured in FBConnect.ashx');
}
});
alert('finished');
}
};
(function () {
var e = document.createElement('script'); e.async = true;
e.src = document.location.protocol + '//connect.facebook.net/en_US/all.js';
document.getElementById('fb-root').appendChild(e);
} ());
</script>
And for the html fragment:
Login with facebook
<div id="fb-root"></div>
When the link is clicked, a popup window for facebook shows up and when i enter my login details, i only see an alert to say "This is the access token..." but the rest of the code don't get processed. If I click the "login with facebook" link one more time, then everything gets processed as normal.
What could be the problem?
It works for me. I get the message 'this is the acccess code' and then i get a 'finished' alert. Check your Chrome debugger tools to look for any script errors that may be occuring silently.
Check out https://github.com/xocialhost/jquery.xocialCore.js/blob/master/jquery.xocialCore.js to see a jQuery specific way of doing this. One thing I noticed is that the jQuery click function when calling the FB.login was triggering the popup blocker in a few browsers I was testing with. I set this up to work around that issue but it's also a way to add a callback that is run properly during the init sequence.

Extracting user id from Facebook Javascript SDK session object

I am using Facebook's Javascript SDK "FB.ui" to pull up an OAuth dialog. Once you click allow, I need to capture the session object, extract the user id and use it further in my script. For some reason I cannot get this working properly, I keep getting undefined, even though the session does exist.
<script src="http://connect.facebook.net/en_US/all.js"></script>
<div id="fb-root"></div>
<script type="text/javascript">
FB.init({
appId : '***************',
status : true,
cookie : true,
xfbml : true
});
FB.getLoginStatus(function(response) {
if (response.session) {
//do something
} else {
FB.ui({
method: 'oauth',
display: 'page',
scope: 'email',
perms: 'email'
},
function(response) {
alert(response.session.uid); //returns underfined
});
}
});
</script>
To get the userID:
FB.getLoginStatus(function(response) {
alert(response.authResponse.userID);
});
When you login from login button of facebook then it stores cookies in your system that contains your access token that is also unique. That cookies contains your facebook id also.
Check your response. I bet it says something like "display" must be one of "popup", "iframe" or "hidden". Apparently the oauth dialog cannot be called with the default "page" display. And display: 'iframe' requires that you include an access_token, which is why you're calling the oauth dialog in the first place >:l .
I don't think FB.ui can currently be used to get the oauth dialog, unless you want to use a popup, which most everyone has blocked nowadays.
So I got this to work as I would like. This may not be the best way, but after much digging around and frustration I hope this could help somebody with the same question get on the right track.
JS source:
FB.getLoginStatus(function(response) {
if (!response.session) {
//initiate FB OAuth js popup dialog
FB.ui({
method: 'oauth',
display: 'page',
scope: 'email',
perms: 'email'
},
function(response) {
if (response.session) { //if permission Allowed
var thesession = response.session;
var thesession = eval('(' + thesession + ')'); //decode json
//POSTing to local file get.user_graph.php, to fetch user info
$.ajax({
type: "POST",
url: "get.user_graph.php",
data: "client_id=<?php echo $appId; ?>&client_secret=<?php echo $secret; ?>&sessions="+thesession.session_key+"&type=client_cred&uid="+thesession.uid,
dataType: "text",
success: function(user_graph){
var user_graph1 = eval('('+user_graph+')');
alert(user_graph1.name); //users name
alert(user_graph1.id); //users id
alert(user_graph1.email); //users email
alert(user_graph1.link); //users profile link
}
});
} else {
//if permission Not Allowed
}
});
}
});
get.user_graph.php source:
//exchange our session for an access_token
define('POSTURL', 'https://graph.facebook.com/oauth/exchange_sessions');
define('POSTVARS', 'client_id='.$_POST['client_id'].'&client_secret='.$_POST['client_secret'].'&sessions='.$_POST['sessions'].'&type=client_cred');
$curl_token = curl_init(POSTURL);
curl_setopt($curl_token, CURLOPT_POST,1);
curl_setopt($curl_token, CURLOPT_POSTFIELDS,POSTVARS);
curl_setopt($curl_token, CURLOPT_FOLLOWLOCATION,1);
curl_setopt($curl_token, CURLOPT_HEADER,0);
curl_setopt($curl_token, CURLOPT_RETURNTRANSFER,1);
$token = curl_exec($curl_token);
$token_decoded = json_decode($token,true);
//get the user graph (personal info)
$user_graph = file_get_contents("https://graph.facebook.com/".$_POST['uid']."?access_token=".$token_decoded[0]['access_token']);
echo $user_graph;