(#210) User not visible - javascript

I'm writing a facebook app in which the logged in user (a) chooses a friends from their friends list (b) and then the app posts on their friends wall (a posts to b).
I ask from the user the publish_stream permissions.
This is the code for showing the friends selector popup:
function selectFriends() {
FB.ui({
method: 'apprequests',
message: 'You should learn more about this awesome game.', data: 'tracking information for the user'
},function(response) {
if (response && response.request_ids) {
//alert(typeof(response.request_ids));
//alert(response.request_ids);
alert (response.request_ids);
len=response.request_ids.length;
alert(len);
if (len>2) {
alert("Select no more than one friend");
}
else {
user_req_id = response.request_ids[0];
postOnFirendsWall(user_req_id);
}
}
else {
alert('Post was not published.');
}
});
}
This is the postOnFirendsWall(user_req_id) method:
function postOnFirendsWall(friend_id)
{
/** Auto Publish **/
var params = {};
params['message'] = 'message';
params['name'] = 'name';
params['description'] = '';
params['link'] = 'https://www.link.com/';
params['picture'] = 'http://profile.ak.fbcdn.net/hprofile-ak-snc4/203550_189420071078986_7788205_q.jpg';
params['caption'] = 'caption';
var stam = '/'+friend_id+'/feed';
FB.api('/'+friend_id+'/feed', 'post', params, function(response) {
if (!response || response.error) {
alert('Error occured');
alert(response.error);
} else {
alert('Published to stream - you might want to delete it now!');
}
});
return false;
}
But I keep getting the following error: (#210) User not visible.
Can you see why?

Have you verified that user A has permission to write on the wall of user B? If not, this will never work.
Also, why are you using an apprequests dialog if you want to post on a friend's wall?

Related

Facebook Graph API me/friends returns nothing

I have an app with Facebook login and the logging in and getting basic profile information works fine. However, when I try to get the Facebook friends (which only returns friends who also use the same app), I get [object Object] from the Facebook API. I have the permissions for friends set (according to the Facebook Developer page of my app).
My code looks like this (I'm using the Phonegap plugin, but the code is similar to the JS version for the Facebook API):
// Login function (permissions included)
var login = function () {
if (!window.cordova) {
var appId = prompt("123456789101112", "");
}
facebookConnectPlugin.login(["email, user_friends"],
// SUCCESS
function (response) {
alert('Login successful!');
},
// FAILURE
function (response) {
alert('Login failed')
}
);
}
// Get the friends
var getFacebookFriends = function() {
facebookConnectPlugin.api("me/friends",
// FAILURE
function(response) {
alert('Retrieving Facebook friends failed');
},
// SUCCESS
function(response) {
alert(JSON.stringify('Facebook friends: ' + response));
});
}
The alert says Facebook friends: [object Object]. I'm sure I have a friend who has also logged in to the app using the same permissions. He doesn't appear on the list, only the empty [object Object]. Why do I get this response and not a list of friends?
It´s not empty, it is a JSON object. You just need to decode it correctly:
alert('Facebook friends: ' + JSON.stringify(response));
You can also just use console.log:
console.log(response);
Just connect your phone to the computer while your App is running and use chrome://inspect to debug it like a website (because that´s what it is).
If you want to define a function, you can just do this:
function name() {
...
}
yes, you can leave that var keyword. Also, you should try this:
function getFacebookFriends() {
facebookConnectPlugin.api('/me/friends', function(response) {
if(response.data) {
$.each(response.data,function(index,friend) {
alert(friend.name + ' - FB ID:' + friend.id);
});
} else {
alert("Unable to return Facebook friends!");
}
});
}
I just Hope if this could help you !!
[FBSDKProfile enableUpdatesOnAccessTokenChange:YES]; in - (void)viewDidLoad Method
- (IBAction)Loginwithfacebookaction:(id)sender
{
FBSDKLoginManager *login = [[FBSDKLoginManager alloc] init];
[login logOut];
[login logInWithReadPermissions:#[#"public_profile", #"email"] handler:^(FBSDKLoginManagerLoginResult *result, NSError *error) {
if (error)
{
// There is an error here.
}
else
{
if(result.token) // This means if There is current access token.
{
// Token created successfully and you are ready to get profile info
[self Method];
}
}
}];
}
-(void)Method {
FBSDKGraphRequest *requestMe = [[FBSDKGraphRequest alloc]initWithGraphPath:#"/me?fields=first_name, last_name, picture, email" parameters:nil];
FBSDKGraphRequestConnection *connection = [[FBSDKGraphRequestConnection alloc] init];
[connection addRequest:requestMe completionHandler:^(FBSDKGraphRequestConnection *connection, id result, NSError *error) {
if(result)
{
if ([result objectForKey:#"email"]) {
email = [result objectForKey:#"email"];
NSLog(#"Email: %#",[result objectForKey:#"email"]);
[[NSUserDefaults standardUserDefaults] setObject:email forKey:#"useremail"];
}
if ([result objectForKey:#"first_name"]) {
NSLog(#"First Name : %#",[result objectForKey:#"first_name"]);
name = [result objectForKey:#"first_name"];
[[NSUserDefaults standardUserDefaults] setObject:name forKey:#"userNameLogin"];
}
if ([result objectForKey:#"id"])
{
NSLog(#"User id : %#",[result objectForKey:#"id"]);
}
}
}];
[connection start];
}

How to get any user details from facebook api

How to find user details by using his/her email ID or mobile number from Facebook? I want to get user details on my website using my app id access.
Please help me out this.
Try this. You need his facebook ID or his facebook name.
var fields = [
'id',
'name',
'first_name',
'middle_name',
'last_name',
'gender']
FB.api('/{insertIDhere}', {fields: fields}, function(details) {
// output the response
});
Or take this code from the FB Api Documentation
FB.api(
"/{user-id}",
function (response) {
if (response && !response.error) {
/* handle the result */
}
}
);
to login you need something like this :
FB.login(function(response) {
if (response.authResponse) {
//login successfull
} else {
//anything else
}
}, {scope: 'public_profile,email,user_friends'});
If you are logged in you could use something like this:
FB.api(
"/me?fields=id,name,picture.redirect(false).type(large)",
function (response) {
if (response && !response.error) {
facebookId = response.id;
facebookName = response.name;
facebookProfilePicture = response.picture.data.url;
callback(facebookUser);
}else{
}
}
);
You can't. Searching by email or phone is not possible.
https://developers.facebook.com/docs/graph-api/using-graph-api/v2.3#search

Using Facebook request dialog with Meteor

I'm trying to send an "app" invite to user friends using the Facebook JavaScript SDK.
Here is a template event when click the Facebook button:
"click #fb": function (e, tmp) {
Meteor.loginWithFacebook({
requestPermissions: ['user_likes',
'friends_about_me',
'user_birthday',
'email',
'user_location',
'user_work_history',
'read_friendlists',
'friends_groups',
'user_groups']
}, function (err) {
if (err) {
console.log("error when login with facebook " + err);
} else {
FB.api('/' + Meteor.user().services.facebook.id + '/friends', { fields: 'name,picture' }, function (response) {
if (response && response.data) {
friends = response.data;
friends_dep.changed();
}
});
}
});
}
after that i want the user to invite people to my app, my code looks like this (another template event):
FB.ui({method: 'apprequests',
message: 'My Great Request'
}, function(response) {
if (response && response.post_id) {
alert('Post was published.');
} else {
alert('Post was not published.');
}
}
);
And it's working. There is a Facebook dialog with all the user friends, but when trying to send the message, I get the response error = 'Post was not published.'
What am I doing wrong here?
Basically the user can build a group - and I want the user to be able to invite his facebook friends into that group. Is there anyway that when sending the request the reciver will just press "yes" and will be automatically added to the sender group?
note I'm using my local machine aka localhost:3000
Can you try removing the && response.post_id portion from the if statement?
According to the Facebook API docs for the Requests Dialog: https://developers.facebook.com/docs/reference/dialogs/requests/ the response will just have 'request' and 'to' data. It looks like you've copy and pasted your callback from an example they give for the Posts Dialog. If you still get an error after removing this then you aren't getting a response, I am unsure how the JS SDK handles responses. If you can get other API calls to work using js sdk then I'm really not sure.
I recently worked with the Facebook API and opted not to use the JS SDK because it seemed to be at odds with using the accounts-facebook package. I'm curious if you're using that too.
Some Facebook API calls like creating a Post (and possibly this one) do require a dialog box, I'll outline how I got around this without using the JS SDK in case it helps you or anyone else. I would just form the URL client side and open a popup window e.g. here's how I handled sending a post:
'click .send-message': function() {
var recipient = this.facebook_id;
var config = Accounts.loginServiceConfiguration.findOne({service: 'facebook'});
var url = "http://www.facebook.com/dialog/feed?app_id=" + config.appId +
"&display=popup&to=" + recipient + "&redirect_uri=" + Meteor.absoluteUrl('_fb?close');
window.open(url, "Create Post", "height=240,width=450,left=100,top=100");
}
Then to get the response server side:
WebApp.connectHandlers
.use(connect.query())
.use(function(req, res, next) {
if(typeof(Fiber)=="undefined") Fiber = Npm.require('fibers');
Fiber(function() {
try {
var barePath = req.url.substring(0, req.url.indexOf('?'));
var splitPath = barePath.split('/');
if (splitPath[1] !== '_fb') {
return next();
}
if (req.query.post_id) {
//process it here
}
res.writeHead(200, {'Content-Type': 'text/html'});
var content = '<html><head><script>window.close()</script></head></html>';
res.end(content, 'utf-8');
} catch (err) {
}
}).run();
});
This code is very similar to the code used in the oauth packages when opening the login popup and listening out for responses.

How to define scope in facebook js SDK to show user interest, movies etc?

My function is :
function CallAfterLogin()
{
FB.login(function(response)
{
if (response.status === "connected")
{
LodingAnimate(); //Animate login
FB.api('/me', function(data)
{
console.log(data);
if(data.email == null)
{
alert("You must allow us to access your email id!");
ResetAnimate();
}
else
{
AjaxResponse();
}
});
}
});
}
Instead of writing on console I want show data in browser, how can it be done?
I tried with $("#mydiv").text(data); , $("#mydiv").html(data); which dont work.
me/movies gives error :
You must allow us to access your email id
You can add the scope like this-
FB.login(function(response)
{
}, {scope: 'user_interests, user_likes, etc..'});

Javascript - how to wait for facebook

My app does the following:
1. Asks for the friends_online_presence permission.
2. If the user gave the permission (i.e the callback indicated success) I immidiately test for the permissions(*) and depending on the result pull the users' details (a call to getOnlineFriends).
The problem is that it takes a couple of miliseconds for facebook to update the permissions on their servers. So when I immidiately query for the permissions I just asked for facebook always says that I don't have the permission.
Using setTimout "solves" the problem.
How can this be solved deteministically?
I've tried some primitive form of polling but it got the browser stuck (and it also seems like a crooked solution).
(*) 'SELECT friends_online_presence FROM permissions WHERE uid=me()'
The code:
c2p.facebook = {
requestViewOnlineFriendsPerms: function (callback) {
myStuff.requestPermission('friends_online_presence', callback);
},
requestPermission: function (perms, callback) {
FB.ui({
method: 'permissions.request',
perms: perms,
display: 'popup'
}, function (response) {
if (response && response.perms) {
if (callback) {
callback(true);
}
} else if (!response.perms) {
if (callback) {
callback(false);
}
}
});
fql: function (q, callback) {
FB.api({ "method": "fql.query", "query": q }, callback);
},
getOnlineFriends: function (callback) {
var q = 'SELECT friends_online_presence FROM permissions WHERE uid=me()';
c2p.facebook.fql(q, function (rows) {
var isPermissionGranted = true;
var q2 = "SELECT uid, name, pic_square, online_presence FROM user WHERE online_presence IN ('active', 'idle') AND uid IN (SELECT uid2 FROM friend WHERE uid1 = me()) ORDER BY name";
if (rows[0].friends_online_presence == 0) {
isPermissionGranted = false;
q2 = "SELECT uid, name, pic_square FROM user WHERE uid IN(SELECT uid2 FROM friend WHERE uid1=me()) ORDER BY name";
}
c2p.facebook.fql(q2, function (arr) {
callback(arr, isPermissionGranted);
});
});
}
}
// The way I call this code is:
c2p.facebook.getOnlineFriends(function (arr, isPermissionGranted) {
$("#tmplFriends").tmpl(arr, { getIndex: function () { return $.inArray(this.data, arr); } }).appendTo("#friendsDiv");
if (!isPermissionGranted) {
$('.c2p_hidden').show();
}
});
How c2p.facebook.fql(q2, function (arr) {...} could see the callback variable? it's crazy.
The callback is a local variable of the getOnlineFriends function.
Tom just place your FQL query inside the
if (response && response.perms) {
}
for example suppose I want to fetch user details thru fql what I do is:
function onloginCall(){
FB.login(handleSessionResponse,{perms:'email,read_stream,publish_stream,offline_access'});
}
function handleSessionResponse(response){
// if we dont have a session, just hide the user info
if (!response.session) {
return;
}
else{
FB.api({
method: 'fql.query',
query: 'SELECT name, pic_square , email FROM user WHERE uid=' + FB.getSession().uid
},
function(response) {
var user = response[0];
store_fbuser_data(FB.getSession().uid,user.name,user.pic_square,user.email) ;
}
);
}
}
I guess this will solve your problem of delay. As this query will only execute when you have sufficient permissions.

Categories

Resources