Search code examples
facebookfacebook-graph-apiappceleratortitanium-mobileappcelerator-titanium

Error 2500 when trying to get Facebook Email in Appcelerator Titanium


I try to get email address of a user logged in via Facebook Module. But get error every time {"error":"An error code 2500 has occured. An active access token must be used to query information about the current user."}

My code is:

var viewClick = function() {
    fb.logout();
    fb.initialize(); 
    fb.authorize();
};

var facebookLogged = function(e) {

    fb.requestWithGraphPath("me?fields=name,email,first_name,last_name", {}, 'GET', function(result) {
        Ti.API.info(JSON.stringify(result))
       // var data = JSON.parse(e.result);
});
};
var window = Ti.UI.createWindow({exitOnClose: true, navBarHidden: true, fullscreen: true, orientationModes: [
        Ti.UI.PORTRAIT,
        Ti.UI.UPSIDE_PORTRAIT,      
    ],
    backgroundColor: '#f0f2f2'
}); 

var fb = require('facebook');

if(Ti.Platform.osname === 'android') {

    window.fbProxy = fb.createActivityWorker({lifecycleContainer: window});
}

    //fb.setLoginBehavior(fb.LOGIN_BEHAVIOR_NATIVE);
fb.permissions = ['email'];

window.open();


var view = Ti.UI.createView({
    height: 200,
    width: 200,
    backgroundColor: 'red'
}); 

view.addEventListener('click', viewClick);

window.add(view);
fb.addEventListener('login', facebookLogged);

I also tried to provide access token code by modyfing requestWithGraphPath parameters:

fb.requestWithGraphPath("me?fields=name,email,first_name,last_name&access_token=" + e.source.accessToken, {}, 'GET', function(result) {}

but in such case I get infromation that accessToken is malformed. TiFacebookModule: (main) [117,178060] requestWithGraphPath callback error: Malformed access token [Here is access token value]?access_token=[Here is access token value]

What I do wrong? How to get Email from FB? Any help deeply appreciated.


Solution

  • I had the same problem and this is what I do, for Android the requestWithGraphPath doesn't work like its IOS counterpart and the documentation is not updated either. You need to send the fields in the object and only the "me" in the first parameter:

    var facebookLogged = function(e) {
        fb.requestWithGraphPath("me", { fields: "name,email,first_name,last_name"}, 'GET', function(result) {
            Ti.API.info(JSON.stringify(result))
            // var data = JSON.parse(e.result);
        });
    };
    

    Hope it helps.