Click here to Skip to main content
12,355,591 members (72,056 online)
Click here to Skip to main content
Add your own
alternative version

Stats

132.8K views
4.6K downloads
46 bookmarked
Posted

Google Calendar (v3) Integration in ASP.NET: Create / edit / delete events

, 25 Feb 2014 CPOL
Rate this:
Please Sign up or sign in to vote.
How to programmatically create / edit / delete events in the Google calendar (v3) of a Gmail user.

Introduction

This article describes how to programmatically create / edit / delete events in the Google calendar (v3) of a Gmail user. The developer needs to create a project on Google API Console and use the credentials (ClientID, ClientSecret, and Redirect URL) to sync events on any Gmail user registered with the project.

Using the Code

My intention is to let the user walk through the code so that he may be able to sync events of a web application (event management system, reservation system, etc.) with a user's Google Calendar.

IDE

  • Visual Studio (I have used 2013)

Description of Files

First, I will give a brief description of the files used in this project.

565032/SolExplorer.jpg

  • App_Code
    • GoogleCalendarAppointmentModel.cs - This file is used to keep the contents that need to be sent on Google Calendar
    • GoogleCalendarManager.cs - This file contains all the functions to interact with Google Calendar i.e., read / write events, access the calendar, etc.
    • GoogleTokenModel.cs - This file's contents are used to get the response from the Google Calendar API, i.e., the AccessToken (will be required for identification, will expire after a certain period of time), Refresh Token (will not expire and is used to get a new AccessToken).
  • App_Data
    • XMLFile.xml - This file server is the DB for this project. To keep it simple, this project works with a single user and single event, i.e.,
      • You register the user with the Google API project so that his Google Calendar can be accessed programmatically
      • You create / edit / delete an event through the Visual Studio project and view the changes on Google Calendar
      • You can change your login credentials. Currently they are set to UserName=Waqas, Pwd = 111
      • When you register with Google calendar, you will get a new AccessToken and RefreshToken which will be saved here
      • When you create / update an event, its details are stored here.
      • When you delete event, these details are removed, as you can see below that there are no values for the event attributes
      • When you revoke rights, your AccessToken and RefreshToken are removed from here
      • You don't need to edit this file manually, except the username, pwd as this may result in exceptions.
  • Default.aspx - The main page where you can register, revoke, create / edit / delete events from Google Calendar

