Sample code for 30+ languages & platforms
Java Requires Chilkat v11.0.0+

MS Graph Create Calendar

See more Microsoft Calendar Examples

Creates a new calendar.

For more details, see https://docs.microsoft.com/en-us/graph/api/user-post-calendars?view=graph-rest-1.0

Chilkat Java Downloads

Chilkat2 Java
import com.chilkatsoft.chilkat2.ChilkatException;
import com.chilkatsoft.chilkat2.Http;
import com.chilkatsoft.chilkat2.HttpResponse;
import com.chilkatsoft.chilkat2.JsonObject;

public class ChilkatExample {

    public static void main(String[] args) {
        // This example requires the Chilkat API to have been previously unlocked.
        // See Global Unlock Sample for sample code.

        Http http = new Http();

        // Use your previously obtained access token as shown here:
        //    Get Microsoft Graph OAuth2 Access Token with Calendars.ReadWrite scope.

        JsonObject jsonToken = new JsonObject();
        try {
            jsonToken.loadFile("qa_data/tokens/msGraphCalendar.json");
        } catch (ChilkatException e) {
            System.out.println(e.getLastErrorText());
            return;
        }

        http.setAuthToken(jsonToken.stringOf("access_token"));

        // Create a JSON body for the HTTP POST
        // {
        //   "name": "Work"
        // }
        JsonObject json = new JsonObject();
        json.updateString("name", "Work");

        // POST the JSON to https://graph.microsoft.com/v1.0/me/calendars
        HttpResponse resp = new HttpResponse();
        try {
            http.httpJson("POST", "https://graph.microsoft.com/v1.0/me/calendars", json, "application/json", resp);
        } catch (ChilkatException e) {
            System.out.println(e.getLastErrorText());
            return;
        }

        json.load(resp.getBodyStr());
        json.setEmitCompact(false);

        if (resp.getStatusCode() != 201) {
            System.out.println(json.emit());
            System.out.println("Failed, response status code = " + resp.getStatusCode());
            return;
        }

        System.out.println(json.emit());

        // A sample response:
        // {
        //   "@odata.context": "https://graph.microsoft.com/v1.0/$metadata#users('admin%40chilkat.io')/calendars/$entity",
        //   "id": "AQMkAD...TgAAAA==",
        //   "name": "Work",
        //   "color": "auto",
        //   "changeKey": "5+vF7T...HjDcA==",
        //   "canShare": true,
        //   "canViewPrivateItems": true,
        //   "canEdit": true,
        //   "owner": {
        //     "name": "...",
        //     "address": "outlook_3A33...4CC15@outlook.com"
        //   }
        // }

        // Use this online tool to generate parsing code from sample JSON:
        // Generate Parsing Code from JSON

        String odataContext = json.stringOf("\"@odata.context\"");
        String id = json.stringOf("id");
        String name = json.stringOf("name");
        String color = json.stringOf("color");
        String changeKey = json.stringOf("changeKey");
        String canShare = json.boolOf("canShare");
        String canViewPrivateItems = json.boolOf("canViewPrivateItems");
        String canEdit = json.boolOf("canEdit");
        String ownerName = json.stringOf("owner.name");
        String ownerAddress = json.stringOf("owner.address");

        System.out.println("Success.");
    }
}