javascript - Try to display calendar list from Google API using Java Script - Stack Overflow

I have some troubles to display list of accesible calendar from Google Calendar via Google API. I use J

I have some troubles to display list of accesible calendar from Google Calendar via Google API. I use JavaScript and AJAX.

What methods do I need to use? I found only event`s related methods, but not for display description of calendar.

Thank you!

I have some troubles to display list of accesible calendar from Google Calendar via Google API. I use JavaScript and AJAX.

What methods do I need to use? I found only event`s related methods, but not for display description of calendar.

Thank you!

Share Improve this question asked Apr 30, 2015 at 17:19 JeygidJeygid 111 silver badge2 bronze badges 1
  • Do you have any sample code of what you have tried so far? – Kmeixner Commented Apr 30, 2015 at 17:29
Add a ment  | 

2 Answers 2

Reset to default 8

To get a basic working model you can follow the quickstart example on the calendar API site

The example has a function called listUpingEvents() to get events on the "primary" calendar. To get the list of calendars use the following method:

function listCalendars()
{
     var request = gapi.client.calendar.calendarList.list();

     request.execute(function(resp){
             var calendars = resp.items;
             console.log(calendars);
     });
}

Here's some code I just wrote:

kb.loadAsync('https://apis.google./js/client.js', 'onload', 'gapi').then(gapi => {
    gapi.auth.authorize({
        client_id: __GOOGLE_CALENDAR_API_KEY__,
        scope: 'https://www.googleapis./auth/calendar',
        immediate: true,
    }, authResult => {
        if(authResult && !authResult.error) {
            gapi.client.load('calendar','v3', () => {
                gapi.client.calendar.calendarList.list({
                    maxResults: 250,
                    minAccessRole: 'writer',
                }).execute(calendarListResponse => {
                    let calendars = calendarListResponse.items;
                    console.log(calendars.map(cal => cal.summary));
                });
            });
        } else {
            console.log('unauthorized');
        }
    });
});

kb.loadAsync is a helper function I wrote; looks like this:

/**
 * Helps load Google APIs asynchronously.
 *
 * @param {string} source
 * @param {string} callbackParam
 * @param {string=} globalName
 * @returns {Promise}
 */
export function loadAsync(source, callbackParam, globalName) {
    return new Promise((resolve,reject) => {
        let callbackFunc = Math.random().toString(36);

        window[callbackFunc] = () => {
            resolve(window[globalName]);
            delete window[callbackFunc];
        };

        let sep = source.includes('?') ? '&' : '?';
        $script(`${source}${sep}${encodeURIComponent(callbackParam)}=${encodeURIComponent(callbackFunc)}`);
    });
}

It uses scriptjs. Unfortunately scriptjs' callback fires too early -- Google loads some more junk after client.js has downloaded, so it isn't quite ready to run even after it's "ready"! You have to use Google's onload parameter.

You don't need all those dependencies if you just to use a bunch of <script> tags, but I prefer me some async functions.

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1742344583a4426290.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信