Creating Project on Google API Console

  • Goto https://console.developers.google.com and create a new project
  • Click the API under API & Auth and click on the calendar API under 
    • Click Enable API
  • Click the Credentials under API & Auth 
    • Click Add Credentials and click oAuth 2.0 clientID 
    • select Configure consent screen
    • Provide product name, logo (optional) and Home page URL (optional). and click save.
    • Choose web application as Application type and provide the URL of your project (e.g. http://localhost:58392/GoogleCalandarDemo/Default.aspx this is the page where google will send the token and other credentials to access google calendar) and click create
    • Click on your project name under OAuth 2.0 client ID (web client 1 is the default name
    • Click Download JSON and save the file (I have added MyGoogleStorage folder in App_Data and placed this file inside. It contains the credentials for the project).
    • The MyGoogleStorage folder will be used for creating a temporary and a permanent file having Access token credential 
      • A temporary file will be created when redirected to google calendar
      • A new file Google.Apis.Auth.OAuth2.Responses.TokenResponse-m will be created upon successful registration

 

View the images below if you find anything confusing with the text.

 

 

Before running the code, make sure that

  1. Change the values of UserID and calID in default.aspx.cs
  2. You have downloaded JSON client_secrets from https://console.developers.google.com and included it in MyGoogleStorage folder 
  3. You have added calendar v3 from package manage console by writing Install-Package Google.Apis.Calendar.v3
 
Walkthrough the code (you will have to follow the sequence as this code was moved from calendar v2 to v3)
  1. When you run the code 1st time you will see the login screen where you can put Waqas and 111 to pass through (these values are used from the XML file, you can change it or remove this portion. this is just to mimic the behavior of normal website where you need to login)
  2. After login, you need to type the address (in my case http://localhost:26033/default.aspx) again to get to the screen to register (just a glitch left)
  3. Click the Register with google calendar button and allow the manage calendar rights
  4. You have successfully registered the application with you google calendar but you need to get to the create event screen now. You need to click the button again to do that
  5. Put the values on the create event screen and click Create / Update event
  6. You will see the Event Created successfully message. You need to click the Google Calendar button to view the event on your google calendar

You need to follow the above sequence for 1st time registration. For next time after login, you will be redirected to create /  update event screen and things will go smooth

 

To start over
If you ever want to check the code from start (when the user is not registered), you need to revoke the rights from https://security.google.com/settings/security/permissions and remove the newly created file (Google.Apis.Auth.OAuth2.Responses.TokenResponse-) from your MyGoogleStorage folder
 
The XML file only serves the purpose to mimic the behavior of db. You need to pay attention to how to register a client with google calendar and after that how to upload the event on the registered client's calendar

Hope this is all. I'll revise the code and remove the minor issues (for which you need to follow the sequence)

License

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

Share

About the Author

m.waqasiqbal
Software Developer (Senior) Freelancer
Pakistan Pakistan
No Biography provided

You may also be interested in...

Comments and Discussions

 
QuestionDownload link not working Pin
Member 1241636525-Mar-16 4:37
memberMember 1241636525-Mar-16 4:37 
Questiondownload link is not working Pin
Member 1080296713-Mar-16 20:15
memberMember 1080296713-Mar-16 20:15 
Questiondownload link is out of servise Pin
Mohsen ms9-Nov-15 12:12
memberMohsen ms9-Nov-15 12:12 
QuestionPlease do an updated v3 article Pin
Member 116616403-May-15 17:47
memberMember 116616403-May-15 17:47 
QuestionTried but getting error Pin
Schatak17-Mar-15 21:00
professionalSchatak17-Mar-15 21:00 
AnswerRe: Tried but getting error Pin
m.waqasiqbal18-Mar-15 3:18
memberm.waqasiqbal18-Mar-15 3:18 
QuestionGoogle Calender - Application integration Pin
Member 114906241-Mar-15 22:26
memberMember 114906241-Mar-15 22:26 
AnswerRe: Google Calender - Application integration Pin
m.waqasiqbal11-Mar-15 18:52
memberm.waqasiqbal11-Mar-15 18:52 
QuestionThe remote server returned an error: (403) Forbidden. Pin
nisharkaliya26-Feb-15 20:05
membernisharkaliya26-Feb-15 20:05 
Questionimplementation google calendar V3 Pin
Member 1134297729-Dec-14 22:36
memberMember 1134297729-Dec-14 22:36 
QuestionHope this help: V3 version of this. Pin
Hewbert Gabon19-Dec-14 10:03
memberHewbert Gabon19-Dec-14 10:03 
AnswerRe: Hope this help: V3 version of this. Pin
m.waqasiqbal19-Dec-14 22:02
memberm.waqasiqbal19-Dec-14 22:02 
QuestionThis integration V2, but a need integration V3 Pin
Member 1128421810-Dec-14 1:29
memberMember 1128421810-Dec-14 1:29 
QuestionRetrieve ExtendedProperty of Google Calendar v3? Pin
Hewbert Gabon23-Nov-14 2:07
memberHewbert Gabon23-Nov-14 2:07 
AnswerRe: Retrieve ExtendedProperty of Google Calendar v3? Pin
m.waqasiqbal26-Nov-14 17:38
memberm.waqasiqbal26-Nov-14 17:38 
GeneralRe: Retrieve ExtendedProperty of Google Calendar v3? Pin
Hewbert Gabon19-Dec-14 10:10
memberHewbert Gabon19-Dec-14 10:10 
GeneralRe: Retrieve ExtendedProperty of Google Calendar v3? Pin
m.waqasiqbal13-Sep-15 0:27
memberm.waqasiqbal13-Sep-15 0:27 
QuestionError (403) when create new event Pin
bitsnbytes20-Nov-14 8:11
memberbitsnbytes20-Nov-14 8:11 
AnswerRe: Error (403) when create new event Pin
m.waqasiqbal20-Nov-14 18:05
memberm.waqasiqbal20-Nov-14 18:05 
GeneralGreat job...but API v2 now deprecated as of 17/11/14 - upgrade to v3, anybody? Pin
Member 1124543418-Nov-14 20:31
memberMember 1124543418-Nov-14 20:31 
GeneralRe: Great job...but API v2 now deprecated as of 17/11/14 - upgrade to v3, anybody? Pin
m.waqasiqbal19-Nov-14 1:57
memberm.waqasiqbal19-Nov-14 1:57 
QuestionCan you provide a V3 variation of this? Pin
Member 233046418-Nov-14 18:49
memberMember 233046418-Nov-14 18:49 
AnswerRe: Can you provide a V3 variation of this? Pin
m.waqasiqbal19-Nov-14 1:52
memberm.waqasiqbal19-Nov-14 1:52 
QuestionFetch google calendar events Pin
Milind Singh11-Oct-14 0:47
memberMilind Singh11-Oct-14 0:47 
GeneralMy vote of 5 Pin
Humayun Kabir Mamun18-Sep-14 4:14
memberHumayun Kabir Mamun18-Sep-14 4:14 
QuestionHow to add event without move Gmail Pin
Debapriya Sahoo17-Jul-14 3:19
memberDebapriya Sahoo17-Jul-14 3:19 
AnswerRe: How to add event without move Gmail Pin
m.waqasiqbal17-Jul-14 6:12
memberm.waqasiqbal17-Jul-14 6:12 
GeneralRe: How to add event without move Gmail Pin
Debapriya Sahoo17-Jul-14 18:33
memberDebapriya Sahoo17-Jul-14 18:33 
GeneralRe: How to add event without move Gmail Pin
m.waqasiqbal18-Jul-14 3:40
memberm.waqasiqbal18-Jul-14 3:40 
Questioninvalid_client Pin
Braneloc12-Jul-14 10:04
memberBraneloc12-Jul-14 10:04 
AnswerRe: invalid_client Pin
m.waqasiqbal17-Jul-14 6:12
memberm.waqasiqbal17-Jul-14 6:12 
QuestionThanks fort your reply Pin
piyush201124-Mar-14 4:14
memberpiyush201124-Mar-14 4:14 
AnswerRe: Thanks fort your reply Pin
m.waqasiqbal24-Mar-14 4:48
memberm.waqasiqbal24-Mar-14 4:48 
QuestionHow will i use this code for storing multiple users Pin
piyush201124-Mar-14 2:39
memberpiyush201124-Mar-14 2:39 
AnswerRe: How will i use this code for storing multiple users Pin
m.waqasiqbal24-Mar-14 3:13
memberm.waqasiqbal24-Mar-14 3:13 
QuestionI want to google calendar access Pin
saylibhagwat20-Feb-14 20:00
membersaylibhagwat20-Feb-14 20:00 
AnswerRe: I want to google calendar access Pin
m.waqasiqbal22-Feb-14 19:19
memberm.waqasiqbal22-Feb-14 19:19 
QuestionAccess Token verification Pin
AhmadFarouq27-Nov-13 1:27
memberAhmadFarouq27-Nov-13 1:27 
AnswerRe: Access Token verification Pin
m.waqasiqbal27-Nov-13 19:21
memberm.waqasiqbal27-Nov-13 19:21 
GeneralRe: Access Token verification Pin
AhmadFarouq28-Nov-13 0:56
memberAhmadFarouq28-Nov-13 0:56 
GeneralRe: Access Token verification Pin
m.waqasiqbal2-Dec-13 0:15
memberm.waqasiqbal2-Dec-13 0:15 
QuestionCalendar colors Pin
jasenf6-Jun-13 10:11
memberjasenf6-Jun-13 10:11 
AnswerRe: Calendar colors Pin
m.waqasiqbal6-Jun-13 23:57
memberm.waqasiqbal6-Jun-13 23:57 
GeneralMy vote of 5 Pin
Renju Vinod21-Mar-13 2:38
memberRenju Vinod21-Mar-13 2:38 
GeneralRe: My vote of 5 Pin
m.waqasiqbal21-Mar-13 3:05
memberm.waqasiqbal21-Mar-13 3:05 
GeneralMy vote of 5 Pin
mohsinshahbaz21-Mar-13 1:43
membermohsinshahbaz21-Mar-13 1:43 
GeneralRe: My vote of 5 Pin
m.waqasiqbal21-Mar-13 3:06
memberm.waqasiqbal21-Mar-13 3:06 
GeneralGreat ! Thanks Pin
mohsinshahbaz21-Mar-13 1:13
membermohsinshahbaz21-Mar-13 1:13 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.160621.1 | Last Updated 26 Feb 2014
Article Copyright 2013 by m.waqasiqbal
Everything else Copyright © CodeProject, 1999-2016
Layout: fixed | fluid