开发者

Gdata JavaScript Authsub continues redirect

I am using the JavaScript Google Data API and having issues getting the AuthSub script to work correctly. This is my script currently:

google.load('gdata', '1');

function getCookie(c_name){
    if(document.cookie.length>0){
        c_start=document.cookie.indexOf(c_name + "=");
        if(c_start!=-1){
            c_start=c_start + c_name.length+1;
            c_end=document.cookie.indexOf(";",c_start);
            if(c_end==-1) c_end=document.cookie.length;
            return unescape(document.cookie.substring(c_start, c_end));
        }
    }
    return "";
}

function main(){
    var scope = 'http://www.google.com/calendar/feeds/';
    if(!google.accounts.user.checkLogin(scope)){
        google.accounts.user.login();
    } else {
        /*
        * Retrieve all calendars
        */

        // Create the calendar service object
        var calendarService = new google.gdata.calendar.CalendarService('GoogleInc-jsguide-1.0');

        // The default "allcalendars" feed is used to retrieve a list of all
        // calendars (primary, secondary and subscribed) of the logged-in user
        var feedUri = 'http://www.google.com/calendar/feeds/default/allcalendars/full';

        // T开发者_开发百科he callback method that will be called when getAllCalendarsFeed() returns feed data
        var callback = function(result) {

          // Obtain the array of CalendarEntry
          var entries = result.feed.entry;

          //for (var i = 0; i < entries.length; i++) {
            var calendarEntry = entries[0];
            var calendarTitle = calendarEntry.getTitle().getText();
            alert('Calendar title = ' + calendarTitle);
          //}
        }

        // Error handler to be invoked when getAllCalendarsFeed() produces an error
        var handleError = function(error) {
          alert(error);
        }

        // Submit the request using the calendar service object
        calendarService.getAllCalendarsFeed(feedUri, callback, handleError);
    }
}

google.setOnLoadCallback(main);

However when I run this the page redirects me to the authentication page. After I authenticate it send me back to my page and then quickly sends me back to the authenticate page again. I've included alerts to check if the token is being set and it doesn't seem to be working. Has anyone has this problem?


I was having the same problem so I built this function

function login() {
    var scope = "http://www.google.com/calendar/feeds/";
    if(!google.accounts.user.checkLogin(scope)) {
        if(google.accounts.user.getStatus() == 0) {
            var token = google.accounts.user.login();
        }
    }
}

I added the check to google.accounts.user.getStatus() if it's 1 that means the application is in the process of logging in and if it is 2 that means the applications is logged in. You can also pass a scope to the getStatus method.


the problem is that setting the cookie takes a little while when google redirects back to your site. However, the callback runs immediately, and there is no cookie by that time to verify authentication, so it again redirects back to google. Try using setTimeout or something to run the authentication check after a second or so to be sure.


You should pass the scope to the login method too.


Sometimes you can end up with an orphaned cookie in your browser - which will keep getting fed back to Google.

What I'm doing now, is doing a checkLogin before I perform my login call, and if it returns true I explicitly call logOut().

The logOut call will remove any cookies which Google had rejected but left in your browser. The reason it seems to keep going in a loop is because the cookie is there, but even on reauth, it doesn't produce a new one because you already have one. But unfortunately for our sake, the one that's there is invalid.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