How to add events to Google Calendar using FullCalendar?

时光毁灭记忆、已成空白 提交于 2019-12-03 21:33:37

I found the answer

https://developers.google.com/google-apps/calendar/v3/reference/events/insert#examples

1)

//Global variables to access the calendar
     clientId = 'Your cliendID',
     scopes = 'https://www.googleapis.com/auth/calendar',
     calendarId = 'Your google calendar id',
     eventsList = [];


//Autorice the user
    checkAuth();

      //authorization in google
      function checkAuth() {
         gapi.auth.authorize(
            {
               'client_id': clientId,
               'scope': scopes,
               'immediate': true
            }, handleAuthResult);
      }

      //checks if authorized
      function handleAuthResult(authResult) {

         if (authResult && !authResult.error) {
            loadCalendarApi();
         } else {
            handleAuthClick();
         }
      }

      //request credentials
      function handleAuthClick() {
         gapi.auth.authorize(
            {
               client_id: clientId,
               scope: scopes,
               immediate: false
            }, handleAuthResult);
         return false;
      }

     function loadCalendarApi() {

         gapi.client.load('calendar', 'v3', makeApiCall);

      }`

2)

// Load the API and make an API call.  Display the results on the screen.
      


function makeApiCall() {

         requestList = gapi.client.calendar.events.list({
            'calendarId': calendarId
         });

         console.log('--- eventsList ---');
         console.log(eventsList);
         uiCalendarConfig.calendars['myCalendar'].fullCalendar('removeEventSource', eventsList);
         eventsList = [];

         // Step 6: Execute the API request
         requestList
            .then(function (resp) {

               if (resp.result.error) {

                  reportError('Google Calendar API: ' + data.error.message, data.error.errors);

               } else if (resp.result.items) {

                  resp.result.items.forEach(function (entry, index) {
                     eventsList.push({
                        id: entry.id,
                        title: entry.summary,
                        start: entry.start.dateTime || entry.start.date, // try timed. will fall back to all-day
                        end: entry.end.dateTime || entry.end.date, // same
                        url: url,
                        location: entry.location,
                        description: entry.description
                     });
                  });

               }

               if (eventsList.length > 0) {
                  uiCalendarConfig.calendars['myCalendar'].fullCalendar('addEventSource', eventsList, true);
               }

            }, function (reason) {
               console.log('Error: ' + reason.result.error.message);
            });
      }`

3)

//insert into calendar



      function makeRpcRequest(eventData) {

         gapi
            .client
            .load('calendar', 'v3')
            .then(function () {
               request = gapi.client.calendar.events.insert({
                  'calendarId': calendarId,
                  'resource': eventData
               });

               request.then(function (resp) {

                  if (resp.result.error) {
                     reportError('Google Calendar API: ' + data.error.message, data.error.errors);
                  } else {

                     makeApiCall();
                     console.log(resp);
                     var creator = resp.result.creator.email;
                     var calendarEntry = resp.result.htmlLink;

                     console.log('--- Calendar entry successfully created by---');
                     console.log(creator);
                     console.log('--- dd ---');
                     console.log(calendarEntry);
                  }
               }, function (reason) {
                  console.log('Error: ' + reason.result.error.message);

               });
            });
      }`

 
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!