Sample code for 30+ languages & platforms
PowerBuilder

Download GMail Message Attachment by ID

See more GMail REST API Examples

Demonstrates how to download a GMail email attachment by the attachment ID.

Chilkat PowerBuilder Downloads

PowerBuilder
integer li_rc
integer li_Success
oleobject loo_Http
string ls_AttachmentId
string ls_MessageId
string ls_Url
string ls_OutputFilePath
oleobject loo_SbJson
oleobject loo_Json
oleobject loo_Bd

li_Success = 0

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

loo_Http = create oleobject
li_rc = loo_Http.ConnectToNewObject("Chilkat.Http")
if li_rc < 0 then
    destroy loo_Http
    MessageBox("Error","Connecting to COM object failed")
    return
end if

loo_Http.AuthToken = "ACCESS_TOKEN"
loo_Http.Accept = "application/json"

// See the following example:  Get GMail Message (format=full)
// It shows how to download an email by message ID, and it shows how to parse the JSON
// response which includes attachment names, ids, and other information.

// This example will assume we already have the attachment id and filename.

// This is the attachment ID for a file named "helloWorld.pdf".
ls_AttachmentId = "ANGjdJ-oy3aCuZISJKLAhUdaEksCEklbAPyMaWzFgqOMGbPCRkgwgeu_Kttd99C17OBTHROkDZGekibTKWXGfscB5ww7fw4E65_V1dQ-jHhb2TD1Cdm58-BbNw2iDxzptco8iILPiSnLLfFn5Ps7nsRcxHaGTt3r0yqFKCuIYNnPK1vM04BXI_cfzo-HnI4I3tD6oHNHOGVQrL01MdShFQjPELPUjXM8z1qs7Kom-QyvV1iOldUN-66UuhynsmDX-CMM5TIdB-8KD_lmdhf-0DqG8JnCA20XpXyfqwS8XFkPA-t-QSjb7SdkHQFtQ4lz2PcBREFzZ2eI5j0l0Y_dQHRPYTeMwkVl1yl4MfFT4C4iso3VSF-eqaIjiFCbXKCFNyeEIW5WFsv189dhlSqU"

ls_MessageId = "1712bc1dc22da2a2"

loo_Http.SetUrlVar("messageId",ls_MessageId)
loo_Http.SetUrlVar("attachmentId",ls_AttachmentId)

ls_Url = "https://www.googleapis.com/gmail/v1/users/userId/messages/{$messageId}/attachments/{$attachmentId}"

// When we download, the response is JSON that contains the attachment data base64url encoded, like this:
// {
//  "size": 934,
//  "data": "JVBERi0xLjM ... CiUlRU9GCg=="
// }

ls_OutputFilePath = "qa_output/helloWorld.pdf"

// Download into a StringBuilder.
loo_SbJson = create oleobject
li_rc = loo_SbJson.ConnectToNewObject("Chilkat.StringBuilder")

li_Success = loo_Http.DownloadSb(ls_Url,"utf-8",loo_SbJson)
if li_Success = 0 then
    Write-Debug loo_Http.LastErrorText
    destroy loo_Http
    destroy loo_SbJson
    return
end if

if loo_Http.LastStatus <> 200 then
    // Something failed.
    // the JSON contains an error message.
    Write-Debug loo_SbJson.GetAsString()
    destroy loo_Http
    destroy loo_SbJson
    return
end if

// Load into a Chilkat JSON object.
loo_Json = create oleobject
li_rc = loo_Json.ConnectToNewObject("Chilkat.JsonObject")

li_Success = loo_Json.LoadSb(loo_SbJson)

// Extract the base64url data into a BinData.
loo_Bd = create oleobject
li_rc = loo_Bd.ConnectToNewObject("Chilkat.BinData")

li_Success = loo_Json.BytesOf("data","base64url",loo_Bd)

// Save the data to a file.
li_Success = loo_Bd.WriteFile("qa_output/helloWorld.pdf")

Write-Debug "Success."


destroy loo_Http
destroy loo_SbJson
destroy loo_Json
destroy loo_Bd