using System.IO;
using System.Threading;
using Google.Apis.Calendar.v3;
using Google.Apis.Calendar.v3.Data;
using Google.Apis.Auth.OAuth2;
using Google.Apis.Auth.OAuth2.Flows;
using Google.Apis.Auth.OAuth2.Web;
using Google.Apis.Services;
using Google.Apis.Util.Store;
CalendarService calService; private const string calID = "xxxxxxxxx...@group.calendar.google.com"; private const string UserId = "user-id";
private static string gFolder = System.Web.HttpContext.Current.Server.MapPath("/App_Data/MyGoogleStorage");
public void Authenticate()
{
CalendarService service = null;
IAuthorizationCodeFlow flow = new GoogleAuthorizationCodeFlow(
new GoogleAuthorizationCodeFlow.Initializer
{
ClientSecrets = GetClientConfiguration().Secrets,
DataStore = new FileDataStore(gFolder),
Scopes = new[] { CalendarService.Scope.Calendar }
});
var uri = Request.Url.ToString();
var code = Request["code"];
if (code != null)
{
var token = flow.ExchangeCodeForTokenAsync(UserId, code,
uri.Substring(0, uri.IndexOf("?")), CancellationToken.None).Result;
// Extract the right state.
var oauthState = AuthWebUtility.ExtracRedirectFromState(
flow.DataStore, UserId, Request["state"]).Result;
Response.Redirect(oauthState);
}
else
{
var result = new AuthorizationCodeWebApp(flow, uri, uri).AuthorizeAsync(UserId,
CancellationToken.None).Result;
if (result.RedirectUri != null)
{
// Redirect the user to the authorization server.
Response.Redirect(result.RedirectUri);
}
else
{
// The data store contains the user credential, so the user has been already authenticated.
service = new CalendarService(new BaseClientService.Initializer
{
ApplicationName = "Calendar API Sample",
HttpClientInitializer = result.Credential
});
}
}
calService = service;
}
public static GoogleClientSecrets GetClientConfiguration()
{
using (var stream = new FileStream(gFolder + @"\client_secrets.json", FileMode.Open, FileAccess.Read))
{
return GoogleClientSecrets.Load(stream);
}
}
public string CreateUpdateEvent(string ExpKey, string ExpVal, string evTitle, string evDate)
{
EventsResource er = new EventsResource(calService);
var queryEvent = er.List(calID);
queryEvent.SharedExtendedProperty = ExpKey + "=" + ExpVal; //"EventKey=9999"
var EventsList = queryEvent.Execute();
Event ev = new Event();
EventDateTime StartDate = new EventDateTime();
StartDate.Date = evDate; //"2014-11-17";
EventDateTime EndDate = new EventDateTime();
EndDate.Date = evDate;
ev.Start = StartDate;
ev.End = EndDate;
ev.Summary = evTitle; //"My Google Calendar V3 Event!";
string FoundEventID = String.Empty;
foreach(var evItem in EventsList.Items)
{
FoundEventID = evItem.Id;
}
if (String.IsNullOrEmpty(FoundEventID))
{
//If event does not exist, Append Extended Property and create the event
Event.ExtendedPropertiesData exp = new Event.ExtendedPropertiesData();
exp.Shared = new Dictionary<string, string>();
exp.Shared.Add(ExpKey, ExpVal);
ev.ExtendedProperties = exp;
return er.Insert(ev, calID).Execute().Summary;
}
else
{
//If existing, Update the event
return er.Update(ev, calID, FoundEventID).Execute().Summary;
}
}
public bool DeleteEvent(string ExpKey, string ExpVal)
{
EventsResource er = new EventsResource(calService);
var queryEvent = er.List(calID);
queryEvent.SharedExtendedProperty = ExpKey + "=" + ExpVal; //"EventKey=9999"
var EventsList = queryEvent.Execute();
string FoundEventID = String.Empty;
foreach (Event ev in EventsList.Items)
{
FoundEventID = ev.Id;
er.Delete(calID, FoundEventID).Execute();
return true;
}
return false;
}
{
"web": {
"client_id": "18000002748-xxxxxxxxxxxxxxxxxxxxxxx.apps.googleusercontent.com",
"client_secret": "G0hxxxxxxxxxxxxxxxxx624"
}
}
I am sharing this because I don't want others to suffer. A working .NET code for Google Calendar API V3 is no where to be found. I hope this would be of help to everybody. Thank you ms. Lucia for the ideas. This is inspired from this post at Code Project: http://www.codeproject.com/Articles/565032/Google-Calendar-Integration-in-ASP-NET-Create-ed
using System.IO;
using System.Threading;
using Google.Apis.Calendar.v3;
using Google.Apis.Calendar.v3.Data;
using Google.Apis.Auth.OAuth2;
using Google.Apis.Auth.OAuth2.Flows;
using Google.Apis.Auth.OAuth2.Web;
using Google.Apis.Services;
using Google.Apis.Util.Store;
CalendarService calService;private const string calID = "xxxxxxxxxxxxxxxxxxx@group.calendar.google.com";private const string UserId = "user-id";
...
...
Creating, updating, deleting Google Calendar Event. This uses Extended Property instead of the built-in ID of an Event.
public string CreateUpdateEvent(string ExpKey, string ExpVal, string evTitle, string evDate)
{
EventsResource er = new EventsResource(calService);
var queryEvent = er.List(calID);
queryEvent.SharedExtendedProperty = ExpKey + "=" + ExpVal; //"EventKey=9999"
var EventsList = queryEvent.Execute();
Event ev = new
...Hi
...
...
...