Sample code for 30+ languages & platforms
Lua 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 Lua Downloads

Lua
local chilkat = require("chilkat")

-- This example requires the Chilkat API to have been previously unlocked.
-- See Global Unlock Sample for sample code.

local http = chilkat.Http.new()

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

local jsonToken = chilkat.JsonObject.new()
local success = jsonToken:load_file("qa_data/tokens/msGraphCalendar.json")
if not success then
    print(jsonToken.last_error_text)
    return
end

http.auth_token = jsonToken:string_of("access_token")

-- Create a JSON body for the HTTP POST
-- {
--   "name": "Work"
-- }
local json = chilkat.JsonObject.new()
json:update_string("name", "Work")

-- POST the JSON to https://graph.microsoft.com/v1.0/me/calendars
local resp = chilkat.HttpResponse.new()
success = http:http_json("POST", "https://graph.microsoft.com/v1.0/me/calendars", json, "application/json", resp)
if not success then
    print(http.last_error_text)
    return
end

json:load(resp.body_str)
json.emit_compact = false

if resp.status_code ~= 201 then
    print(json:emit())
    print("Failed, response status code = " .. resp.status_code)
    return
end

print(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

local odataContext = json:string_of("\"@odata.context\"")
local id = json:string_of("id")
local name = json:string_of("name")
local color = json:string_of("color")
local changeKey = json:string_of("changeKey")
local canShare = json:bool_of("canShare")
local canViewPrivateItems = json:bool_of("canViewPrivateItems")
local canEdit = json:bool_of("canEdit")
local ownerName = json:string_of("owner.name")
local ownerAddress = json:string_of("owner.address")

print("Success.")