I am using the new FB.ui to create an request to friends on Facebook for my application.
The user is show a panel with their friends and they select the ones they want to send the request.
On the callback I can access the request_id and now I want to get the details for the users that were invited.
If I paste the following code in the browser I can get the user_id of the invited user which is the info I want:
https://graph.facebook.com/138744992885393?access_token=193078857407882|d0048a9beb58a9a247c6b987.0-751640040|zNmBLfZxBBKikoj8RlZiHfKpugM
What I want to be able to do is do the same thing but from my code and then access the information returned.
Here is my code:
function sendRequests() {
FB.ui({
method: 'apprequests',
message: ' should learn more about this awesome site.',
data: 'extra data'
}, function(response) {
if (response != null && response.request_ids && response.request_ids.length > 0) {
for (var i = 0; i < response.request_ids.length; i++) {
alert("Invited: " + response.request_ids[i]);
// somehow send a request to Facebook api to get the invited user id from the request_id and save to the database
//can save these id's in the database to be used to track the user to the correct page in application.
}
top.location.href="http://localhost:3000/";
} else {
alert('No invitations sent');
}
});
}
How can I do this?
I am using Rails 3.0.7 Ruby 1.9.2
You can get the facebook user ID as follows:
for (var i = 0; i < req_ids.length; i++) {
alert("Invited: " + req_ids[i]);
FB.api('/me/apprequests/?request_ids='+toString(req_ids[i]),
function(response)
{ alert(response);
alert(response['data'][0]['from']['id']);
});
}
Thanks very much to this post on stackoverflow
You can also get the facebook IDs and even names of selected friends using below code:
FB.ui( {
method: 'apprequests',
redirect_uri: 'YOUR APP URL',
message: "Tom sent you a request"
},
function(response) {
if(response && response.hasOwnProperty('to')) {
for(i = 0; i < response.to.length; i++) {
//alert( response.to[i]);
// response.to[i] gives the selected facebook friends ID
// To get name of selected friends call this function below
getfbnames(response.to[i]);
}
}
}
);
function getfbnames(selectedfrndid)
{
var url = 'getfriendfbname.php'; // call a php file through URL and jquery ajax
$.ajax({
type:'POST',
url: url,
data : { fbid : selectedfrndid },
async: false,
success: function(data)
{
alert(data);
}
}); // end of ajax
}
A file getfriendfbname.php which returns the name of facebook friend name using friend facebook id in php
$fbid=$_POST['fbid'];
$json = file_get_contents('https://graph.facebook.com/'.$fbid);
$data = json_decode($json);
echo $data->name;
return $data->name;
If that's the user_id of the invited users, you can have them in your callback, in response.request_ids. But you seem to already know that.
Could you clarify your problem?
Related
I'm building an app using the QuickBlox SDK. I have the user's ID stored and need to use that ID to retrieve the user object. Essentially I need to query for the user by ID and retrieve the user object How is this possible?
I attempted to use the userWithID method but it seems this has been deprecated. Any advice would be appreciated.
As per QuickBlox officail doc , the method you find is deprecated but you can use this below method to get user(s) by user(s) id
official doc of QuickBlox for android
QBPagedRequestBuilder pagedRequestBuilder = new QBPagedRequestBuilder();
pagedRequestBuilder.setPage(1);
pagedRequestBuilder.setPerPage(50);
ArrayList<Integer> usersIds = new ArrayList<>();
usersIds.add(qbChatDialog.getRecipientId());//set user id(s) here
QBUsers.getUsersByIDs(usersIds, pagedRequestBuilder).performAsync(new QBEntityCallback<ArrayList<QBUser>>() {
#Override
public void onSuccess(ArrayList<QBUser> users, Bundle params) {
//here you can get User(S) detail in users arrayList
String email = users.get(0).getEmail();
}
#Override
public void onError(QBResponseException errors) {
Log.e("error" , errors.toString());
}
});
You can use QB.users.listUsers to retrieve the user object from userid. I had attached source code here.It's work for me
var userId = 18767586;
var params = {
filter: { field: 'id', param: 'in', value: [userId] },
order: { sort: 'desc', field: 'id' }
};
QB.users.listUsers(params, function(err, result){
if (err) {
console.log("errMsg ==> ", err);
} else {
_.each(result.items, function(item) {
console.log("User Object ", item.user);
console.log(item.user.full_name);
// here you can get the user
});
}
});
This solution has been found from http://qaoverflow.com/question/quickblox-filtering-custom-objects-with-javascript/
So I'm using Angularfire in an ionic app and trying to figure out how to make a user object that is associated with the auth data from an Auth $createUser call. My first try had the auth call and the user got authenticated, then a user object was made and pushed into a $firebaseArray which works fine, but I don't know how to grab the current user after they are logged in to update, destory, or do anything with that users data. I have made it work with looping through the users array and matching the uid from the user array item and the auth.uid item which was set to be the same in the user array object creation. This seems really ineffecient to loop over if there is a large user array and it needs to be done on multiple pages.
My current attempt is using a different method like so:
angular.module('haulya.main')
.controller('RegisterController', ['Auth', '$scope', 'User', '$ionicPlatform', '$cordovaCamera','CurrentUserService',
function(Auth, $scope, User, $ionicPlatform, $cordovaCamera, CurrentUserService) {
//scope variable for controller
$scope.user = {};
console.log(User);
$scope.createUser = function(isValid) {
var userModel;
$scope.submitted = true;
//messages for successful or failed user creation
$scope.user.message = null;
$scope.user.error = null;
//if form is filled out valid
if(isValid) {
//Create user with email and password firebase Auth method
Auth.$createUser({
email: $scope.user.email,
password: $scope.user.password
})
.then(function(userData) {
userModel = {
uid: userData.uid,
photo: $scope.user.photo || null,
firstName: $scope.user.firstName,
lastName: $scope.user.lastName,
email: $scope.user.email,
cell: $scope.user.cell,
dob: $scope.user.dob.toString(),
city: $scope.user.city,
state: $scope.user.state,
zip: $scope.user.zip
}
// add new user to profiles array
User.create(userModel).then(function(user) {
$scope.sharedUser = User.get(user.path.o[1]);
});
$scope.user.message = "User created for email: " + $scope.user.email;
})
.catch(function(error) {
//set error messages contextually
if(error.code == 'INVALID_EMAIL') {
$scope.user.error = "Invalid Email";
}
else if(error.code == 'EMAIL_TAKEN'){
$scope.user.error = "Email already in use, if you think this is an error contact an administrator";
}
else {
$scope.user.error = "Fill in all required fields";
}
});
}
};
//Get profile pic from camera, or photo library
$scope.getPhoto = function(type) {
$ionicPlatform.ready(function() {
//options for images quality/type/size/dimensions
var options = {
quality: 65,
destinationType: Camera.DestinationType.DATA_URL,
sourceType: Camera.PictureSourceType[type.toUpperCase()],
allowEdit: true,
encodingType: Camera.EncodingType.JPEG,
targetWidth: 100,
targetHeight: 100,
popoverOptions: CameraPopoverOptions,
saveToPhotoAlbum: false
};
//get image function using cordova-plugin-camera
$cordovaCamera.getPicture(options)
.then(function(photo) {
$scope.user.photo = photo;
}, function(err) {
console.log(err);
});
});
};
}]);
And here's the service the controller is using:
angular
.module('haulya.main')
.factory('User', function($firebaseArray) {
var ref = new Firebase('https://haulya.firebaseio.com');
var users = $firebaseArray(ref.child('profiles'));
var User = {
all: users,
create: function(user) {
return users.$add(user);
},
get: function(userId) {
return $firebaseArray(ref.child('profiles').child(userId));
},
delete: function(user) {
return users.$remove(user);
}
};
return User;
});
This also works, but again I don't have a solid reference to the currently logged in users object data from the array. The objects id is only stored on the controllers scope.
I looked through other posts, but they were all using older versions of firebase with deprecated methods.
If you're storing items that have a "natural key", it is best to store them under that key. For users this would be the uid.
So instead of storing them with $add(), store them with child().set().
create: function(user) {
var userRef = users.$ref().child(user.uid);
userRef.set(user);
return $firebaseObject(userRef);
}
You'll note that I'm using non-AngularFire methods child() and set(). AngularFire is built on top of Firebase's regular JavaScript SDK, so they interoperate nicely. The advantage of this is that you can use all the power of the Firebase JavaScript SDK and only use AngularFire for what it's best at: binding things to Angular's $scope.
Storing user data is explained in Firebase's guide for JavaScript. We store them under their uid there too instead of using push(), which is what $add() calls behind the scenes.
I'm making a facebook app where to get the link user of the group members (like this : "https://www.facebook.com/app_scoped_user_id/536994703068744/"
but my code doesn't work, getting error "property length undefined"
here my code :
function getUserLink() {
FB.api('/243031772570397/members',{limit:5}, function(response) {
for(i=0;i<response.data.length; i++) {
var uid = response.data[i].id;
FB.api('/'+uid+'/', function(response2) {
for(y=0;y<response2.data.length; y++) {
console.log(response2.data[y].link);
}
});
}
});
}
Response2 has only one record.
changinging it to below should work.
FB.api('/'+uid+'/', function(response2) {
console.log(response2.link);
});
I need to exclude friend on invite dialog request and function looklike
duplicate:function(){
var responsive = '';
FB.api(
{
method: 'fql.query',
query:'SELECT uid,name FROM user WHERE uid IN \n\
(SELECT uid2 FROM friend WHERE uid1 = me()) AND is_app_user = 1 '
},
function(response) {
responsive = response;}
);
return responsive;
},
onInviteClick: function(responsive) {
FB.ui( {
method: 'apprequests',
title: 'Popsecret ป็อบคอร์นแสนอร่อย',
exclude_ids: responsive
message: 'ชวนคุณกดไลท์เพื่อลุ้นรับไอแพดเเละของรางวัลอีกมากมาย',
max_recipients: 15
} , function(response) {
if (response !== null) {
$.post(Site.inviteCallbackURL, response, function(res) {
});
}
});
and I can't pass data from duplicate to onInviteClick
Move var responsive = ''; from function into global scope. Then you can access it from all other functions. Keep in mind, that the API call is asynchronous, so you need to wait for the completion of the request (jQuery Deferreds might help you there)
I have the following code displaying the request dialog. The function sends the requests to the users I select but the problem is that it DOES NOT RETURN THE SELECTED USERS IDS
$('#request_btn').click(function(){send_request()});
function send_request()
{
var message = 'Bla Bla';
var title = 'Bla Bla'
FB.ui({
method: 'apprequests',
message: message,
title: title,
max_recipients: 20
},
function (response)
{
if (response && response.request_ids)
{
alert('success')
}
});
return false;
}
It should be returning the request IDs, not the user IDs, are you sure that's not happening?
The example at http://www.fbrell.com/fb.ui/apprequests is working fine for me