> ## Documentation Index
> Fetch the complete documentation index at: https://help.dingtalk.io/llms.txt
> Use this file to discover all available pages before exploring further.

# 予定の作成、変更、照会、削除

> 社内アプリで予定 API を利用して、予定の作成・変更・照査・削除を実現します

本ドキュメントでは、予定 API を呼び出して予定を作成するなどのフローを説明します。まず社内アプリを作成し、次に予定が提供する API を使用して、予定の作成、変更、単一予定の詳細照会、削除のフローを実現します。

## 期待される効果

予定を作成後、効果は以下のとおりです。

## 統合フローの概要

本ドキュメントでは、社内アプリを作成し、予定が提供する API を使用して、予定の作成、変更、単一予定の詳細照会、削除のフローを実現する方法を示します。

前提条件：[アプリの作成と設定](/ja/open/dingstart/create-application)のフローを完了してください。

ステップ 1：アプリ認証情報を取得し、クライアント ID と Client Secret を取得します。

ステップ 2：API 権限をリクエストし、予定関連 API の権限を申請します。

ステップ 3：アプリのアクセス認証情報を取得します。[社内アプリの access\_token を取得する](/ja/open/development/obtain-the-access-token-of-an-internal-app)。API 呼び出し時、access\_token で呼び出し元の身元を認証します。

ステップ 4：サーバーサイドの予定関連 API を呼び出します。

1. サーバー API の [予定を作成](/ja/open/development/create-schedule) を呼び出し、予定を作成します。予定の `id` を取得します。
2. 予定の `id` に基づき、サーバー API の [予定を変更](/ja/open/development/modify-event) を呼び出し、予定情報を変更します。
3. 予定の `id` に基づき、サーバー API の [単一予定の詳細を照会](/ja/open/development/query-details-about-an-event) を呼び出し、予定の詳細情報を取得します。
4. 予定の `id` に基づき、サーバー API の [予定を削除](/ja/open/development/delete-event) を呼び出し、作成した予定を削除します。

## 前提条件

[アプリの作成と設定](/ja/open/dingstart/create-application)のフローを完了してください。

## ステップ 1：アプリ認証情報の取得

1. 対象アプリを選択し、アプリ詳細ページに移動して、**ベーシック情報** > **認証情報とベーシック情報** をクリックします。
2. クライアント ID と Client Secret を取得します。

## ステップ 2：API 権限の追加

**開発設定** > **権限管理** をクリックし、権限検索ボックスに `Calendar.Event.Write` と `Calendar.Event.Read` をそれぞれ入力し、権限を申請します。

## ステップ 3：アプリのアクセス認証情報 access\_token の取得

<Warning>
  サーバー API の差異の詳細は、旧版 API VS 新版 API を参照してください。以下の API はすべてサーバー API を使用しています。SDK ダウンロードの詳細は、サーバーサイド SDK ダウンロードを参照してください。
</Warning>

ステップ 1 で取得した Client ID と Client Secret に基づき、アプリのアクセス認証情報として[社内アプリの access\_token を取得](/ja/open/development/obtain-orgapp-token)します。

```java theme={"theme":{"light":"github-light","dark":"github-dark"}}
 public void getAccessToken() throws Exception {
        Config config = new Config();
        config.protocol = "https";
        config.regionId = "central";
        com.aliyun.dingtalkoauth2_1_0.Client client = new com.aliyun.dingtalkoauth2_1_0.Client(config);
        GetAccessTokenRequest accessTokenRequest = new GetAccessTokenRequest()
                .setAppKey("ding*********hgn")
                .setAppSecret("9G_O**********xamBkhgGIO");
        GetAccessTokenResponse accessToken = client.getAccessToken(accessTokenRequest);
        System.out.println(JSON.toJSONString(accessToken.getBody()));
    }
```

## ステップ 4：サーバーサイドの予定関連 API の呼び出し

