การทำงานกับเหตุการณ์โดยใช้ Google Calendar Service Client ใน Java
CalendarClient จาก com.aspose.email.services.google.calendar.client แพ็กเกจทำงานกับปฏิทินและเหตุการณ์ผ่าน Google Calendar API โดยเชื่อมต่อผ่าน HTTP เพื่อเรียนรู้วิธีเลือกไลบรารี HTTP ดู HTTP Clients สำหรับบริการอีเมล.
เชื่อมต่อกับ Google Calendar
คลิเอนต์ทำการตรวจสอบสิทธิ์ด้วย OAuth 2.0 เช่นเดียวกับ GmailClient. รายละเอียดเพิ่มเติมดู เชื่อมต่อกับ Gmail.
โค้ดตัวอย่างต่อไปนี้สร้าง client และแสดงรายการปฏิทินของผู้ใช้ ตัวอย่างโค้ดอื่นในหน้านี้ใช้ calendar ตัวแปรที่สร้างที่นี่
import com.aspose.email.services.google.base.auth.GoogleOAuthTokenProvider;
import com.aspose.email.services.google.calendar.client.CalendarClient;
import com.aspose.email.services.google.calendar.model.GoogleCalendarListEntry;
GoogleOAuthTokenProvider tokens = new GoogleOAuthTokenProvider("client-id", "client-secret", "refresh-token");
try (CalendarClient calendar = CalendarClient.builder().tokenProvider(tokens).build()) {
for (GoogleCalendarListEntry entry : calendar.calendarList().list()) {
System.out.println(entry.getId() + ": " + entry.getSummary() + ", primary: " + entry.isPrimary());
}
}
ในเมธอดอื่น ให้ใช้รหัสของปฏิทินจากรายการนี้ หรือ primary สำหรับปฏิทินหลักของผู้ใช้
การแสดงรายการเหตุการณ์
เพื่อแสดงรายการเหตุการณ์ในช่วงเวลา ให้ส่งค่าเริ่มต้นและสิ้นสุดในรูปแบบ RFC 3339 ไปที่ list. ลูกค้าจะคืนค่าการเกิดซ้ำของเหตุการณ์แต่ละรายการเป็นเหตุการณ์แยกกัน โดยเรียงตามเวลาเริ่มต้น
import com.aspose.email.services.google.calendar.model.GoogleEvent;
for (GoogleEvent event : calendar.events("primary").list("2030-01-01T00:00:00Z", "2030-02-01T00:00:00Z")) {
System.out.println(event.getSummary() + ": " + event.getStart().getDateTime());
}
นี้ list เมธอดนี้คืนค่าหนึ่งหน้าผลลัพธ์ หากต้องการอ่านทั้งหมดให้ใช้ listPaged:
import com.aspose.email.services.google.base.api.GooglePagedResult;
import com.aspose.email.services.google.calendar.model.GoogleEvent;
String pageToken = null;
do {
GooglePagedResult<GoogleEvent> page = calendar.events("primary")
.listPaged("2030-01-01T00:00:00Z", "2030-02-01T00:00:00Z", 100, pageToken);
for (GoogleEvent event : page.getItems()) {
System.out.println(event.getSummary());
}
pageToken = page.getNextPageToken();
} while (pageToken != null);
หากต้องการค้นหาเหตุการณ์ตามข้อความ ให้ใช้ search:
import com.aspose.email.services.google.calendar.model.GoogleEvent;
for (GoogleEvent event : calendar.events("primary").search("report")) {
System.out.println(event.getSummary());
}
การสร้างเหตุการณ์
สร้าง GoogleEvent อ็อบเจ็กต์ ตั้งค่าคุณสมบัติ แล้วส่งให้ create. วันที่เริ่มต้นและสิ้นสุดของเหตุการณ์คือ GoogleEventDateTime อ็อบเจ็กต์ที่มีวันที่และเวลาในรูปแบบ RFC 3339 พร้อมเขตเวลา:
import com.aspose.email.services.google.calendar.model.GoogleAttendee;
import com.aspose.email.services.google.calendar.model.GoogleEvent;
import com.aspose.email.services.google.calendar.model.GoogleEventDateTime;
import java.util.Arrays;
GoogleEvent event = new GoogleEvent();
event.setSummary("Weekly meeting");
event.setDescription("Review of the weekly report");
event.setLocation("Room 101");
event.setStart(new GoogleEventDateTime("2030-01-10T10:00:00+02:00", "Europe/Kyiv"));
event.setEnd(new GoogleEventDateTime("2030-01-10T11:00:00+02:00", "Europe/Kyiv"));
event.setAttendees(Arrays.asList(new GoogleAttendee("user@example.com")));
GoogleEvent created = calendar.events("primary").create(event);
System.out.println(created.getId() + ": " + created.getHtmlLink());
เพื่อสร้างเหตุการณ์ตลอดวัน ให้ใช้ GoogleEventDateTime.allDay. วันที่สิ้นสุดของเหตุการณ์แบบนี้เป็นวันที่ไม่รวม:
import com.aspose.email.services.google.calendar.model.GoogleEvent;
import com.aspose.email.services.google.calendar.model.GoogleEventDateTime;
GoogleEvent event = new GoogleEvent();
event.setSummary("Company holiday");
event.setStart(GoogleEventDateTime.allDay("2030-01-10"));
event.setEnd(GoogleEventDateTime.allDay("2030-01-11"));
calendar.events("primary").create(event);
การทำงานกับเหตุการณ์ที่เกิดซ้ำ
เพื่อสร้างเหตุการณ์ที่เกิดซ้ำ ให้ตั้งกฎการเกิดซ้ำ หากต้องการดึงรายการเหตุการณ์ในช่วงเวลาหนึ่ง ให้เรียก instances:
import com.aspose.email.services.google.calendar.model.GoogleEvent;
import com.aspose.email.services.google.calendar.model.GoogleEventDateTime;
import java.util.Arrays;
GoogleEvent event = new GoogleEvent();
event.setSummary("Weekly meeting");
event.setStart(new GoogleEventDateTime("2030-01-10T10:00:00+02:00", "Europe/Kyiv"));
event.setEnd(new GoogleEventDateTime("2030-01-10T11:00:00+02:00", "Europe/Kyiv"));
event.setRecurrence(Arrays.asList("RRULE:FREQ=WEEKLY;COUNT=10"));
GoogleEvent series = calendar.events("primary").create(event);
for (GoogleEvent occurrence : calendar.events("primary", series.getId())
.instances("2030-01-01T00:00:00Z", "2030-03-01T00:00:00Z")) {
System.out.println(occurrence.getStart().getDateTime());
}
หากต้องการแสดงรายการเหตุการณ์ที่เกิดซ้ำเองแทนที่จะเป็นแต่ละครั้งให้ใช้ listMasters.
การอัปเดตและการลบเหตุการณ์
นี้ update เมธอดนี้แทนที่เหตุการณ์ด้วยอ็อบเจ็กต์ที่คุณส่ง ดังนั้นให้ดึงเหตุการณ์ก่อนหากต้องการเก็บคุณสมบัติอื่น ๆ ของมัน patch เมธอดนี้เปลี่ยนเฉพาะคุณสมบัติที่คุณตั้งค่าเท่านั้น delete เมธอดนี้ลบเหตุการณ์
import com.aspose.email.services.google.calendar.model.GoogleEvent;
GoogleEvent event = calendar.events("primary", "event-id").get();
event.setSummary("Updated meeting");
calendar.events("primary", "event-id").update(event);
GoogleEvent changes = new GoogleEvent();
changes.setLocation("Room 202");
calendar.events("primary", "event-id").patch(changes);
calendar.events("primary", "event-id").delete();
การจัดการข้อผิดพลาด
เมื่อ Google Calendar API ส่งสถานะข้อผิดพลาด ลูกค้าจะโยน HttpClientException, ซึ่งประกอบด้วยสถานะและ response body หาก token provider ไม่สามารถรับ access token ลูกค้าจะโยน java.io.IOException, ซึ่งเป็นคลาสฐานของ HttpClientException.
import com.aspose.ext.http.HttpClientException;
try {
calendar.events("primary", "unknown-id").get();
} catch (HttpClientException e) {
System.out.println(e.getStatusCode() + ": " + e.getResponseBody());
}