Click here to Skip to main content
15,900,818 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi experts,
I want to add google calendar for event same as google contact in my mvc application
my need is i have a textbox for date and a button for add this date to google calender. and i have no idea how i implement this. if any one has the code for the same send me.

Thanks

What I have tried:

C#
var CLIENT_ID = 'myclientid';
var SCOPES = ["https://www.googleapis.com/auth/calendar"];

/**
 * Check if current user has authorized this application.
 */
function checkAuth() {
    alert('aa');
    gapi.auth.authorize(
      {
          'client_id': CLIENT_ID,
          'scope': SCOPES,
          'immediate': true
      }, handleAuthResult);
}

$("#GotoAuthSubLink").click(function () {
    handleAuthClickResult(event)
});
/**
 * Handle response from authorization server.
 *
 * param {Object} authResult Authorization result.
 */
function handleAuthResult(authResult) {
    alert('bb');
    var authorizeDiv = document.getElementById('GotoAuthSubLink');
    if (authResult.error) {
        // Hide auth UI, then load client library.
        //          authorizeDiv.style.display = 'none';

    } else {
        // Show auth UI, allowing the user to initiate authorization by
        // clicking authorize button.
        authorizeDiv.style.display = 'inline';
    }
}
function handleAuthClickResult(authResult) {
    alert('yes');
    var authorizeDiv = document.getElementById('GotoAuthSubLink');
    if (authResult && !authResult.error) {
        // Hide auth UI, then load client library.
        //authorizeDiv.style.display = 'none';
        loadCalendarApi();

    } else {
        // Show auth UI, allowing the user to initiate authorization by
        // clicking authorize button.
        authorizeDiv.style.display = 'inline';
        appendPre('Please try later!');
    }
}

/**
 * Initiate auth flow in response to user clicking authorize button.
 *
   * param {Event} event Button click event.
   */

  function handleAuthClick(event) {
    gapi.auth.authorize(
      {client_id: CLIENT_ID, scope: SCOPES, immediate: false},
      handleAuthClickResult);
    return false;
  }

  /**
   * Load Google Calendar client library. List upcoming events
   * once client library is loaded.
   */
  function loadCalendarApi() {
    gapi.client.load('calendar', 'v3', addEventToGoogleCalendar);
  }

  /**
   * Handle response from authorization server.
   *
     * param {string} message Text to be placed in pre element.
   */

  function appendPre(message) {
    document.getElementById('calendarOutput').innerHTML = message;
    alert(message);
  }


  function addEventToGoogleCalendar() {
      // Refer to the JavaScript quickstart on how to setup the environment:
      // https://developers.google.com/google-apps/calendar/quickstart/js
      // Change the scope to 'https://www.googleapis.com/auth/calendar' and delete any
      // stored credentials.

      var date = $("#datepicker3").val();
      alert(date);
      if (date.length < 6) {
          appendPre("Select a date");
      } else {

          var parts = date.split('/');
          var startDate = new Date(parts[2], parts[1] - 1, parts[0]);
          var endDate = new Date(startDate.getTime() + (12 * 1000 * 60 * 60));

          var eventAddress = $("#lblUserDetails").val();
          var eventName = $("#lblOwnerDetails").val();

          var event = {
              'summary': eventName,
              'location': eventAddress,
              'description': 'Connect with ' + eventName + ' at ' + eventAddress,
              'start': {
                  'dateTime': startDate.toISOString(),
                  'timeZone': 'Australia/Melbourne'
              },
              'end': {
                  'dateTime': endDate.toISOString(),
                  'timeZone': 'Australia/Melbourne'
              },
              'recurrence': [
                'RRULE:FREQ=DAILY;COUNT=2'
              ],
              'reminders': {
                  'useDefault': false,
                  'overrides': [
                    { 'method': 'email', 'minutes': 24 * 60 },
                    { 'method': 'sms', 'minutes': 10 }
                  ]
              }
          };

          var request = gapi.client.calendar.events.insert({
              'calendarId': 'primary',
              'resource': event
          });

          request.execute(function (event) {
              appendPre('Added successfully!');
          });
      }
  }
Posted
Updated 1-Apr-16 7:42am
v3

1 solution

Hello,

Please follow the below link. I hope that will help you some way.
ASP.NET MVC 4 using Google Calendar API via OAuth 2[^]

Thanks
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900