1. サーバー API の [予定を作成](/ja/open/development/create-schedule) を呼び出し、予定を作成します。予定の `id` を取得します。

   ```java lines theme={"theme":{"light":"github-light","dark":"github-dark"}}
    public void createCalendar() throws Exception {
           Config config = new Config();
           config.protocol = "https";
           config.regionId = "central";
           com.aliyun.dingtalkcalendar_1_0.Client client = new com.aliyun.dingtalkcalendar_1_0.Client(config);
           CreateEventHeaders createEventHeaders = new CreateEventHeaders();
           createEventHeaders.xAcsDingtalkAccessToken = "accessToken";
           java.util.Map<String, String> extra = TeaConverter.buildMap(
                   new TeaPair("noChatNotification", "false"),
                   new TeaPair("noPushNotification","false")
           );
           CreateEventRequest.CreateEventRequestOnlineMeetingInfo onlineMeetingInfo = new CreateEventRequest.CreateEventRequestOnlineMeetingInfo()
                   .setType("dingtalk");
           CreateEventRequest.CreateEventRequestReminders reminders0 = new CreateEventRequest.CreateEventRequestReminders()
                   .setMethod("dingtalk")
                   .setMinutes(15);
           CreateEventRequest.CreateEventRequestLocation location = new CreateEventRequest.CreateEventRequestLocation()
                   .setDisplayName("********A座9F");
           CreateEventRequest.CreateEventRequestAttendees attendees0 = new CreateEventRequest.CreateEventRequestAttendees()
                   .setId("予定参加者unionId")
                   .setIsOptional(false);
           CreateEventRequest.CreateEventRequestAttendees attendees1 = new CreateEventRequest.CreateEventRequestAttendees()
                   .setId("予定参加者unionId")
                   .setIsOptional(false);
           CreateEventRequest.CreateEventRequestAttendees attendees2 = new CreateEventRequest.CreateEventRequestAttendees()
                   .setId("予定参加者unionId")
                   .setIsOptional(false);
           CreateEventRequest.CreateEventRequestRecurrenceRange recurrenceRange = new CreateEventRequest.CreateEventRequestRecurrenceRange()
                   .setType("numbered")
                   .setNumberOfOccurrences(2);
           CreateEventRequest.CreateEventRequestRecurrencePattern recurrencePattern = new CreateEventRequest.CreateEventRequestRecurrencePattern()
                   .setType("daily")
                   .setInterval(1);
           CreateEventRequest.CreateEventRequestRecurrence recurrence = new CreateEventRequest.CreateEventRequestRecurrence()
                   .setPattern(recurrencePattern)
                   .setRange(recurrenceRange);
           CreateEventRequest.CreateEventRequestEnd end = new CreateEventRequest.CreateEventRequestEnd()
                   .setDate("2022-09-03");
   //                .setDateTime("2021-09-20T10:15:30+08:00")
   //                .setTimeZone("Asia/Shanghai");
           CreateEventRequest.CreateEventRequestStart start = new CreateEventRequest.CreateEventRequestStart()
                   .setDate("2022-09-02");
   //                .setDateTime("2021-09-20T10:15:30+08:00")
   //                .setTimeZone("Asia/Shanghai");
           CreateEventRequest createEventRequest = new CreateEventRequest()
                   .setSummary("0902予定を作成")
                   .setDescription("これは0902に作成された予定です")
                   .setStart(start)
                   .setEnd(end)
                   .setIsAllDay(true)
                   .setRecurrence(recurrence)
                   .setAttendees(java.util.Arrays.asList(
                           attendees0,
                           attendees1,
                           attendees2
                   ))
                   .setLocation(location)
                   .setReminders(java.util.Arrays.asList(
                           reminders0
                   ))
                   .setOnlineMeetingInfo(onlineMeetingInfo)
                   .setExtra(extra);
           try {
               CreateEventResponse eventWithOptions = client.createEventWithOptions("予定組織者unionId", "primary", createEventRequest, createEventHeaders, new RuntimeOptions());
               System.out.println(JSON.toJSONString(eventWithOptions.getBody()));
           } catch (TeaException err) {
               if (!com.aliyun.teautil.Common.empty(err.code) && !com.aliyun.teautil.Common.empty(err.message)) {
                   // err には code と message 属性が含まれ、開発者の問題特定に役立ちます
                   System.out.println(err.code);
                   System.out.println(err.message);
               }
           } catch (Exception _err) {
               TeaException err = new TeaException(_err.getMessage(), _err);
               if (!com.aliyun.teautil.Common.empty(err.code) && !com.aliyun.teautil.Common.empty(err.message)) {
                   // err には code と message 属性が含まれ、開発者の問題特定に役立ちます
                   System.out.println(err.code);
                   System.out.println(err.message);
               }
           }
       }
   ```
