Tạo mới 1 calendar trên google
Để tạo mới 1 calendar trên google bằng ngôn ngữ C#, chúng ta làm theo các bước sau: Bước 1: tạo đối tượng CalendarService tên service cho tài khoản gmail mà bạn muốn tạo calendar. CalendarService service = new CalendarService("FrmGoogleCalendar"); service.setUserCredentials(txtEma ...
Để tạo mới 1 calendar trên google bằng ngôn ngữ C#, chúng ta làm theo các bước sau:
- Bước 1: tạo đối tượng CalendarService tên service cho tài khoản gmail mà bạn muốn tạo calendar.
CalendarService service = new CalendarService("FrmGoogleCalendar");
service.setUserCredentials(txtEmail.Text.Trim(), txtPassword.Text.Trim());
- Bước 2: tạo đối tượng CalendarEntry tên calendar, và thiết lập các thuộc tính thích hợp cho đối tượng calndar vừa tạo:
CalendarEntry calendar = new CalendarEntry();
calendar.Title.Text = "Góc Kinh Nghiệm";
calendar.Summary.Text = "www.gockinhnghiem.com";
calendar.TimeZone = "Asia/Saigon";
calendar.Hidden = false;
calendar.Color = "#2952A3";
calendar.Location = new Where("", "", "Hochiminh");
- Bước 3: tạo đối tượng Uri tên postUri, với địa chỉ “https://www.google.com/calendar/feeds/default/owncalendars/full” như sau:
Uri postUri = new Uri("https://www.google.com/calendar/feeds/default/owncalendars/full");
- Bước 4: dùng hàm Insert() của đối tượng service ở bước 1 để tạo mới một calendar:
CalendarEntry createdCalendar = (CalendarEntry)service.Insert(postUri, calendar);
Ghi chú:
Để sử dụng được các đối tượng CalendarService, CalendarEntry thì chương trình cần phải sử dụng 3 namespace bên dưới:
using Google.GData.Calendar; using Google.GData.Client; using Google.GData.Extensions;
Nếu bạn chưa có sẵn, bạn lên google tải “Google Data API Setup” về cài đặt, sau đó vào thư mục cài đặt trong ổ đĩa C: để copy 3 dll trên ra sử dụng. Bạn nhớ add 3 dll này vào References của project nhé.
Dưới đây là demo được viết trên VS2005, gồm các đối tượng sau:
- Một TextBox tên txtEmail dùng để nhập địa chỉ gmail tạo calendar
- Một TextBox tên txtPassword dùng để nhập password cho địa chỉ gmail trên.
- Một Button tên btnCreateCalendar và sự kiện click trên button này là btnCreateCalendar_Click. Khi nhấn lên button btnCreateCalendar thì hàm CreateCalendar() được gọi.
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
//
using Google.GData.Calendar;
using Google.GData.Client;
using Google.GData.Extensions;
namespace MyProject
{
public partial class FrmGoogleCalendar : Form
{
public FrmGoogleCalendar()
{
InitializeComponent();
}
private void btnCreateCalendar_Click(object sender, EventArgs e)
{
CreateCalendar();
}
private void CreateCalendar()
{
try
{
CalendarService service = new CalendarService("FrmGoogleCalendar");
service.setUserCredentials(txtEmail.Text.Trim(), txtPassword.Text.Trim());
CalendarEntry calendar = new CalendarEntry();
calendar.Title.Text = "Góc Kinh Nghiệm"; // Tên calendar
calendar.Summary.Text = "www.gockinhnghiem.com";
calendar.TimeZone = "Asia/Saigon";
calendar.Hidden = false;
calendar.Color = "#2952A3";
calendar.Location = new Where("", "", "Hochiminh");
Uri postUri = new Uri("https://www.google.com/calendar/feeds/default/owncalendars/full");
CalendarEntry createdCalendar = (CalendarEntry)service.Insert(postUri, calendar);
if (createdCalendar != null)
MessageBox.Show("Tạo calendar thành công");
else
MessageBox.Show("Tạo calendar thất bại");
}
catch (Exception ex)
{
MessageBox.Show(ex.ToString());
}
}
}
}
Dưới đây là link tham khảo về danh sách Timezone:
- http://www.timezoneconverter.com/cgi-bin/tzc.tzc?now=1&zone=Asia/Ho_Chi_Minh&tozone=GMT
- http://www.iselfschooling.com/mc4articles2/OracleTimeZone.htm
Chúc các bạn thành công! 