DataFlex Requires Chilkat v11.0.0+
DataFlex
Inspect the MIME parts of a Gmail message
See more GMail REST API Examples
Supply a Gmail message ID and request format=full. Walk the root payload and every nested parts array, including a single-part root. For each part, show its MIME type, filename, and size. A body.attachmentId can be passed to the attachment download example. Inline body.data is already included as base64url.
Authorize the application with https://www.googleapis.com/auth/gmail.readonly. The example reads the access token from gmail-token.json in a private working directory. Authorize a desktop application.
Chilkat DataFlex Downloads
Use ChilkatAx-win32.pkg
Procedure Test
Boolean iSuccess
Handle hoToken
Handle hoHttp
Variant vResp
Handle hoResp
Handle hoReply
Handle hoUrl
Handle hoPaths
Variant vPart
Handle hoPart
Handle hoChildPath
Integer p
Integer j
String sTemp1
Integer iTemp1
Integer iTemp2
Boolean bTemp1
Move False To iSuccess
// Assumes Chilkat is unlocked. See Global Unlock Sample.
Get Create (RefClass(cComChilkatJsonObject)) To hoToken
If (Not(IsComObjectCreated(hoToken))) Begin
Send CreateComObject of hoToken
End
Get ComLoadFile Of hoToken "gmail-token.json" To iSuccess
If (iSuccess <> True) Begin
Showln "Could not read gmail-token.json from the private working directory."
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 "An OAuth access token is required."
Procedure_Return
End
Get Create (RefClass(cComChilkatHttp)) To hoHttp
If (Not(IsComObjectCreated(hoHttp))) Begin
Send CreateComObject of hoHttp
End
Set ComRequireSslCertVerify Of hoHttp To True
Get ComStringOf Of hoToken "access_token" To sTemp1
Set ComAuthToken Of hoHttp To sTemp1
Get Create (RefClass(cComChilkatHttpResponse)) To hoResp
If (Not(IsComObjectCreated(hoResp))) Begin
Send CreateComObject of hoResp
End
Get Create (RefClass(cComChilkatJsonObject)) To hoReply
If (Not(IsComObjectCreated(hoReply))) Begin
Send CreateComObject of hoReply
End
Get Create (RefClass(cComChilkatStringBuilder)) To hoUrl
If (Not(IsComObjectCreated(hoUrl))) Begin
Send CreateComObject of hoUrl
End
Get ComAppend Of hoUrl "https://gmail.googleapis.com/gmail/v1/users/me/messages/" To iSuccess
Get ComAppend Of hoUrl "GMAIL_MESSAGE_ID" To iSuccess
Get ComAppend Of hoUrl "?format=full" To iSuccess
Get ComGetAsString Of hoUrl To sTemp1
Get pvComObject of hoResp to vResp
Get ComHttpNoBody Of hoHttp "GET" sTemp1 vResp To iSuccess
Get ComStatusCode Of hoResp To iTemp1
If (iTemp1 = 0) Begin
Showln "Gmail did not return an HTTP response."
Procedure_Return
End
Get ComStatusCode Of hoResp To iTemp1
Get ComStatusCode Of hoResp To iTemp2
If ((iSuccess <> True) Or (iTemp1 < 200) Or (iTemp2 >= 300)) Begin
Get ComStatusCode Of hoResp To iTemp1
Showln "Gmail HTTP " iTemp1
Get ComBodyStr Of hoResp To sTemp1
Get ComLoad Of hoReply sTemp1 To iSuccess
If (iSuccess = True) Begin
Get ComStringOf Of hoReply "error.message" To sTemp1
Showln sTemp1
End
Procedure_Return
End
Get ComBodyStr Of hoResp To sTemp1
Get ComLoad Of hoReply sTemp1 To iSuccess
If (iSuccess <> True) Begin
Showln "Expected a Gmail JSON response."
Procedure_Return
End
Get ComJsonTypeOf Of hoReply "payload" To iTemp1
If (iTemp1 <> 3) Begin
Showln "Expected a MIME payload."
Procedure_Return
End
// Queue paths into the original JSON document so any MIME nesting depth is supported.
Get Create (RefClass(cComChilkatStringTable)) To hoPaths
If (Not(IsComObjectCreated(hoPaths))) Begin
Send CreateComObject of hoPaths
End
Get ComAppend Of hoPaths "payload" To iSuccess
If (iSuccess <> True) Begin
Showln "Could not queue the root part."
Procedure_Return
End
Get Create (RefClass(cComChilkatJsonObject)) To hoPart
If (Not(IsComObjectCreated(hoPart))) Begin
Send CreateComObject of hoPart
End
Get Create (RefClass(cComChilkatStringBuilder)) To hoChildPath
If (Not(IsComObjectCreated(hoChildPath))) Begin
Send CreateComObject of hoChildPath
End
Move 0 To p
While (p < (ComCount(hoPaths)))
Get ComStringAt Of hoPaths p To sTemp1
Get pvComObject of hoPart to vPart
Get ComObjectOf2 Of hoReply sTemp1 vPart To iSuccess
If (iSuccess <> True) Begin
Showln "Could not read a MIME part."
Procedure_Return
End
Get ComStringOf Of hoPart "mimeType" To sTemp1
Get ComIntOf Of hoPart "body.size" To iTemp1
Showln "Part: " sTemp1 " bytes: " iTemp1
Get ComJsonTypeOf Of hoPart "filename" To iTemp1
If (iTemp1 = 1) Begin
Get ComStringOfEquals Of hoPart "filename" "" True To bTemp1
If (bTemp1 <> True) Begin
Get ComStringOf Of hoPart "filename" To sTemp1
Showln "Filename: " sTemp1
End
End
Get ComJsonTypeOf Of hoPart "body.attachmentId" To iTemp1
If (iTemp1 = 1) Begin
Get ComStringOf Of hoPart "body.attachmentId" To sTemp1
Showln "Attachment ID: " sTemp1
End
Else Begin
Get ComJsonTypeOf Of hoPart "body.data" To iTemp1
If (iTemp1 = 1) Begin
Showln "Body data is available in this part."
End
End
Move 0 To j
While (j < (ComSizeOfArray(hoPart, "parts")))
Get ComStringAt Of hoPaths p To sTemp1
Get ComSetString Of hoChildPath sTemp1 To iSuccess
Get ComAppend Of hoChildPath ".parts[" To iSuccess
Get ComAppendInt Of hoChildPath j To iSuccess
Get ComAppend Of hoChildPath "]" To iSuccess
Get ComGetAsString Of hoChildPath To sTemp1
Get ComAppend Of hoPaths sTemp1 To iSuccess
If (iSuccess <> True) Begin
Showln "Could not queue a child part."
Procedure_Return
End
Move (j + 1) To j
Loop
Move (p + 1) To p
Loop
End_Procedure