Gdata JavaScript Authsub继续重定向

Kru*_*tal 5 javascript google-calendar-api authsub google-data-api

我正在使用JavaScript Google Data API,并且在让AuthSub脚本正常运行时遇到问题.这是我目前的脚本:

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';

        // The 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);
Run Code Online (Sandbox Code Playgroud)

但是,当我运行此页面时,页面会将我重定向到身份验证页面.在我进行身份验证后,将我发回给我的页面,然后再次将我发回到身份验证页面.我已经包含警报来检查是否正在设置令牌并且它似乎不起作用.有人有这个问题吗?

Anu*_*rag 1

问题是,当 google 重定向回您的网站时,设置 cookie 需要一些时间。但是,回调会立即运行,并且当时没有 cookie 来验证身份验证,因此它再次重定向回 google。尝试使用 setTimeout 或其他方法在一秒钟左右后运行身份验证检查以确保确定。