2. 予定の `id` に基づき、サーバー API の [予定を変更](/ja/open/development/modify-event) を呼び出し、予定情報を変更します。

   ```java lines theme={"theme":{"light":"github-light","dark":"github-dark"}}
   public void  updateCalendar() throws Exception {
           Config config = new Config();
           config.protocol = "https";
           config.regionId = "central";
           com.aliyun.dingtalkcalendar_1_0.Client client = new com.aliyun.dingtalkcalendar_1_0.Client(config);
           PatchEventHeaders patchEventHeaders = new PatchEventHeaders();
           patchEventHeaders.xAcsDingtalkAccessToken = "accessToken";
           PatchEventRequest.PatchEventRequestReminders reminders0 = new PatchEventRequest.PatchEventRequestReminders()
                   .setMethod("dingtalk")
                   .setMinutes(15);
           java.util.Map<String, String> extra = TeaConverter.buildMap(
                   new TeaPair("noChatNotification", "false"),
                   new TeaPair("noPushNotification","false")
           );
           PatchEventRequest.PatchEventRequestLocation location = new PatchEventRequest.PatchEventRequestLocation()
                   .setDisplayName("room 1-2-3");
           PatchEventRequest.PatchEventRequestAttendees attendees0 = new PatchEventRequest.PatchEventRequestAttendees()
                   .setId("予定参加者unionId")
                   .setIsOptional(false);
           PatchEventRequest.PatchEventRequestRecurrenceRange recurrenceRange = new PatchEventRequest.PatchEventRequestRecurrenceRange()
                   .setType("numbered")
                   .setNumberOfOccurrences(1);
           PatchEventRequest.PatchEventRequestRecurrencePattern recurrencePattern = new PatchEventRequest.PatchEventRequestRecurrencePattern()
                   .setType("daily")
                   .setInterval(1);
           PatchEventRequest.PatchEventRequestRecurrence recurrence = new PatchEventRequest.PatchEventRequestRecurrence()
                   .setPattern(recurrencePattern)
                   .setRange(recurrenceRange);
           PatchEventRequest.PatchEventRequestEnd end = new PatchEventRequest.PatchEventRequestEnd()
                   .setDate("2022-09-02");
           PatchEventRequest.PatchEventRequestStart start = new PatchEventRequest.PatchEventRequestStart()
                   .setDate("2022-09-02");
           PatchEventRequest patchEventRequest = new PatchEventRequest()
                   .setSummary("0902予定内容を変更")
                   .setId("予定id")
                   .setDescription("これは0902に変更された予定です")
                   .setStart(start)
                   .setEnd(end)
                   .setIsAllDay(true)
                   .setRecurrence(recurrence)
                   .setAttendees(java.util.Arrays.asList(
                           attendees0
                   ))
                   .setLocation(location)
                   .setExtra(extra)
                   .setReminders(java.util.Arrays.asList(
                           reminders0
                   ));
           try {
               PatchEventResponse patchEventResponse = client.patchEventWithOptions("予定組織者unionId", "primary", "予定id", patchEventRequest, patchEventHeaders, new RuntimeOptions());
               System.out.println(JSON.toJSONString(patchEventResponse.getBody()));
           } catch (TeaException err) {
               if (!com.aliyun.teautil.Common.empty(err.code) && !com.aliyun.teautil.Common.empty(err.message)) {
                   // err には code と message 属性が含まれ、開発者の問題特定に役立ちます
                   System.out.println(err.code);
                   System.out.println(err.message);
               }
           } catch (Exception _err) {
               TeaException err = new TeaException(_err.getMessage(), _err);
               if (!com.aliyun.teautil.Common.empty(err.code) && !com.aliyun.teautil.Common.empty(err.message)) {
                   // err には code と message 属性が含まれ、開発者の問題特定に役立ちます
                   System.out.println(err.code);
                   System.out.println(err.message);
               }
           }
       }
   ```
