Add Event in Google Calendar Using Javascript

Add Event in Google Calendar Using Javascript with google authentication.

More information visit on
https://developers.google.com/google-apps/calendar/


<a href="javascript:void(0)" class="addEvent-button" id="addEvent-button"  onclick="addEventOnClick('<?php echo htmlentities($array); ?>');"><img src="img/gcalendar.png" title="Add to google calendar" style="height:24px !important;width:24px !important;"></a>

<script type="text/javascript">
    var clientId = 'CLIENTID';
    var apiKey = 'APP KEY';
    var scopes = 'https://www.googleapis.com/auth/calendar';
    // Oauth2 functions
    function handleClientLoad() {
        gapi.client.setApiKey(apiKey);
    }
   
    function addEventOnClick(data)
    {
        gapi.auth.authorize({client_id: clientId, scope: scopes, immediate: false}, handleAuthResult);
        function handleAuthResult(authResult)
        {
            if (authResult && !authResult.error) {
                makeApiCall(data);
            }
        }
        return false;
    }
   
    // function load the calendar api and make the api call
    function makeApiCall(eventDetails)
    {
        var obj = JSON.parse(eventDetails);
        var resource = {
            "summary": obj.eventName,
            "location": obj.location,
            "start": {
                "dateTime": obj.startDate
             },
                "end": {
                "dateTime": obj.enddate
             }
        };
       
        gapi.client.load('calendar', 'v3', function()
        { // load the calendar api (version 3)
            var request = gapi.client.calendar.events.insert({
                'calendarId': 'primary', // calendar ID
                "resource": resource // pass event details with api call
            });
            // handle the response from our api call
            request.execute(function(resp)
            {
                  console.log(resp);
          if(resp.status=='confirmed') {
            console.log('Created');
          }
            });
        });
    }
    </script>
    <script src="https://apis.google.com/js/client.js?onload=handleClientLoad"></script>

Comments

Popular posts from this blog

Login with facebook using coldfusion

Create CSV file in Coldfusion Using CFFile

Get Previous One Day Data in Sql Server