using System;
using System.Diagnostics;
using System.Linq;
using System.Windows.Forms;
using DotNetOpenAuth.OAuth2;
using Google.Apis.Authentication.OAuth2;
using Google.Apis.Authentication.OAuth2.DotNetOpenAuth;
using Google.Apis.Services;
using Google.Apis.Util;
using Google.Apis.Calendar.v3;
using Google.Apis.Calendar.v3.Data;
using GoogleCalendar;
namespace TestGoogleCalendar
{
public partial class frmMain : Form
{
private Google.Apis.Calendar.v3.CalendarService service;
public frmMain()
{
InitializeComponent();
listView1.Sorting = SortOrder.Ascending;
}
private void frmMain_Load(object sender, EventArgs e)
{
TestMethod1();
}
private static IAuthorizationState GetAuthorization(NativeApplicationClient arg)
{
// Get the auth URL:
IAuthorizationState state = new AuthorizationState(new[] { CalendarService.Scopes.Calendar.GetStringValue() });
state.Callback = new Uri(NativeApplicationClient.OutOfBandCallbackUrl);
Uri authUri = arg.RequestUserAuthorization(state);
// Request authorization from the user (by opening a browser window):
Process.Start(authUri.ToString());
frmGetToken fToken = new frmGetToken();
fToken.ShowDialog();
string authCode = fToken.Token;
// Retrieve the access token by using the authorization code:
return arg.ProcessUserAuthorization(authCode, state);
}
public void TestMethod1()
{
var provider = new NativeApplicationClient(GoogleAuthenticationServer.Description)
{
ClientIdentifier = "xxxx.apps.googleusercontent.com",
ClientSecret = "zzzzz"
};
var auth = new OAuth2Authenticator<NativeApplicationClient>(provider, GetAuthorization);
service = new Google.Apis.Calendar.v3.CalendarService(new BaseClientService.Initializer()
{
Authenticator = auth
});
CalendarList cl = service.CalendarList.List().Fetch();
comboBox1.Items.Clear();
foreach (CalendarListEntry cle in cl.Items)
{
comboBox1.Items.Add(new CalendarEntry(cle));
}
}
private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
{
LoadDay();
}
private void dateTimePicker1_ValueChanged(object sender, EventArgs e)
{
LoadDay();
}
private void dateTimePicker2_ValueChanged(object sender, EventArgs e)
{
LoadDay();
}
private void LoadDay()
{
listView1.Items.Clear();
if (comboBox1.SelectedItem == null) return;
EventsResource.ListRequest lr = service.Events.List((comboBox1.SelectedItem as CalendarEntry).Id);
Events e = lr.Fetch();
foreach (Event s in e.Items)
{
if (s.Status == "cancelled" ) continue;
string a = (s.Start.DateTime != null) ? s.Start.DateTime : s.Start.Date;
DateTime dt = DateTime.Parse(a);
if (dt >= dateTimePicker1.Value.Date && dt < dateTimePicker2.Value.AddDays(1).Date)
{
ListViewItem i = new ListViewItem(new []{dt.ToString(), s.Summary});
i.Tag = s;
listView1.Items.Add(i);
}
}
}
private void listView1_DoubleClick(object sender, EventArgs e)
{
if (listView1.SelectedItems[0].Tag != null)
{
frmCalendarEntry entry = new frmCalendarEntry(listView1.SelectedItems[0].Tag as Event);
entry.ShowDialog();
service.Events.Update(entry.evt, (comboBox1.SelectedItem as CalendarEntry).Id, entry.evt.Id);
LoadDay();
}
}
}
}