Outlook Calendar: Accept, Tentatively Accept, or Decline a Meeting
See more Outlook Calendar Examples
Choose one action for an invitation in the signed-in attendee's calendar. The example first checks that the user is not the organizer, then sends one action request. Set sendResponse explicitly: true sends a response to the organizer; false suppresses that response message while applying the calendar action. Running this changes the attendee's response, and decline can remove the invitation from the calendar.
HTTP 202 has no JSON body. Do not implement a response by editing responseStatus or deleting the organizer's event. An occurrence ID addresses one occurrence; a series-master ID addresses the series. A response timeout is not proof that no action occurred: inspect the invitation before retrying.
Delegated permission: Calendars.ReadWrite.
Uses Microsoft Graph v1.0 in the global cloud with a delegated access token saved by the calendar OAuth2 example. Both personal Outlook.com accounts and Microsoft 365 work/school accounts with a mailbox can use this flow. The app registration must support the selected account type. A token for SMTP/IMAP/POP or another API is not a Graph token.
/me means the signed-in user and requires delegated authentication. An unattended Microsoft 365 service instead needs application permissions and administrator consent, and addresses an explicit mailbox through /users/{id-or-userPrincipalName}. That client-credentials flow does not apply to personal Outlook.com accounts. These samples target the global cloud; national clouds use different authority and Graph hosts.
HTTP methods can return false for an HTTP error as well as a connection failure. The code distinguishes those cases and checks the expected status before reading JSON. For 401, refresh the access token or authorize again; for 403, check permissions, mailbox access, and tenant policy. For 429, honor Retry-After before a bounded retry. Do not automatically repeat a write after an uncertain outcome. Keep detailed errors and calendar data private.
Chilkat DataFlex Downloads
Use ChilkatAx-win32.pkg
Procedure Test
Boolean iSuccess
Handle hoToken
Handle hoHttp
Variant vResp
Handle hoResp
Handle hoResult
String sEventId
String sAction
Boolean iSendResponse
Handle hoEndpoint
Variant vBody
Handle hoBody
String sTemp1
Integer iTemp1
Boolean bTemp1
Move False To iSuccess
// Assumes Chilkat is unlocked. See Global Unlock Sample.
Move False To iSuccess
// Obtain a delegated Microsoft Graph token using the calendar authorization example.
// See Authorize Outlook Calendar with OAuth2 and PKCE.
Get Create (RefClass(cComChilkatJsonObject)) To hoToken
If (Not(IsComObjectCreated(hoToken))) Begin
Send CreateComObject of hoToken
End
Set ComStrict Of hoToken To True
Get ComLoadFile Of hoToken "qa_data/tokens/outlookCalendar.json" To iSuccess
If (iSuccess <> True) Begin
Showln "Could not read the private calendar token cache."
Procedure_Return
End
Get ComJsonTypeOf Of hoToken "access_token" To iTemp1
Get ComStringOfEquals Of hoToken "access_token" "" True To bTemp1
If ((iTemp1 <> 1) Or (bTemp1 = True)) Begin
Showln "Expected a nonempty access_token in the JSON."
Procedure_Return
End
Get Create (RefClass(cComChilkatHttp)) To hoHttp
If (Not(IsComObjectCreated(hoHttp))) Begin
Send CreateComObject of hoHttp
End
Get ComStringOf Of hoToken "access_token" To sTemp1
Set ComAuthToken Of hoHttp To sTemp1
Set ComRequireSslCertVerify Of hoHttp To True
Set ComFollowRedirects Of hoHttp To False
Set ComConnectTimeout Of hoHttp To 20
Set ComReadTimeout Of hoHttp To 30
Send ComSetRequestHeader To hoHttp "Accept" "application/json"
Get Create (RefClass(cComChilkatHttpResponse)) To hoResp
If (Not(IsComObjectCreated(hoResp))) Begin
Send CreateComObject of hoResp
End
Get Create (RefClass(cComChilkatJsonObject)) To hoResult
If (Not(IsComObjectCreated(hoResult))) Begin
Send CreateComObject of hoResult
End
Set ComStrict Of hoResult To True
Send ComSetRequestHeader To hoHttp "Prefer" 'outlook.timezone="UTC", IdType="ImmutableId"'
Move "YOUR_INVITATION_EVENT_ID" To sEventId
// Choose exactly one: accept, tentativelyAccept, decline.
Move "accept" To sAction
Move True To iSendResponse
If ((sAction <> "accept") And (sAction <> "tentativelyAccept") And (sAction <> "decline")) Begin
Showln "Choose a supported response action."
Procedure_Return
End
Get Create (RefClass(cComChilkatStringBuilder)) To hoEndpoint
If (Not(IsComObjectCreated(hoEndpoint))) Begin
Send CreateComObject of hoEndpoint
End
Get ComAppend Of hoEndpoint "https://graph.microsoft.com/v1.0/me/events/" To iSuccess
Get ComUrlEncode Of hoHttp sEventId To sTemp1
Get ComAppend Of hoEndpoint sTemp1 To iSuccess
Get ComGetAsString Of hoEndpoint To sTemp1
Get pvComObject of hoResp to vResp
Get ComHttpNoBody Of hoHttp "GET" sTemp1 vResp To iSuccess
// A false result can still include an HTTP error response.
Get ComStatusCode Of hoResp To iTemp1
If ((iSuccess <> True) And (iTemp1 = 0)) Begin
Showln "No HTTP response was received. Inspect diagnostics privately; reconcile a write before retrying."
Procedure_Return
End
Get ComStatusCode Of hoResp To iTemp1
If (iTemp1 <> 200) Begin
Get ComStatusCode Of hoResp To iTemp1
Showln "Unexpected HTTP status: " iTemp1
Get ComGetHeaderField Of hoResp "request-id" To sTemp1
Showln "Request ID: " sTemp1
Get ComStatusCode Of hoResp To iTemp1
If (iTemp1 = 429) Begin
Get ComGetHeaderField Of hoResp "Retry-After" To sTemp1
Showln "Retry-After: " sTemp1
End
Get ComStatusCode Of hoResp To iTemp1
If (iTemp1 = 401) Begin
Showln "Refresh the Graph access token or authorize again."
End
Procedure_Return
End
Get ComBodyStr Of hoResp To sTemp1
Get ComLoad Of hoResult sTemp1 To iSuccess
If (iSuccess <> True) Begin
Showln "Expected a well-formed JSON response."
Procedure_Return
End
Get ComJsonTypeOf Of hoResult "isOrganizer" To iTemp1
Get ComBoolOf Of hoResult "isOrganizer" To bTemp1
If ((iTemp1 <> 5) Or (bTemp1 = True)) Begin
Showln "Use the invitation in the attendee mailbox, not the organizer event."
Procedure_Return
End
Get Create (RefClass(cComChilkatJsonObject)) To hoBody
If (Not(IsComObjectCreated(hoBody))) Begin
Send CreateComObject of hoBody
End
Get ComUpdateBool Of hoBody "sendResponse" iSendResponse To iSuccess
Get ComUpdateString Of hoBody "comment" "Response from the application." To iSuccess
Get ComAppend Of hoEndpoint "/" To iSuccess
Get ComAppend Of hoEndpoint sAction To iSuccess
Get ComGetAsString Of hoEndpoint To sTemp1
Get pvComObject of hoBody to vBody
Get pvComObject of hoResp to vResp
Get ComHttpJson Of hoHttp "POST" sTemp1 vBody "application/json" vResp To iSuccess
// A false result can still include an HTTP error response.
Get ComStatusCode Of hoResp To iTemp1
If ((iSuccess <> True) And (iTemp1 = 0)) Begin
Showln "No HTTP response was received. Inspect diagnostics privately; reconcile a write before retrying."
Procedure_Return
End
Get ComStatusCode Of hoResp To iTemp1
If (iTemp1 <> 202) Begin
Get ComStatusCode Of hoResp To iTemp1
Showln "Unexpected HTTP status: " iTemp1
Get ComGetHeaderField Of hoResp "request-id" To sTemp1
Showln "Request ID: " sTemp1
Get ComStatusCode Of hoResp To iTemp1
If (iTemp1 = 429) Begin
Get ComGetHeaderField Of hoResp "Retry-After" To sTemp1
Showln "Retry-After: " sTemp1
End
Get ComStatusCode Of hoResp To iTemp1
If (iTemp1 = 401) Begin
Showln "Refresh the Graph access token or authorize again."
End
Procedure_Return
End
Showln "Meeting response accepted for processing."
End_Procedure