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

Download a Gmail attachment by ID

See more GMail REST API Examples

Use a Gmail message ID and a body.attachmentId from the same message. Find the attachment ID by inspecting its MIME parts. Decode the returned data as base64url, check the decoded byte count, and save it to an application-chosen path. A part containing body.data can be decoded directly from the full message.

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 DataFlex Downloads

DataFlex
Use ChilkatAx-win32.pkg

Procedure Test
    Boolean iSuccess
    Handle hoToken
    Handle hoHttp
    Variant vResp
    Handle hoResp
    Handle hoReply
    Handle hoUrl
    Variant vData
    Handle hoData
    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 "/attachments/" To iSuccess
    Get ComAppend Of hoUrl "GMAIL_ATTACHMENT_ID" 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 "data" To iTemp1
    Get ComJsonTypeOf Of hoReply "size" To iTemp2
    If ((iTemp1 <> 1) Or (iTemp2 <> 2)) Begin
        Showln "Expected attachment data and a byte count."
        Procedure_Return
    End

    Get Create (RefClass(cComChilkatBinData)) To hoData
    If (Not(IsComObjectCreated(hoData))) Begin
        Send CreateComObject of hoData
    End
    Get ComStringOfEquals Of hoReply "data" "" True To bTemp1
    If (bTemp1 <> True) Begin
        Get pvComObject of hoData to vData
        Get ComBytesOf Of hoReply "data" "base64url" vData To iSuccess
        If (iSuccess <> True) Begin
            Showln "Could not decode the attachment."
            Procedure_Return
        End

    End

    Get ComNumBytes Of hoData To iTemp1
    Get ComIntOf Of hoReply "size" To iTemp2
    If (iTemp1 <> iTemp2) Begin
        Showln "Attachment size mismatch."
        Procedure_Return
    End

    Get ComWriteFile Of hoData "downloaded-attachment.bin" To iSuccess
    If (iSuccess <> True) Begin
        Showln "Could not save the attachment."
        Procedure_Return
    End

    Get ComNumBytes Of hoData To iTemp1
    Showln "Saved " iTemp1 " bytes."


End_Procedure