Gmail क्लाइंट का उपयोग करके Google कैलेंडर्स प्रबंधित करें
Gmail कैलेंडर जोड़ें, संपादित करें और हटाएँ
Aspose.Email अनुप्रयोगों को Gmail कैलेंडरों को उपयोग करके प्रबंधित करने की अनुमति देता है IGmailClient जो Gmail कैलेंडरों को जोड़ने, हटाने और अपडेट करने जैसी सुविधाएँ प्रदान करता है। यह क्लाइंट क्लास ExtendedCalendar प्रकार के ऑब्जेक्ट्स की सूची लौटाता है जिसमें Gmail कैलेंडर आइटम की जानकारी होती है। IGmailClient क्लास कैलेंडरों के लिए निम्नलिखित फ़ंक्शन उजागर करता है:
- CreateCalendar नया कैलेंडर डालने के लिए इसे इस्तेमाल किया जा सकता है
- ListCalendars इसे क्लाइंट के सभी कैलेंडरों की सूची प्राप्त करने के लिए उपयोग किया जा सकता है
- DeleteCalendar इसे कैलेंडर को हटाने के लिए उपयोग किया जा सकता है
- FetchCalendar इसे क्लाइंट के विशिष्ट कैलेंडर को लाने के लिए उपयोग किया जा सकता है
- UpdateCalendar इस फ़ंक्शन का उपयोग क्लाइंट के संशोधित कैलेंडर को वापस डालने के लिए किया जाता है
कैलेंडर्स तक पहुँचने के लिए, Gmail खाते की क्रेडेंशियल्स का उपयोग कर GoogleTestUser इनिशियलाइज़ किया जाता है। GoogleOAuthHelper उपयोगकर्ता के लिए एक्सेस टोकन प्राप्त करता है, जिसे आगे IGmailClient इनिशियलाइज़ करने में प्रयोग किया जाता है।
Gmail कैलेंडर डालें, प्राप्त करें और अपडेट करें
कैलेंडर डालने के लिए, Calendar प्रकार का ऑब्जेक्ट इनिशियलाइज़ करें और इसका उपयोग करके डालें CreateCalendar() फ़ंक्शन। CreateCalendar() नवीनतम रूप से सम्मिलित कैलेंडर का आईडी लौटाता है। इस आईडी का उपयोग सर्वर से कैलेंडर लाने के लिए किया जा सकता है। निम्नलिखित कोड स्निपेट दर्शाता है कि कैलेंडर को कैसे सम्मिलित, प्राप्त और अपडेट किया जाए।
// Get access token
GoogleTestUser User2 = new GoogleTestUser("user", "email address", "password", "clientId", "client secret");
string accessToken;
string refreshToken;
GoogleOAuthHelper.GetAccessToken(User2, out accessToken, out refreshToken);
using (IGmailClient client = GmailClient.GetInstance(accessToken, User2.EMail))
{
// Insert, get and update calendar
Aspose.Email.Clients.Google.Calendar calendar = new Aspose.Email.Clients.Google.Calendar("summary - " + Guid.NewGuid().ToString(), null, null, "America/Los_Angeles");
// Insert calendar and Retrieve same calendar using id
string id = client.CreateCalendar(calendar);
Aspose.Email.Clients.Google.Calendar cal = client.FetchCalendar(id);
//Match the retrieved calendar info with local calendar
if ((calendar.Summary == cal.Summary) && (calendar.TimeZone == cal.TimeZone))
{
Console.WriteLine("fetched calendar information matches");
}
else
{
Console.WriteLine("fetched calendar information does not match");
}
// Change information in the fetched calendar and Update calendar
cal.Description = "Description - " + Guid.NewGuid().ToString();
cal.Location = "Location - " + Guid.NewGuid().ToString();
client.UpdateCalendar(cal);
}
विशिष्ट Google कैलेंडर हटाएँ
एक विशिष्ट कैलेंडर को हटाने के लिए, हमें क्लाइंट के सभी कैलेंडर की सूची प्राप्त करनी होगी और फिर आवश्यकतानुसार हटाना होगा। ListCalendars() सूची लौटाता है ExtendedCalendar जिसमें Gmail कैलेंडर शामिल हैं। निम्नलिखित कोड स्निपेट दर्शाता है कि विशिष्ट कैलेंडर को कैसे हटाया जाए।
// Get access token
GoogleTestUser User2 = new GoogleTestUser("user", "email address", "password", "clientId", "client secret");
string accessToken;
string refreshToken;
GoogleOAuthHelper.GetAccessToken(User2, out accessToken, out refreshToken);
using (IGmailClient client = GmailClient.GetInstance(accessToken, User2.EMail))
{
// Access and delete calendar with summary starting from "Calendar summary - "
string summary = "Calendar summary - ";
// Get calendars list
ExtendedCalendar[] lst0 = client.ListCalendars();
foreach (ExtendedCalendar extCal in lst0)
{
// Delete selected calendars
if (extCal.Summary.StartsWith(summary))
client.DeleteCalendar(extCal.Id);
}
}
कैलेंडर एक्सेस नियंत्रण
Aspose.Email कैलेंडर आइटम्स तक पहुंच पर पूर्ण नियंत्रण प्रदान करता है। ListAccessRules() फ़ंक्शन द्वारा उजागर किया गया है IGmailClient जो सूची लौटाता है AccessControlRule. व्यक्तिगत नियम जानकारी को पुनः प्राप्त, संशोधित और क्लाइंट के कैलेंडर के लिए वापस सहेजा जा सकता है। IGmailClient एक्सेस कंट्रोल नियमों को प्रबंधित करने के लिए निम्नलिखित फ़ंक्शन शामिल हैं।
- ListAccessRules यह फ़ंक्शन AccessControlRule की सूची प्रदान करता है
- CreateAccessRule यह फ़ंक्शन कैलेंडर के लिए नया एक्सेस नियम बनाता है।
- UpdateAccessRule यह फ़ंक्शन एक्सेस नियम को अपडेट करने के लिए उपयोग किया जाता है।
- FetchAccessRule इसे क्लाइंट के कैलेंडर के लिए विशिष्ट एक्सेस नियम लाने के लिए इस्तेमाल किया जा सकता है
- DeleteAccessRule यह फ़ंक्शन एक्सेस नियम को हटाने के लिए उपयोग किया जाता है।
निम्नलिखित कोड स्निपेट दिखाता है कि एक्सेस नियम प्रबंधन के फ़ंक्शन कैसे उपयोग करें:
GoogleTestUser User2 = new GoogleTestUser("user", "email address", "password", "clientId", "client secret");
string accessToken;
string refreshToken;
GoogleOAuthHelper.GetAccessToken(User2, out accessToken, out refreshToken);
using (IGmailClient client = GmailClient.GetInstance(accessToken, User2.EMail))
{
// Retrieve list of calendars for the current client
ExtendedCalendar[] calendarList = client.ListCalendars();
// Get first calendar id and retrieve list of AccessControlRule for the first calendar
string calendarId = calendarList[0].Id;
AccessControlRule[] roles1 = client.ListAccessRules(calendarId);
// Create a local access control rule and Set rule properties
AccessControlRule rule = new AccessControlRule();
rule.Role = AccessRole.reader;
rule.Scope = new AclScope(AclScopeType.user, User2.EMail);
// Insert new rule for the calendar. It returns the newly created rule
AccessControlRule createdRule = client.CreateAccessRule(calendarId, rule);
// Confirm if local created rule and returned rule are equal
if ((rule.Role == createdRule.Role) && (rule.Scope.Type == createdRule.Scope.Type) && (rule.Scope.Value.ToLower() == createdRule.Scope.Value.ToLower()))
{
Console.WriteLine("local rule and returned rule after creation are equal");
}
else
{
Console.WriteLine("Rule could not be created successfully");
return;
}
// Get list of rules
AccessControlRule[] roles2 = client.ListAccessRules(calendarId);
// Current list length should be 1 more than the earlier one
if (roles1.Length + 1 == roles2.Length)
{
Console.WriteLine("List lengths are ok");
}
else
{
Console.WriteLine("List lengths are not ok");
return;
}
// Change rule and Update the rule for the selected calendar
createdRule.Role = AccessRole.writer;
AccessControlRule updatedRule = client.UpdateAccessRule(calendarId, createdRule);
// Check if returned access control rule after update is ok
if ((createdRule.Role == updatedRule.Role) && (createdRule.Id == updatedRule.Id))
{
Console.WriteLine("Rule is updated successfully");
}
else
{
Console.WriteLine("Rule is not updated");
return;
}
// Retrieve individaul rule against a calendar
AccessControlRule fetchedRule = client.FetchAccessRule(calendarId, createdRule.Id);
//Check if rule parameters are ok
if ((updatedRule.Id == fetchedRule.Id) && (updatedRule.Role == fetchedRule.Role) && (updatedRule.Scope.Type == fetchedRule.Scope.Type) && (updatedRule.Scope.Value.ToLower() == fetchedRule.Scope.Value.ToLower()))
{
Console.WriteLine("Rule parameters are ok");
}
else
{
Console.WriteLine("Rule parameters are not ok");
}
// Delete particular rule against a given calendar and Retrieve the all rules list for the same calendar
client.DeleteAccessRule(calendarId, createdRule.Id);
AccessControlRule[] roles3 = client.ListAccessRules(calendarId);
// Check that current rules list length should be equal to the original list length before adding and deleting the rule
if (roles1.Length == roles3.Length)
{
Console.WriteLine("List lengths are same");
}
else
{
Console.WriteLine("List lengths are not equal");
return;
}
}
कैलेंडर क्लाइंट सेटिंग्स और रंग जानकारी
Aspose.Email क्लाइंट सेटिंग्स तक पहुँचने का समर्थन करता है, उपयोग करके IGmailClient.GetSettings(). यह नीचे दिए गए अनुसार सेटिंग्स की सूची लौटाता है:
- dateFieldOrder
- displayAllTimezones
- hideInvitations
- format24HourTime
- defaultCalendarMode
- defaultEventLength
- locale
- remindOnRespondedEventsOnly
- alternateCalendar
- userLocation
- hideWeekends
- showDeclinedEvents
- weekStart
- weather
- customCalendarMode
- timezoneLabel
- timezone
- useKeyboardShortcuts
- country
इसी तरह क्लाइंट्स के लिए रंग जानकारी को भी उपयोग करके प्राप्त किया जा सकता है IGmailClient.GetColors(). यह रंग जानकारी ऑब्जेक्ट फोरग्राउंड रंगों, बैकग्राउंड रंगों और अपडेट तिथि व समय की सूची लौटाता है।
क्लाइंट सेटिंग्स तक पहुँच
निम्नलिखित कोड स्निपेट दिखाता है कि फ़ंक्शन को क्लाइंट सेटिंग्स एक्सेस करने के लिए कैसे उपयोग किया जा सकता है:
GoogleTestUser User2 = new GoogleTestUser("user", "email address", "password", "clientId", "client secret");
string accessToken;
string refreshToken;
GoogleOAuthHelper.GetAccessToken(User2, out accessToken, out refreshToken);
using (IGmailClient client = GmailClient.GetInstance(accessToken, User2.EMail))
{
// Retrieve client settings
Dictionary<string, string> settings = client.GetSettings();
if (settings.Count < 1)
{
Console.WriteLine("No settings are available.");
return;
}
// Traverse the settings list
foreach (KeyValuePair<string, string> pair in settings)
{
// Get the setting value and test if settings are ok
string value = client.GetSetting(pair.Key);
if (pair.Value == value)
{
Console.WriteLine("Key = " + pair.Key + ", Value = " + pair.Value);
}
else
{
Console.WriteLine("Settings could not be retrieved");
}
}
}
रंग जानकारी तक पहुँच
निम्नलिखित कोड स्निपेट दिखाता है कि फ़ंक्शन को क्लाइंट रंग सेटिंग्स एक्सेस करने के लिए कैसे उपयोग किया जा सकता है।
GoogleTestUser User2 = new GoogleTestUser("user", "email address", "password", "clientId", "client secret");
string accessToken;
string refreshToken;
GoogleOAuthHelper.GetAccessToken(User2, out accessToken, out refreshToken);
using (IGmailClient client = GmailClient.GetInstance(accessToken, User2.EMail))
{
ColorsInfo colors = client.GetColors();
Dictionary<string, Colors> palettes = colors.Calendar;
// Traverse the settings list
foreach (KeyValuePair<string, Colors> pair in palettes)
{
Console.WriteLine("Key = " + pair.Key + ", Color = " + pair.Value);
}
Console.WriteLine("Update Date = " + colors.Updated);
}
Google कैलेंडर नियुक्तियों को प्रबंधित करें
Aspose.Email Google कैलेंडरों में नियुक्तियों के साथ काम करने के लिए विशेषताएं प्रदान करता है। Google कैलेंडर में नियुक्तियों पर निम्नलिखित कार्य किए जा सकते हैं:
- नियुक्तियाँ जोड़ें।
- नियुक्तियों की सूची प्राप्त करें।
- एक विशिष्ट नियुक्ति प्राप्त करें।
- एक अपॉइंटमेंट को अपडेट करें।
- नियुक्ति को एक कैलेंडर से दूसरे कैलेंडर में स्थानांतरित करें।
- नियुक्ति हटाएँ।
IGmailClient फ़ंक्शन प्रदान करता है जैसे CreateAppointment, FetchAppointment, UpdateAppointment, ListAppointments, MoveAppointment और DeleteAppointment.
Google कैलेंडर में अपॉइंटमेंट जोड़ें
निम्नलिखित कोड उदाहरण कैलेंडर में एक अपॉइंटमेंट जोड़ने की सुविधा को दर्शाता है। इसे प्राप्त करने के लिए, नीचे दिए चरणों का पालन करें:
- एक कैलेंडर बनाएं और डालें।
- एक नए कैलेंडर से अपॉइंटमेंट की सूची प्राप्त करें।
- एक अपॉइंटमेंट बनाएँ।
- एक अपॉइंटमेंट डालें।
GoogleTestUser User2 = new GoogleTestUser("user", "email address", "password", "clientId", "client secret");
string accessToken;
string refreshToken;
GoogleOAuthHelper.GetAccessToken(User2, out accessToken, out refreshToken);
// Get IGmailclient
using (IGmailClient client = GmailClient.GetInstance(accessToken, User2.EMail))
{
// Create local calendar
Aspose.Email.Clients.Google.Calendar calendar1 = new Aspose.Email.Clients.Google.Calendar("summary - " + Guid.NewGuid().ToString(), null, null, "Europe/Kiev");
// Insert calendar and get id of inserted calendar and Get back calendar using an id
string id = client.CreateCalendar(calendar1);
Aspose.Email.Clients.Google.Calendar cal1 = client.FetchCalendar(id);
string calendarId1 = cal1.Id;
try
{
// Retrieve list of appointments from the first calendar
Appointment[] appointments = client.ListAppointments(calendarId1);
if (appointments.Length > 0)
{
Console.WriteLine("Wrong number of appointments");
return;
}
// Get current time and Calculate time after an hour from now
DateTime startDate = DateTime.Now;
DateTime endDate = startDate.AddHours(1);
// Initialize a mail address collection and set attendees mail address
MailAddressCollection attendees = new MailAddressCollection();
attendees.Add("User1.EMail@domain.com");
attendees.Add("User3.EMail@domain.com");
// Create an appointment with above attendees
Appointment app1 = new Appointment("Location - " + Guid.NewGuid().ToString(), startDate, endDate, User2.EMail, attendees);
// Set appointment summary, description, start/end time zone
app1.Summary = "Summary - " + Guid.NewGuid().ToString();
app1.Description = "Description - " + Guid.NewGuid().ToString();
app1.StartTimeZone = "Europe/Kiev";
app1.EndTimeZone = "Europe/Kiev";
// Insert appointment in the first calendar inserted above and get back inserted appointment
Appointment app2 = client.CreateAppointment(calendarId1, app1);
// Retrieve appointment using unique id
Appointment app3 = client.FetchAppointment(calendarId1, app2.UniqueId);
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}
}
Google कैलेंडर नियुक्तियों को प्राप्त करें और अपडेट करें
यहाँ कैलेंडर को प्राप्त करने और अपडेट करने का प्रदर्शन इस प्रकार है:
- विशिष्ट अपॉइंटमेंट प्राप्त करें।
- अपॉइंटमेंट संशोधित करें।
- कैलेंडर में अपॉइंटमेंट अपडेट करें।
मान लिया गया है कि "calendarId" आईडी वाला कैलेंडर और "AppointmentUniqueId" अद्वितीय आईडी वाली नियुक्ति पहले ही निकाली जा चुकी है। निम्नलिखित कोड स्निपेट दिखाता है कि कैसे एक नियुक्ति प्राप्त और अपडेट की जाती है।
GoogleTestUser User2 = new GoogleTestUser("user", "email address", "password", "clientId", "client secret");
string accessToken;
string refreshToken;
GoogleOAuthHelper.GetAccessToken(User2, out accessToken, out refreshToken);
// Get IGmailclient
using (IGmailClient client = GmailClient.GetInstance(accessToken, User2.EMail))
{
string calendarId = client.ListCalendars()[0].Id;
string AppointmentUniqueId = client.ListAppointments(calendarId)[0].UniqueId;
// Retrieve Appointment
Appointment app3 = client.FetchAppointment(calendarId, AppointmentUniqueId);
// Change the appointment information
app3.Summary = "New Summary - " + Guid.NewGuid().ToString();
app3.Description = "New Description - " + Guid.NewGuid().ToString();
app3.Location = "New Location - " + Guid.NewGuid().ToString();
app3.Flags = AppointmentFlags.AllDayEvent;
app3.StartDate = DateTime.Now.AddHours(2);
app3.EndDate = app3.StartDate.AddHours(1);
app3.StartTimeZone = "Europe/Kiev";
app3.EndTimeZone = "Europe/Kiev";
// Update the appointment and get back updated appointment
Appointment app4 = client.UpdateAppointment(calendarId, app3);
}
Google कैलेंडर में नियुक्तियों को स्थानांतरित करें और हटाएँ
स्रोत कैलेंडर, गंतव्य कैलेंडर और स्रोत कैलेंडर में नियुक्ति का अद्वितीय आईडी प्रदान करके नियुक्ति को स्थानांतरित किया जा सकता है। निम्नलिखित कोड स्निपेट दिखाता है कि कैसे एक नियुक्ति को स्थानांतरित और हटाया जाता है।
GoogleTestUser User2 = new GoogleTestUser("user", "email address", "password", "clientId", "client secret");
string accessToken;
string refreshToken;
GoogleOAuthHelper.GetAccessToken(User2, out accessToken, out refreshToken);
// Get IGmailclient
using (IGmailClient client = Aspose.Email.Clients.Google.GmailClient.GetInstance(accessToken, User2.EMail))
{
string SourceCalendarId = client.ListCalendars()[0].Id;
string DestinationCalendarId = client.ListCalendars()[1].Id;
string TargetAppUniqueId = client.ListAppointments(SourceCalendarId)[0].UniqueId;
// Retrieve the list of appointments in the destination calendar before moving the appointment
Appointment[] appointments = client.ListAppointments(DestinationCalendarId);
Console.WriteLine("Before moving count = " + appointments.Length);
Appointment Movedapp = client.MoveAppointment(SourceCalendarId, DestinationCalendarId, TargetAppUniqueId);
// Retrieve the list of appointments in the destination calendar after moving the appointment
appointments = client.ListAppointments(DestinationCalendarId);
Console.WriteLine("After moving count = " + appointments.Length);
// Delete particular appointment from a calendar using unique id
client.DeleteAppointment(DestinationCalendarId, Movedapp.UniqueId);
// Retrieve the list of appointments. It should be one less than the earlier appointments in the destination calendar
appointments = client.ListAppointments(DestinationCalendarId);
Console.WriteLine("After deleting count = " + appointments.Length);
}
Google कैलेंडर के लिए FreeBusy क्वेरी
Aspose.Email ऐसी क्वेरी तंत्र प्रदान करता है जिससे यह जांचा जा सके कि किसी नियुक्ति की नियत तिथि निकट है या नहीं, मानदंड के अनुसार। इसके लिए FreebusyQuery क्लास उपलब्ध कराई गई है, जो किसी विशेष कैलेंडर के लिए क्वेरी तैयार करने की अनुमति देती है।
यह कोड उदाहरण कैलेंडर को क्वेरी करने की सुविधा दर्शाता है। इस उदाहरण में निम्नलिखित कार्य किए जाते हैं:
- कैलेंडर बनाएं और सम्मिलित करें
- एक अपॉइंटमेंट बनाएं
- अपॉइंटमेंट सम्मिलित करें
- FreeBusyQuery तैयार करें
- FreebusyResponse प्राप्त करें
// Use the GoogleUser and GoogleOAuthHelper classes below to receive an access token
using (IGmailClient client = GmailClient.GetInstance(accessToken, user.Email))
{
// Initialize calendar item
Aspose.Email.Clients.Google.Calendar calendar1 = new Aspose.Email.Clients.Google.Calendar("summary - " + Guid.NewGuid().ToString(), null, null, "Europe/Kiev");
// Insert calendar and get back id of newly inserted calendar and Fetch the same calendar using calendar id
string id = client.CreateCalendar(calendar1);
Aspose.Email.Clients.Google.Calendar cal1 = client.FetchCalendar(id);
string calendarId1 = cal1.Id;
try
{
// Get list of appointments in newly inserted calendar. It should be zero
Appointment[] appointments = client.ListAppointments(calendarId1);
if (appointments.Length != 0)
{
Console.WriteLine("Wrong number of appointments");
return;
}
// Create a new appointment and Calculate appointment start and finish time
DateTime startDate = DateTime.Now;
DateTime endDate = startDate.AddHours(1);
// Create attendees list for appointment
MailAddressCollection attendees = new MailAddressCollection();
attendees.Add("user1@domain.com");
attendees.Add("user2@domain.com");
// Create appointment
Appointment app1 = new Appointment("Location - " + Guid.NewGuid().ToString(), startDate, endDate, "user2@domain.com", attendees);
app1.Summary = "Summary - " + Guid.NewGuid().ToString();
app1.Description = "Description - " + Guid.NewGuid().ToString();
app1.StartTimeZone = "Europe/Kiev";
app1.EndTimeZone = "Europe/Kiev";
// Insert the newly created appointment and get back the same in case of successful insertion
Appointment app2 = client.CreateAppointment(calendarId1, app1);
// Create Freebusy query by setting min/max timeand time zone
FreebusyQuery query = new FreebusyQuery();
query.TimeMin = DateTime.Now.AddDays(-1);
query.TimeMax = DateTime.Now.AddDays(1);
query.TimeZone = "Europe/Kiev";
// Set calendar item to search and Get the reponse of query containing
query.Items.Add(cal1.Id);
FreebusyResponse resp = client.GetFreebusyInfo(query);
// Delete the appointment
client.DeleteAppointment(calendarId1, app2.UniqueId);
}
finally
{
// Delete the calendar
client.DeleteCalendar(cal1.Id);
}
}