PureBasic Requires Chilkat v11.0.0+
PureBasic
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 PureBasic Downloads
IncludeFile "CkHttpResponse.pb"
IncludeFile "CkHttp.pb"
IncludeFile "CkStringBuilder.pb"
IncludeFile "CkJsonObject.pb"
IncludeFile "CkStringTable.pb"
Procedure ChilkatExample()
success.i = 0
; Assumes Chilkat is unlocked. See Global Unlock Sample.
token.i = CkJsonObject::ckCreate()
If token.i = 0
Debug "Failed to create object."
ProcedureReturn
EndIf
success = CkJsonObject::ckLoadFile(token,"gmail-token.json")
If success <> 1
Debug "Could not read gmail-token.json from the private working directory."
CkJsonObject::ckDispose(token)
ProcedureReturn
EndIf
If (CkJsonObject::ckJsonTypeOf(token,"access_token") <> 1) OR (CkJsonObject::ckStringOfEquals(token,"access_token","",1) = 1)
Debug "An OAuth access token is required."
CkJsonObject::ckDispose(token)
ProcedureReturn
EndIf
http.i = CkHttp::ckCreate()
If http.i = 0
Debug "Failed to create object."
ProcedureReturn
EndIf
CkHttp::setCkRequireSslCertVerify(http, 1)
CkHttp::setCkAuthToken(http, CkJsonObject::ckStringOf(token,"access_token"))
resp.i = CkHttpResponse::ckCreate()
If resp.i = 0
Debug "Failed to create object."
ProcedureReturn
EndIf
reply.i = CkJsonObject::ckCreate()
If reply.i = 0
Debug "Failed to create object."
ProcedureReturn
EndIf
url.i = CkStringBuilder::ckCreate()
If url.i = 0
Debug "Failed to create object."
ProcedureReturn
EndIf
CkStringBuilder::ckAppend(url,"https://gmail.googleapis.com/gmail/v1/users/me/messages/")
CkStringBuilder::ckAppend(url,"GMAIL_MESSAGE_ID")
CkStringBuilder::ckAppend(url,"?format=full")
success = CkHttp::ckHttpNoBody(http,"GET",CkStringBuilder::ckGetAsString(url),resp)
If CkHttpResponse::ckStatusCode(resp) = 0
Debug "Gmail did not return an HTTP response."
CkJsonObject::ckDispose(token)
CkHttp::ckDispose(http)
CkHttpResponse::ckDispose(resp)
CkJsonObject::ckDispose(reply)
CkStringBuilder::ckDispose(url)
ProcedureReturn
EndIf
If (success <> 1) OR (CkHttpResponse::ckStatusCode(resp) < 200) OR (CkHttpResponse::ckStatusCode(resp) >= 300)
Debug "Gmail HTTP " + Str(CkHttpResponse::ckStatusCode(resp))
success = CkJsonObject::ckLoad(reply,CkHttpResponse::ckBodyStr(resp))
If success = 1
Debug CkJsonObject::ckStringOf(reply,"error.message")
EndIf
CkJsonObject::ckDispose(token)
CkHttp::ckDispose(http)
CkHttpResponse::ckDispose(resp)
CkJsonObject::ckDispose(reply)
CkStringBuilder::ckDispose(url)
ProcedureReturn
EndIf
success = CkJsonObject::ckLoad(reply,CkHttpResponse::ckBodyStr(resp))
If success <> 1
Debug "Expected a Gmail JSON response."
CkJsonObject::ckDispose(token)
CkHttp::ckDispose(http)
CkHttpResponse::ckDispose(resp)
CkJsonObject::ckDispose(reply)
CkStringBuilder::ckDispose(url)
ProcedureReturn
EndIf
If CkJsonObject::ckJsonTypeOf(reply,"payload") <> 3
Debug "Expected a MIME payload."
CkJsonObject::ckDispose(token)
CkHttp::ckDispose(http)
CkHttpResponse::ckDispose(resp)
CkJsonObject::ckDispose(reply)
CkStringBuilder::ckDispose(url)
ProcedureReturn
EndIf
; Queue paths into the original JSON document so any MIME nesting depth is supported.
paths.i = CkStringTable::ckCreate()
If paths.i = 0
Debug "Failed to create object."
ProcedureReturn
EndIf
success = CkStringTable::ckAppend(paths,"payload")
If success <> 1
Debug "Could not queue the root part."
CkJsonObject::ckDispose(token)
CkHttp::ckDispose(http)
CkHttpResponse::ckDispose(resp)
CkJsonObject::ckDispose(reply)
CkStringBuilder::ckDispose(url)
CkStringTable::ckDispose(paths)
ProcedureReturn
EndIf
part.i = CkJsonObject::ckCreate()
If part.i = 0
Debug "Failed to create object."
ProcedureReturn
EndIf
childPath.i = CkStringBuilder::ckCreate()
If childPath.i = 0
Debug "Failed to create object."
ProcedureReturn
EndIf
p.i = 0
While (p < CkStringTable::ckCount(paths))
success = CkJsonObject::ckObjectOf2(reply,CkStringTable::ckStringAt(paths,p),part)
If success <> 1
Debug "Could not read a MIME part."
CkJsonObject::ckDispose(token)
CkHttp::ckDispose(http)
CkHttpResponse::ckDispose(resp)
CkJsonObject::ckDispose(reply)
CkStringBuilder::ckDispose(url)
CkStringTable::ckDispose(paths)
CkJsonObject::ckDispose(part)
CkStringBuilder::ckDispose(childPath)
ProcedureReturn
EndIf
Debug "Part: " + CkJsonObject::ckStringOf(part,"mimeType") + " bytes: " + Str(CkJsonObject::ckIntOf(part,"body.size"))
If CkJsonObject::ckJsonTypeOf(part,"filename") = 1
If CkJsonObject::ckStringOfEquals(part,"filename","",1) <> 1
Debug "Filename: " + CkJsonObject::ckStringOf(part,"filename")
EndIf
EndIf
If CkJsonObject::ckJsonTypeOf(part,"body.attachmentId") = 1
Debug "Attachment ID: " + CkJsonObject::ckStringOf(part,"body.attachmentId")
Else
If CkJsonObject::ckJsonTypeOf(part,"body.data") = 1
Debug "Body data is available in this part."
EndIf
EndIf
j.i = 0
While (j < CkJsonObject::ckSizeOfArray(part,"parts"))
CkStringBuilder::ckSetString(childPath,CkStringTable::ckStringAt(paths,p))
CkStringBuilder::ckAppend(childPath,".parts[")
CkStringBuilder::ckAppendInt(childPath,j)
CkStringBuilder::ckAppend(childPath,"]")
success = CkStringTable::ckAppend(paths,CkStringBuilder::ckGetAsString(childPath))
If success <> 1
Debug "Could not queue a child part."
CkJsonObject::ckDispose(token)
CkHttp::ckDispose(http)
CkHttpResponse::ckDispose(resp)
CkJsonObject::ckDispose(reply)
CkStringBuilder::ckDispose(url)
CkStringTable::ckDispose(paths)
CkJsonObject::ckDispose(part)
CkStringBuilder::ckDispose(childPath)
ProcedureReturn
EndIf
j = j + 1
Wend
p = p + 1
Wend
CkJsonObject::ckDispose(token)
CkHttp::ckDispose(http)
CkHttpResponse::ckDispose(resp)
CkJsonObject::ckDispose(reply)
CkStringBuilder::ckDispose(url)
CkStringTable::ckDispose(paths)
CkJsonObject::ckDispose(part)
CkStringBuilder::ckDispose(childPath)
ProcedureReturn
EndProcedure