Appcelerator-Q-A-banner

Facebook access token

You must Login before you can answer or comment on any questions.

Hi.

I have struggled with the facebook access token for a while now and i have no idea on how to get it to work.

I need to retrieve a token and put it in a var to use with this

var artist ='lucyseven';
 
var url = "https://graph.facebook.com/" + artist + "/statuses" + access_token;
To show my bands statuses in a tableview.

I would like it if the login appears in a dialog onload the first thing that happends when you start the app.

I have looked at this but that didnt make me any smarter.......

/*globals Titanium, Ti, alert, JSON */
var win = Ti.UI.currentWindow;
Titanium.Facebook.appid = "368268836546636";
Titanium.Facebook.permissions = ['publish_stream', 'read_stream'];
//
// Login Status
//
var label = Ti.UI.createLabel({
    text:'Logged In = ' + Titanium.Facebook.loggedIn,
    font:{fontSize:14},
    height:'auto',
    top:10,
    textAlign:'center'
});
win.add(label);
 
var forceButton = Ti.UI.createButton({
    title:'Force dialog: '+Titanium.Facebook.forceDialogAuth,
    top:50,
    width:160,
    height:40
});
forceButton.addEventListener('click', function() {
    Titanium.Facebook.forceDialogAuth = !Titanium.Facebook.forceDialogAuth;
    forceButton.title = "Force dialog: "+Titanium.Facebook.forceDialogAuth;
});
win.add(forceButton);
 
function updateLoginStatus() {
    label.text = 'Logged In = ' + Titanium.Facebook.loggedIn;
}
 
// capture
Titanium.Facebook.addEventListener('login', updateLoginStatus);
Titanium.Facebook.addEventListener('logout', updateLoginStatus);
 
//
// Login Button
//
if(Titanium.Platform.name == 'iPhone OS'){
    win.add(Titanium.Facebook.createLoginButton({
        style:Ti.Facebook.BUTTON_STYLE_WIDE,
        bottom:30
    }));
}
else{
    win.add(Titanium.Facebook.createLoginButton({
        style:'wide',
        bottom:30
    }));
}
So my question is:

How can i make the dialog popup on load and how do iretrieve the token and put it in place for my json?

Thanx

2 Answers

Accepted Answer

Hi Rickard Harrysson,

Try this code...

win.addEventListener("focus", function(e) {
    if (Ti.Facebook.loggedIn) {
 
    } else {
        Titanium.Facebook.authorize();
    }
});
 
Titanium.Facebook.addEventListener('login', function(e) {
    var accTo = Titanium.Facebook.getAccessToken();
    // when you login to facebook then you can access using above line for access Token
    alert(accTo);
});

— answered 3 years ago by Nitin Chavda
answer permalink
5 Comments
  • Hi.

    I just get the alert "Couldnt retreive the remote data."

    when i use this code.

    What have i done wrong?

    Thanx

    var win = Ti.UI.currentWindow;
     
    win.showNavBar();
     
    Titanium.UI.setBackgroundColor('#ffffff');
     
    var bgImage = Ti.UI.createImageView({
        height  : 'auto',
        top     : -50,
        fullscreen: true,
        image   : 'images/h-4.jpg',
        zIndex  : 0
    });
    win.add(bgImage);
     
     
    win.addEventListener("focus", function(e) {
        if (Ti.Facebook.loggedIn) {
     
        } else {
            Titanium.Facebook.authorize();
        }
    });
     
    Titanium.Facebook.addEventListener('login', function(e) {
        var accTo = Titanium.Facebook.getAccessToken();
        // when you login to facebook then you can access using above line for access Token
        alert(accTo);
    });
     
     
    var artist ='lucyseven';
     
    var url = "https://graph.facebook.com/" + artist + "/statuses";
     
    var table = Ti.UI.createTableView({
               top: 10,
               bottom: 10,
                backgroundColor: 'transparent',
                separatorColor:'transparent',
            });
    var tableData = [];
    var json, data, name, picture, description;
     
    var xhr = Ti.Network.createHTTPClient({
        onload: function() {
        // Ti.API.debug(this.responseText);
     
        json = JSON.parse(this.responseText);
        for (i = 0; i < json.data.length; i++) {
            data = json.data[i];
            row = Ti.UI.createTableViewRow({
                height:'90dp',
                backgroundColor: 'transparent',
                separatorColor:'transparent',
            });
          var  name = Ti.UI.createLabel({
                text: data.message,
                font:{
                    fontSize:'17dp',
                fontWeight:'bold'
            },
            height:'auto',
            left:'90dp',
            top:'8dp',
            color:'#111',
            touchEnabled:true
            });
            row.add(name);
     
     
            row.message = data.message;
            row.name = data.name;
     
     
            tableData.push(row);
            }
     
        table.setData(tableData);
        },
        onerror: function(e) {
        Ti.API.debug("STATUS: " + this.status);
        Ti.API.debug("TEXT:   " + this.responseText);
        Ti.API.debug("ERROR:  " + e.error);
        alert('There was an error retrieving the remote data. Try again.');
        },
        timeout:5000
    });
     
    xhr.open("GET", url);
    xhr.send();
     
    win.add(table);
    win.open();
    // Handle a click event on the table
            table.addEventListener('click',function(e) {
                // Create the new window with the link from the post
                var faceWindow = Ti.UI.createWindow({
                    title   : e.row.name,
                    data: e.row.message,           // Pass the row data to the new window for use
                    url: 'gallview.js',
                    barColor: '#99a8c7',
                    backgroundColor: '#ffffff'              
                });
     
     
                // Create the close button to go in the left area of the navbar popup
                var close = Titanium.UI.createButton({
                    title: 'Bak├ąt',
                    style: Titanium.UI.iPhone.SystemButtonStyle.PLAIN
                });
                faceWindow.setLeftNavButton(close);
     
                // Handle the close event
                close.addEventListener('click',function() {
                    faceWindow.close();
                });
     
     
     
                        // Open the window in the current tab
        Titanium.UI.currentTab.open(faceWindow, { animated:true });
            });

    — commented 3 years ago by Richard Harrysson

  • Hi Rickard,

    I think you need to add AccessToken in your url.

    — commented 3 years ago by Nitin Chavda

  • Ok.

    How do i get the var for tha token then?

    I have tried

    var url = "https://graph.facebook.com/" + artist + "/statuses" + accTo;
    With no result.

    — commented 3 years ago by Richard Harrysson

  • Show 2 more comments

