titanium - get the facebook user name? - facebook

Using titanium, does anybody have some simple instructions to get the user's facebook name, once signed into facebook?

you don't need to do any of this, the username is provided in the data response after the login is done.
Look at the appcelerator documentation

I haven't tested the code but you can try this:
var fbuid = Titanium.Facebook.uid; //this would be the logged user's facebook uid
function fQuery() //this function exec the fql query
{
var myQuery = "SELECT name FROM user WHERE uid = "+fbuid;
var data = [];
Titanium.Facebook.request('fql.query', {query: myQuery}, function(x)
{
var results = JSON.parse(x.result);
var username = results[0].name; //user's fb name
});
};

Ah, here is how you do it:
function getFacebookInfo(){
Titanium.Facebook.requestWithGraphPath('me', {}, 'GET', function(e){
if (e.success){
var jsonObject = JSON.parse(e.result);
//do something here with these values. They cannot be passed out of this
//function call... this is an asynchronous call
//that is, do this:
saveToDb(jsonObject.first_name);
} else {
//some sort of error message here i guess
}
});
};
Finally, along with name and username, check out the facebook page for the other variables you can get -
http://developers.facebook.com/docs/reference/api/
FINALLY: be aware that this is a callback, and titanium won't actually wait for this call to finish. That is, any variable declared to hold the results the returned after the requestWithGraphPAth will immediately return, and as a result almost always be empty.
I guess you could make a nifty loop that just... loops until some variable is set to false. And you'd set the variable to false in the callback... but that seems dodgy.
Just make your call back do everything else, that is, save to the db etc etc
If you do go the route of calling Ti.Facebook.authorise() to log in the user, remember to define
Ti.Facebook.addEventListener('login',function(e){
if (e.success){
...
...
} else if (e.error){ } else if (e.cancel) { }
}
before the call. And then, in the success bit, you can make a requestWithGraphPath call and so on. I just save all the details to the database and retrieve them each time after that, works fine for me!

Related

Meteor - no more callbacks for "findOne" function

i'm working on a Meteor project, and I must say that isn't easy at all, especially for one thing: callbacks !
Everything is async, so I wonder how do I must do to get results from my mongodb.
var user = Meteor.users.findOne({username: "john"});
return (user); // sometimes returns "undefined"
...
var user = Meteor.users.findOne({username: "john"});
if (user) // so ok, I check if it exists!
return (user); // Cool, I got my user!
return (); // Ok and what should I return here? I want my user!
I don't want to be dirty and put like setTimeout everywhere.
Anybody has a solution for this ?
EDIT :
I noticed in router.js with console.log that my data is returned 4 times. 2 times with an undefined value and 2 other times with the expected value. In the view, it's still undefined.
Why the router passes like 4 times in this route ? Does it display the first result of the return value in the router ?
What should I return if the find() doesn't find anything ?
EDIT 2: Here is some code to understand.
this.route('profilePage', {
path: 'profil/:_id?',
waitOn: function() {
return [
Meteor.subscribe('article', { prop: this.params._id}), // id can be id or username
Meteor.subscribe('article', { userId: this.params._id}), // id can be id or username
Meteor.subscribe('params'),
Meteor.subscribe('profil', (this.params._id ? this.params._id : Meteor.userId()))
];
},
data: function() {
if (this.params._id) {
var user = Meteor.users.findOne(this.params._id);
if (!user)
user = Meteor.users.findOne({username: this.params._id});
console.log(user);
return user;
}
else if (Meteor.userId())
return Meteor.user();
else
Router.go("userCreate");
}
});
I get this on the console:
http://puu.sh/debdJ/69419911f7.png
(text version following)
undefined
undefined
Object_id: "o3mgLcechYTtHPELh"addresses: (....)
Object_id: "o3mgLcechYTtHPELh"addresses: (....)
findOne(yourId) is a sync method which is equivalent to find({ _id: yourId}, callback). The difference is that find() allows you to define a callback. If you don't pass a callback to find() this method will be sync.
check wrapAsync: http://docs.meteor.com/#/full/meteor_wrapasync
It allows you to code in a sync style with a async operations.
Free lesson on EventedMind: https://www.eventedmind.com/feed/meteor-meteor-wrapasync
My experience thus far is that the Meteor Mongodb package is that the functions do not generally provide callbacks (for some reason insert does...), the functions are atomic (thus sync).
There are meteor packages that can make Mongodb async if you want (I havn't tried any).
I guess this sync approach is in line with the simple maintenance goal of Mongodb. Thinking about it, one of my pet peeves using Node is working with async callback waterfalls/nests, they are a pain to create and maintain... and hopefully this will make my code easier to read and understand and change...
var future = new Future();
var _h = Hunts.findOne({huntId});
if(_h) {
future.return(_h)
} else {
return future.wait();
}
on server/startup.js you need:
Future = Npm.require('fibers/future');

Autopublish removed but why can I still retrieve data from db?

I have a simple Meteor/MongoDB project using the 'roles' package where I optain data from the db to the client. The roles package seems to work fine and the browser shows the right data depending on who is logged in, just like it should do. Then when running 'meteor remove autopublish' in the terminal inside my applications directory I get 'autopublish removed' just like it should. Still I can retrieve data from the server just as before(!?)
I have all of my db calls from the client/client.js.
The server/server.js does nothing (I do have publish/subscribe code but uncomment for now) and same goes for the common js file in main directory.
How can this be? Am I perhaps retrieving data from minimongo somehow? I have also removed insecure even if I don't think that matters in this case(?) Thanks in advance.
EDIT: Here's the code:
client.js:
//when uncomment the subscribe's you should not get access to the server/db, but 'data' that holds all the inlogg info still shows. The 'movies' on the other hand doesn't, just like it shouldn't.
//Meteor.subscribe('data');
//Meteor.subscribe('movies');
/*############# Get User Data ###############*/
Template.userLoggedIn.id = function () {
return Meteor.userId();
};
Template.userLoggedIn.email = function () {
var email = Meteor.users.findOne({_id: Meteor.userId()});
return email.emails[0].address;
};
Template.userLoggedIn.profile = function () {
var profile = Meteor.users.findOne({_id: Meteor.userId()});
return profile.profile.name;
};
Template.userLoggedIn.role = function () {
var role = Meteor.users.findOne({_id: Meteor.userId()});
return role.roles[0];
};
/*############# ###############*/
Template.movies.movies = function() {
var movies = Movies.find().fetch();
return movies;
}
server.js:
Meteor.publish('data', function () {
return Meteor.users.find();
});
Meteor.publish('movies', function() {
return Movies.find();
});
Thanks for providing the code - I see how this could be confusing. The users section of the docs should be written to explicitly say this, but what's happening is the current user is always published. So even if you don't write a publish function for users (or your have your subscribe commented out), you should expect to see the current user on the client. Because your template code only looks for Meteor.userId(), I would expect it to still work.
Assuming you have other users in the database, you can quickly check that they are not being published by running: Meteor.users.find().count() in your browser console. If it returns 1 then you are only publishing the current user (or 0 if you are logged out).

FB.api links.get can't work with short name accounts

I use this code:
FB.api({
method: 'links.get',
uid: user_uid,
limit: 100
},function(response){
var response = response[0];
if(response){
console.log(response);
}
});
I can't get response from callback of this function, if people with mentioned use short name( instead of ?id=100000000).
I'm getting user id using this code.
But it works only if user doesn't have short name:
FB.Event.subscribe('auth.login',function(response){
user_id = response.authResponse.userID;
});
But it works only if user doesn't have short name.
How can i listen event from people with a short name?
Thank you.
Name has nothing to do with it. response.authResponse.userID will always return the user_id not user_name. You must be doing something else wrong.

Post on Friends' Wall(s) via Facebook Actionscript 3 SDK

I'm absolutely new to programming and just managed to learn the basics of ActionScript 3. Now, I would like to learn how to post on my Friends' Walls via the as3 SDK using the UI class (taken from a nice Tutorial):
This is how I post on my own Wall:
protected function newsFeed ():void
{
// define your caption text
var theCaption:String = "CaptionText";
// define the descrition text
var theDescription:String = "Text for game Achievement";
// We need to follow the FB docs to tell it what sort of input we are sending to FB
// We are trying to set the 'feed'
var methodInput:String = 'feed';
var thePicture:String = "mylink/picture.png";
var theLink:String = "mylink";
var theName:String = "Name of FB Status Setter";
// Create an object that we'll call 'data' and fill it with the actual data we're sending to Facebook
var data:Object = {
caption:theCaption,
description:theDescription,
picture:thePicture,
name:theName,
link:theLink
};
Facebook.ui(methodInput, data, onUICallback);
}
protected function onUICallback(result:Object):void
{
// do something
}
This works perfectly fine. I know that I have to integrate the parameter "to" somewhere. But I don't know where and how. Sorry I'm very very new to this. This is from Facebook Docs
Properties
from: The ID or username of the user posting the message. If this is unspecified, it defaults to the current user. If specified, it must be the ID of the user or of a page >that the user administers.
to: The ID or username of the profile that this story will be published to. If this >is unspecified, it defaults to the the value of from.
Hopefully someone can help me out.
Best Regards,
Amir
P.S.: Is there a way to post only one friend's wall and another way to post on several friends' walls?
I believe you want to use Facebook.api() rather than 'ui'. According to the documentation for the AS3 FB API, 'ui' just opens the share dialog. If you want to create a post on a friends wall, then you'll want to use 'api'.
I haven't tested this in Flash, but I think you can set the method as /PROFILE_ID/feed ... of course replacing "PROFILE_ID" with the FB uid of the friend. Then, include the arguments; message, picture, link, name, caption, description and source in your data object.
So your code would look something like:
var method:String = "/friend_id/feed";
var data:Object = {};
data.message = "Your message";
data.picture = "http://www.google.com/kittens.jpg";
data.link = "http://www.mysite.com/link";
data.caption = "Your caption";
data.description = "Your description";
data.source = "http://www.mysite.com/video.swf";//(optional) source is a video or Flash SWF
Facebook.api(method, yourCallback, data, "POST");
function yourCallback(result:Object, fail:Object):void {
if (result) {
trace(result)
} else if (fail) {
trace(fail);
}
}
If you have multiple friends, you could probably just put the uid's in an array and loop through the method above. The AS3 API has a batch request method that I haven't tried, but you can check out the Documentation.
Facebook has some pretty helpful tools that are somewhat hidden.
Checkout their Debugger and their Graph API Explorer
Hope that's helpful.

events_create fails in facebook

strugglin to create event in javascript as
api.events_create(eventInfo,function(result,ex){
is failing and
catch(FacebookRestClientException){
gives
TypeError: api.events_create is not a function message=api.events_create is not a function
any clue
Some more context would help in debugging this.
You've created the api object, yes? (e.g., var api = FB.Facebook.apiClient;)
I'm having the same problem. If I look at the list of functions attached to FB.Facebook.apiClient using a DOM inspector, events_create() does not exist - even though other methods like events_get() and feed_publishUserAction() are there.
Facebook might have deliberately omitted it.
api.callMethod works - have put a sample call , hope it helps
var eventInfo = {
"name":this.name.value,
"category":"1",
"subcategory":"2",
"host":"My Host",
"location":"JP Nagar",
"city":"Bang",
"start_time":starttime,
"end_time":endtime};
function createEvent(eventinfo) {
try{
//check if user has extended permission to create otherwise prompt him for same
api.users_hasAppPermission('create_event',function(res,ex){
if (res == 0)
FB.Connect.showPermissionDialog("create_event",
function(res,ex){alert("Congratulations events");});
});
dict = {};
dict['event_info'] = eventinfo;
//provide a call back or a sequencer
var ret = api.callMethod(
'events.create',
dict,
function(eventid,ex){
console.log(data);
});
return ret;
}
catch(FacebookRestClientException){
console.log(FacebookRestClientException);
}
return;
}//createEvent routine