ejci / Google-Auth-for-Titanium

Google Auth for Titanium
GNU General Public License v2.0
66 stars 28 forks source link

About loading Google plus people lists in circles #8

Open nir777 opened 9 years ago

nir777 commented 9 years ago

Hi i'm new to titanium..i'm getting empty page when i try to load g+ people in circles..i have posted my code below...

Alloy.js::

Alloy.Globals.GoogleAuth_module = require('googleAuth');

Alloy.Globals.googleAuth = new Alloy.Globals.GoogleAuth_module({ //clientId : '219575370718-u3vb42f04899h02es4mj4uh34otgr5pe.apps.googleusercontent.com', clientId : '533122259018-2af78f7d00okfssi8i550k4ngnm28qhk.apps.googleusercontent.com', clientSecret : '*****', propertyName : 'googleToken', quiet: false, scope : [ 'https://www.googleapis.com/auth/tasks', 'https://www.googleapis.com/auth/tasks.readonly','https://www.googleapis.com/auth/plus.login','https://www.googleapis.com/auth/plus.me' ] });

I have added scopes for g+ people lists in scope parameter...

My index.xml page:

``` ```

Controller page (index.js):

$.index.open();

$.index.addEventListener('open', function(e) { var activity = $.index.getActivity(); activity.invalidateOptionsMenu(); //force reload of menus
});

var googleAuth = Alloy.Globals.googleAuth;

function menuItemLogout_click() { googleAuth.deAuthorize(); //googleAuth.refreshToken(); $.table.setData([]); }

function menuItemSync_click() { Ti.API.info('Authorized: ' + googleAuth.isAuthorized()); googleAuth.isAuthorized(function() { Ti.API.info('Access Token: ' + googleAuth.getAccessToken()); //empty table view $.table.setData([]);

        var xhrList = Ti.Network.createHTTPClient({
            // function called when the response data is available
            onload : function(e) {
                try {
                    var resp = JSON.parse(this.responseText);
                    alert('test2');
                    for (var i = 0; i < resp.items.length; i++) {
                        //GET DATA FOR LIST
                        for (var j = 0; j < resp.items.length; j++) {
                                    if (resp.items[j].title != '') {
                                        var row = Titanium.UI.createTableViewRow({
                                            leftImage : resp.items[j].image.url,
                                            title : resp.items[j].displayName,
                                            height:70
                                        });
                                        alert(resp.items[j].displayName);
                                        $.table.appendRow(row);
                                    }
                                }

                    }
                } catch(e) {
                    Titanium.UI.createAlertDialog({
                        title : 'Error',
                        message : 'Can\'t load people for list' 
                    });
                    Ti.API.error('RESPONSE: '+JSON.stringify(e));
                }
            },
            // function called when an error occurs, including a timeout
            onerror : function(e) {
                Titanium.UI.createAlertDialog({
                    title : 'Error',
                    message : 'Can\'t load peoplelist'
                });
                Ti.API.error('HTTP: '+JSON.stringify(e));
            },
            timeout : 5000
        });
        xhrList.open("GET", 'https://www.googleapis.com/plus/v1/people/me/people/visible?access_token=' + googleAuth.getAccessToken());
        xhrList.send();
    }, function() {
        Ti.API.info('Authorize google account...');
        googleAuth.authorize();
    });

}

Please help me fix this issue..and apologize if i have done mistakes in code.....

ejci commented 9 years ago

can you also share log output?

nir777 commented 9 years ago

Hi Miroslav thanks for the reply and i fixed that issue ... i was trying to login the user who has zero friends in G+.. so it doesn't enter into for loop...

But still i'm facing another issue..when i click a button it redirects to the google website,get authorized and doesnt load friends lists after that..

when i click the same button again (second time), friends list gets loaded...

I have attached my button click code below...

function menuItemSync_click() { Ti.API.info('Authorized: ' + googleAuth.isAuthorized()); googleAuth.isAuthorized(function() { Ti.API.info('Access Token: ' + googleAuth.getAccessToken()); //empty table view //$.table.setData([]); var xhrList = Ti.Network.createHTTPClient({ // function called when the response data is available onload : function(e) { try { Ti.API.info('Access Token:inside on load'); var resp = JSON.parse(this.responseText); Ti.API.info(this.responseText);
// var data = []; for (var j = 0; j < resp.items.length; j++) { if (resp.items[j].title != '') { var row = Titanium.UI.createTableViewRow({ leftImage : resp.items[j].image.url, title : resp.items[j].displayName, height:30 }); Ti.API.info(resp.items[j].displayName); //alert(resp.items[j].displayName); $.table.appendRow(row); } }

                } catch(e) {
                    Titanium.UI.createAlertDialog({
                        title : 'Error',
                        message : 'Can\'t load tasks for list' 
                    });
                    Ti.API.error('RESPONSE: '+JSON.stringify(e));
                }
            },
            // function called when an error occurs, including a timeout
            onerror : function(e) {
                Titanium.UI.createAlertDialog({
                    title : 'Error',
                    message : 'Can\'t load tasklists'
                });
                Ti.API.error('HTTP: '+JSON.stringify(e));
            },
            timeout : 500000
        });
        xhrList.open("GET", 'https://www.googleapis.com/plus/v1/people/me/people/visible?access_token=' + googleAuth.getAccessToken());
        xhrList.send();
    }, function() {
        Ti.API.info('Authorize google account...');
        googleAuth.authorize();
    });

}

kindly assist me for the solution.......

ejci commented 9 years ago

In your case I would do something like this:

//button click handler
function menuItemSync_click() {
   googleAuth.isAuthorized(function() {
      //is authorized so load data
      loadDataInTable();
   }, function() {
      //authorize and then load the data
      googleAuth.authorize(function(){
         //is authorized so load data
         loadDataInTable();
      });
   });
}
//loading the data
function loadDataInTable(){
   //XHR request to get the data and append it to table...
}

Let me know if you have any question or if its not clear