YES!

Worked!

I just changed to /statuses?access_token= and no problem!

Thanx man!!

//R

— answered 3 years ago by Richard Harrysson
answer permalink
3 Comments
  • votes so i wrote this bit of code: Ti.Facebook.requestWithGraphPath('me?fields=name,picture', {}, 'GET', function(e) { if (e.success) { var response = JSON.parse(e.result); } else if (e.error) { alert("Error = "+e.error); } else { alert('Unknown response'); } }); var profileView = Ti.UI.createImageView({ image:'https://graph.facebook.com/'+Ti.Facebook.uid+'/picture?type=square', t op: 65, left: 20, height:70, });

    var url = 'https://graph.facebook.com/me/name';

    var httpClient = Titanium.Network.createHTTPClient({ onload: function() { var reply = JSON.stringify(this.responseText); Ti.API.info('user id ' + Titanium.Facebook.uid); Ti.API.info('user name: ' + reply); Ti.API.info('access token: ' + Ti.Facebook.accessToken); callback(reply);
    } }); httpClient.open("GET",url); httpClient.send();

    basically what i want to achieve is to show the users name from Facebook

    upon running the app i get the following error:

    [INFO] user name: "{\"error\":{\"message\":\"An active access token must be used to query information about thecurrent user.\",\"type\":\"OAuthException\",\"code\":2500}}"

    my question is where to i place the access token (as you can see i have found what the access token is via Ti.API.info('access token: ' + Ti.Facebook.accessToken); but i don't now how to implement it

    — commented 3 years ago by Napp Dev

  • i have made the necessary adjustment to the code by following this post:

    var winProfile = Ti.UI.createWindow({ backgroundColor:'#dededd', });

    var navbar4View = Ti.UI.createImageView({ image:'images/navbar4.png', width:320, height:46, top:-3, });

    btnBackToInterface.addEventListener('click', function (e){ winProfile.close(); });

    Ti.Facebook.requestWithGraphPath('me?fields=name,picture', {}, 'GET', function(e) { if (e.success) { var response = JSON.parse(e.result); } else if (e.error) { alert("Error = "+e.error); } else { alert('Unknown response'); }
    });

    var profileViewBg = Ti.UI.createView({ backgroundColor:'fff', top: 60, left: 15, width:75, height:75, });

    var profileView = Ti.UI.createImageView({ image:'https://graph.facebook.com/'+Ti.Facebook.uid+'/picture?type=large', top: 65, left: 20, height:70, });

    var url = 'https://graph.facebook.com/me/?fields=name/?access_token='+Ti.Facebook.getAccessToken();

    // var url = "https://graph.facebook.com/oauth/access_token?me/?fields=name";

    var httpClient = Titanium.Network.createHTTPClient({ onload: function() { var reply = JSON.stringify(this.responseText); Ti.API.info('user id ' + Titanium.Facebook.uid); Ti.API.info('user name: ' + reply); Ti.API.info('access token: ' + Ti.Facebook.accessToken); // callback(reply);
    } });
    httpClient.open("GET",url); httpClient.send();

    but i still get the error message staying that i need an active access token to query the info of the current user ....

    how to do this

    — commented 3 years ago by Napp Dev

  • var url = https://graph.facebook.com/?ids=' + Ti.Facebook.uid + '&access_token=' + Titanium.Facebook.getAccessToken()

    — commented 2 years ago by Jayesh Joshi

Your Answer

Think you can help? Login to answer this question!