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

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.

Google documentation

Chilkat PowerBuilder Downloads

PowerBuilder
integer li_rc
long li_Success
oleobject loo_Token
oleobject loo_Http
oleobject loo_Resp
oleobject loo_Reply
oleobject loo_Url
oleobject loo_Paths
oleobject loo_Part
oleobject loo_ChildPath
long p
long j

li_Success = 0

//  Assumes Chilkat is unlocked. See Global Unlock Sample.

loo_Token = create oleobject
li_rc = loo_Token.ConnectToNewObject("Chilkat.JsonObject")
if li_rc < 0 then
    destroy loo_Token
    MessageBox("Error","Connecting to COM object failed")
    return
end if
li_Success = loo_Token.LoadFile("gmail-token.json")
if li_Success <> 1 then
    Write-Debug "Could not read gmail-token.json from the private working directory."
    destroy loo_Token
    return
end if

if (loo_Token.JsonTypeOf("access_token") <> 1) OR (loo_Token.StringOfEquals("access_token","",1) = 1) then
    Write-Debug "An OAuth access token is required."
    destroy loo_Token
    return
end if

loo_Http = create oleobject
li_rc = loo_Http.ConnectToNewObject("Chilkat.Http")

loo_Http.RequireSslCertVerify = 1
loo_Http.AuthToken = loo_Token.StringOf("access_token")
loo_Resp = create oleobject
li_rc = loo_Resp.ConnectToNewObject("Chilkat.HttpResponse")

loo_Reply = create oleobject
li_rc = loo_Reply.ConnectToNewObject("Chilkat.JsonObject")

loo_Url = create oleobject
li_rc = loo_Url.ConnectToNewObject("Chilkat.StringBuilder")

loo_Url.Append("https://gmail.googleapis.com/gmail/v1/users/me/messages/")
loo_Url.Append("GMAIL_MESSAGE_ID")
loo_Url.Append("?format=full")
li_Success = loo_Http.HttpNoBody("GET",loo_Url.GetAsString(),loo_Resp)
if loo_Resp.StatusCode = 0 then
    Write-Debug "Gmail did not return an HTTP response."
    destroy loo_Token
    destroy loo_Http
    destroy loo_Resp
    destroy loo_Reply
    destroy loo_Url
    return
end if

if (li_Success <> 1) OR (loo_Resp.StatusCode < 200) OR (loo_Resp.StatusCode >= 300) then
    Write-Debug "Gmail HTTP " + string(loo_Resp.StatusCode)
    li_Success = loo_Reply.Load(loo_Resp.BodyStr)
    if li_Success = 1 then
        Write-Debug loo_Reply.StringOf("error.message")
    end if

    destroy loo_Token
    destroy loo_Http
    destroy loo_Resp
    destroy loo_Reply
    destroy loo_Url
    return
end if

li_Success = loo_Reply.Load(loo_Resp.BodyStr)
if li_Success <> 1 then
    Write-Debug "Expected a Gmail JSON response."
    destroy loo_Token
    destroy loo_Http
    destroy loo_Resp
    destroy loo_Reply
    destroy loo_Url
    return
end if

if loo_Reply.JsonTypeOf("payload") <> 3 then
    Write-Debug "Expected a MIME payload."
    destroy loo_Token
    destroy loo_Http
    destroy loo_Resp
    destroy loo_Reply
    destroy loo_Url
    return
end if

//  Queue paths into the original JSON document so any MIME nesting depth is supported.
loo_Paths = create oleobject
li_rc = loo_Paths.ConnectToNewObject("Chilkat.StringTable")

li_Success = loo_Paths.Append("payload")
if li_Success <> 1 then
    Write-Debug "Could not queue the root part."
    destroy loo_Token
    destroy loo_Http
    destroy loo_Resp
    destroy loo_Reply
    destroy loo_Url
    destroy loo_Paths
    return
end if

loo_Part = create oleobject
li_rc = loo_Part.ConnectToNewObject("Chilkat.JsonObject")

loo_ChildPath = create oleobject
li_rc = loo_ChildPath.ConnectToNewObject("Chilkat.StringBuilder")

p = 0
do while (p < loo_Paths.Count)
    li_Success = loo_Reply.ObjectOf2(loo_Paths.StringAt(p),loo_Part)
    if li_Success <> 1 then
        Write-Debug "Could not read a MIME part."
        destroy loo_Token
        destroy loo_Http
        destroy loo_Resp
        destroy loo_Reply
        destroy loo_Url
        destroy loo_Paths
        destroy loo_Part
        destroy loo_ChildPath
        return
    end if

    Write-Debug "Part: " + loo_Part.StringOf("mimeType") + "  bytes: " + string(loo_Part.IntOf("body.size"))
    if loo_Part.JsonTypeOf("filename") = 1 then
        if loo_Part.StringOfEquals("filename","",1) <> 1 then
            Write-Debug "Filename: " + loo_Part.StringOf("filename")
        end if

    end if

    if loo_Part.JsonTypeOf("body.attachmentId") = 1 then
        Write-Debug "Attachment ID: " + loo_Part.StringOf("body.attachmentId")
    else
        if loo_Part.JsonTypeOf("body.data") = 1 then
            Write-Debug "Body data is available in this part."
        end if

    end if

    j = 0
    do while (j < loo_Part.SizeOfArray("parts"))
        loo_ChildPath.SetString(loo_Paths.StringAt(p))
        loo_ChildPath.Append(".parts[")
        loo_ChildPath.AppendInt(j)
        loo_ChildPath.Append("]")
        li_Success = loo_Paths.Append(loo_ChildPath.GetAsString())
        if li_Success <> 1 then
            Write-Debug "Could not queue a child part."
            destroy loo_Token
            destroy loo_Http
            destroy loo_Resp
            destroy loo_Reply
            destroy loo_Url
            destroy loo_Paths
            destroy loo_Part
            destroy loo_ChildPath
            return
        end if

        j = j + 1
    loop
    p = p + 1
loop


destroy loo_Token
destroy loo_Http
destroy loo_Resp
destroy loo_Reply
destroy loo_Url
destroy loo_Paths
destroy loo_Part
destroy loo_ChildPath