3. 予定の `id` に基づき、サーバー API の [単一予定の詳細を照会](/ja/open/development/query-details-about-an-event) を呼び出し、予定の詳細情報を取得します。

   ```java lines theme={"theme":{"light":"github-light","dark":"github-dark"}}
    public void calendarInfo() throws Exception {
           Config config = new Config();
           config.protocol = "https";
           config.regionId = "central";
           com.aliyun.dingtalkcalendar_1_0.Client client = new com.aliyun.dingtalkcalendar_1_0.Client(config);
           GetEventHeaders getEventHeaders = new GetEventHeaders();
           getEventHeaders.xAcsDingtalkAccessToken = "accessToken";
           GetEventRequest getEventRequest = new GetEventRequest()
                   .setMaxAttendees(100L);
           try {
               GetEventResponse eventWithOptions = client.getEventWithOptions("予定組織者unionId", "primary", "予定id", getEventRequest, getEventHeaders, new RuntimeOptions());
               System.out.println(JSON.toJSONString(eventWithOptions.getBody()));
           } catch (TeaException err) {
               if (!com.aliyun.teautil.Common.empty(err.code) && !com.aliyun.teautil.Common.empty(err.message)) {
                   // err には code と message 属性が含まれ、開発者の問題特定に役立ちます
                   System.out.println(err.code);
                   System.out.println(err.message);
               }
           } catch (Exception _err) {
               TeaException err = new TeaException(_err.getMessage(), _err);
               if (!com.aliyun.teautil.Common.empty(err.code) && !com.aliyun.teautil.Common.empty(err.message)) {
                   // err には code と message 属性が含まれ、開発者の問題特定に役立ちます
                   System.out.println(err.code);
                   System.out.println(err.message);
               }
           }
       }
   ```
4. 予定の `id` に基づき、サーバー API の [予定を削除](/ja/open/development/delete-event) を呼び出し、作成した予定を削除します。

   ```java lines theme={"theme":{"light":"github-light","dark":"github-dark"}}
   public void  deleteCalendar() throws Exception {
           Config config = new Config();
           config.protocol = "https";
           config.regionId = "central";
           com.aliyun.dingtalkcalendar_1_0.Client client = new com.aliyun.dingtalkcalendar_1_0.Client(config);
           DeleteEventHeaders deleteEventHeaders = new DeleteEventHeaders();
           deleteEventHeaders.xAcsDingtalkAccessToken = "accessToken";
           try {
               client.deleteEventWithOptions("予定組織者unionId", "primary", "予定id", deleteEventHeaders, new RuntimeOptions());
           } catch (TeaException err) {
               if (!com.aliyun.teautil.Common.empty(err.code) && !com.aliyun.teautil.Common.empty(err.message)) {
                   // err には code と message 属性が含まれ、開発者の問題特定に役立ちます
                   System.out.println(err.code);
                   System.out.println(err.message);
               }
           } catch (Exception _err) {
               TeaException err = new TeaException(_err.getMessage(), _err);
               if (!com.aliyun.teautil.Common.empty(err.code) && !com.aliyun.teautil.Common.empty(err.message)) {
                   // err には code と message 属性が含まれ、開発者の問題特定に役立ちます
                   System.out.println(err.code);
                   System.out.println(err.message);
               }
           }
       }
   ```

<Note>
  以下 2 種類の削除シナリオをサポートします。
</Note>

* 主催者が予定を削除する場合（Path パラメータの userId に主催者の unionId を使用）、すべての参加者に予定キャンセル通知が送信され、同時に予定はすべての参加者のカレンダーから削除されます。
* 参加者が予定を削除する場合（Path パラメータの userId に参加者の unionId を使用）、予定は自身のカレンダーからのみ削除され、その他の予定参加者には影響しません。
