PowerBuilder GMail: Get a specific message (format="full")

Back to Index

Gets a specific message using format=full.

Documentation: https://developers.google.com/gmail/api/v1/reference/users/messages/get


integer li_rc
oleobject loo_Rest
integer li_Success
oleobject loo_Oauth2
oleobject loo_SbJson
oleobject loo_Json
string ls_Id
string ls_ThreadId
string ls_Snippet
string ls_HistoryId
string ls_InternalDate
string ls_PayloadPartId
string ls_PayloadMimeType
string ls_PayloadFilename
integer li_PayloadBodySize
integer li_SizeEstimate
integer i
integer li_Count_i
string ls_StrVal
string ls_Name
string ls_Value
string ls_PartId
string ls_MimeType
string ls_Filename
integer li_BodySize
string ls_BodyAttachmentId
integer j
integer li_Count_j
integer k
integer li_Count_k
string ls_BodyData
integer li_I1
integer li_Count_i1
oleobject loo_Json1

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

//   Provide a previously obtained OAuth2 access token.
loo_Oauth2 = create oleobject
li_rc = loo_Oauth2.ConnectToNewObject("Chilkat_9_5_0.OAuth2")

loo_Oauth2.AccessToken = "OAUTH2_ACCESS_TOKEN"
loo_Rest.SetAuthOAuth2(loo_Oauth2)

li_Success = loo_Rest.Connect("www.googleapis.com",443,1,1)
if li_Success <> 1 then
    Write-Debug loo_Rest.LastErrorText
    destroy loo_Rest
    destroy loo_Oauth2
    return
end if

loo_Rest.AddPathParam("messageId","15fc23688c553739")

loo_Rest.AddQueryParam("format","full")

loo_SbJson = create oleobject
li_rc = loo_SbJson.ConnectToNewObject("Chilkat_9_5_0.StringBuilder")

li_Success = loo_Rest.FullRequestNoBodySb("GET","/gmail/v1/users/me/messages/messageId",loo_SbJson)
if li_Success <> 1 then
    Write-Debug loo_Rest.LastErrorText
    destroy loo_Rest
    destroy loo_Oauth2
    destroy loo_SbJson
    return
end if

if loo_Rest.ResponseStatusCode <> 200 then
    Write-Debug "Received error response code: " + string(loo_Rest.ResponseStatusCode)
    Write-Debug "Response body:"
    Write-Debug loo_SbJson.GetAsString()
    destroy loo_Rest
    destroy loo_Oauth2
    destroy loo_SbJson
    return
end if

loo_Json = create oleobject
li_rc = loo_Json.ConnectToNewObject("Chilkat_9_5_0.JsonObject")

loo_Json.LoadSb(loo_SbJson)

//  The following code parses the JSON response.
//  A sample JSON response is shown below the sample code.

ls_Id = loo_Json.StringOf("id")
ls_ThreadId = loo_Json.StringOf("threadId")
ls_Snippet = loo_Json.StringOf("snippet")
ls_HistoryId = loo_Json.StringOf("historyId")
ls_InternalDate = loo_Json.StringOf("internalDate")
ls_PayloadPartId = loo_Json.StringOf("payload.partId")
ls_PayloadMimeType = loo_Json.StringOf("payload.mimeType")
ls_PayloadFilename = loo_Json.StringOf("payload.filename")
li_PayloadBodySize = loo_Json.IntOf("payload.body.size")
li_SizeEstimate = loo_Json.IntOf("sizeEstimate")
i = 0
li_Count_i = loo_Json.SizeOfArray("labelIds")
do while i < li_Count_i
    loo_Json.I = i
    ls_StrVal = loo_Json.StringOf("labelIds[i]")
    i = i + 1
loop
i = 0
li_Count_i = loo_Json.SizeOfArray("payload.headers")
do while i < li_Count_i
    loo_Json.I = i
    ls_Name = loo_Json.StringOf("payload.headers[i].name")
    ls_Value = loo_Json.StringOf("payload.headers[i].value")
    i = i + 1
loop
i = 0
li_Count_i = loo_Json.SizeOfArray("payload.parts")
do while i < li_Count_i
    loo_Json.I = i
    ls_PartId = loo_Json.StringOf("payload.parts[i].partId")
    ls_MimeType = loo_Json.StringOf("payload.parts[i].mimeType")
    ls_Filename = loo_Json.StringOf("payload.parts[i].filename")
    li_BodySize = loo_Json.IntOf("payload.parts[i].body.size")
    ls_BodyAttachmentId = loo_Json.StringOf("payload.parts[i].body.attachmentId")
    j = 0
    li_Count_j = loo_Json.SizeOfArray("payload.parts[i].headers")
    do while j < li_Count_j
        loo_Json.J = j
        ls_Name = loo_Json.StringOf("payload.parts[i].headers[j].name")
        ls_Value = loo_Json.StringOf("payload.parts[i].headers[j].value")
        j = j + 1
    loop
    j = 0
    li_Count_j = loo_Json.SizeOfArray("payload.parts[i].parts")
    do while j < li_Count_j
        loo_Json.J = j
        ls_PartId = loo_Json.StringOf("payload.parts[i].parts[j].partId")
        ls_MimeType = loo_Json.StringOf("payload.parts[i].parts[j].mimeType")
        ls_Filename = loo_Json.StringOf("payload.parts[i].parts[j].filename")
        li_BodySize = loo_Json.IntOf("payload.parts[i].parts[j].body.size")
        ls_BodyAttachmentId = loo_Json.StringOf("payload.parts[i].parts[j].body.attachmentId")
        k = 0
        li_Count_k = loo_Json.SizeOfArray("payload.parts[i].parts[j].headers")
        do while k < li_Count_k
            loo_Json.K = k
            ls_Name = loo_Json.StringOf("payload.parts[i].parts[j].headers[k].name")
            ls_Value = loo_Json.StringOf("payload.parts[i].parts[j].headers[k].value")
            k = k + 1
        loop
        k = 0
        li_Count_k = loo_Json.SizeOfArray("payload.parts[i].parts[j].parts")
        do while k < li_Count_k
            loo_Json.K = k
            ls_PartId = loo_Json.StringOf("payload.parts[i].parts[j].parts[k].partId")
            ls_MimeType = loo_Json.StringOf("payload.parts[i].parts[j].parts[k].mimeType")
            ls_Filename = loo_Json.StringOf("payload.parts[i].parts[j].parts[k].filename")
            li_BodySize = loo_Json.IntOf("payload.parts[i].parts[j].parts[k].body.size")
            ls_BodyData = loo_Json.StringOf("payload.parts[i].parts[j].parts[k].body.data")

            loo_Json1 = loo_Json.ObjectOf("payload.parts[i].parts[j].parts[k]")
            li_I1 = 0
            li_Count_i1 = loo_Json1.SizeOfArray("headers")
            do while li_I1 < li_Count_i1
                loo_Json1.I = li_I1
                ls_Name = loo_Json1.StringOf("headers[i].name")
                ls_Value = loo_Json1.StringOf("headers[i].value")
                li_I1 = li_I1 + 1
            loop
            destroy loo_Json1
            k = k + 1
        loop
        j = j + 1
    loop
    i = i + 1
loop

Write-Debug "Example Completed."


destroy loo_Rest
destroy loo_Oauth2
destroy loo_SbJson
destroy loo_Json

Sample JSON Response Body

{
  "id": "15fc23688c553739",
  "threadId": "15fc23688c553739",
  "labelIds": [
    "IMPORTANT",
    "SENT",
    "INBOX"
  ],
  "snippet": "This is a test. \u200b",
  "historyId": "1438",
  "internalDate": "1510791874000",
  "payload": {
    "partId": "",
    "mimeType": "multipart/mixed",
    "filename": "",
    "headers": [
      {
        "name": "MIME-Version",
        "value": "1.0"
      },
      {
        "name": "Received",
        "value": "by 10.25.23.105 with HTTP; Wed, 15 Nov 2017 16:24:34 -0800 (PST)"
      },
      {
        "name": "X-Originating-IP",
        "value": "[2601:249:e00:9093:e894:b27f:e5d6:d01d]"
      },
      {
        "name": "Date",
        "value": "Wed, 15 Nov 2017 18:24:34 -0600"
      },
      {
        "name": "Delivered-To",
        "value": "matt@chilkat.io"
      },
      {
        "name": "Message-ID",
        "value": "\u003cCAPhjyBM1qqXS6NVB65W8Zt5-McF03qWSRDjW5am66wTMuard=w@mail.gmail.com\u003e"
      },
      {
        "name": "Subject",
        "value": "Test HTML email with image and attachment."
      },
      {
        "name": "From",
        "value": "Matt Fausey \u003cmatt@chilkat.io\u003e"
      },
      {
        "name": "To",
        "value": "Matt Fausey \u003cmatt@chilkat.io\u003e"
      },
      {
        "name": "Content-Type",
        "value": "multipart/mixed; boundary=\"001a11401a6c097887055e0ea572\""
      }
    ],
    "body": {
      "size": 0
    },
    "parts": [
      {
        "partId": "0",
        "mimeType": "multipart/related",
        "filename": "",
        "headers": [
          {
            "name": "Content-Type",
            "value": "multipart/related; boundary=\"001a11401a6c097885055e0ea571\""
          }
        ],
        "body": {
          "size": 0
        },
        "parts": [
          {
            "partId": "0.0",
            "mimeType": "multipart/alternative",
            "filename": "",
            "headers": [
              {
                "name": "Content-Type",
                "value": "multipart/alternative; boundary=\"001a11401a6c097882055e0ea570\""
              }
            ],
            "body": {
              "size": 0
            },
            "parts": [
              {
                "partId": "0.0.0",
                "mimeType": "text/plain",
                "filename": "",
                "headers": [
                  {
                    "name": "Content-Type",
                    "value": "text/plain; charset=\"UTF-8\""
                  },
                  {
                    "name": "Content-Transfer-Encoding",
                    "value": "quoted-printable"
                  }
                ],
                "body": {
                  "size": 22,
                  "data": "VGhpcyBpcyBhIHRlc3QuDQrigIsNCg=="
                }
              },
              {
                "partId": "0.0.1",
                "mimeType": "text/html",
                "filename": "",
                "headers": [
                  {
                    "name": "Content-Type",
                    "value": "text/html; charset=\"UTF-8\""
                  },
                  {
                    "name": "Content-Transfer-Encoding",
                    "value": "quoted-printable"
                  }
                ],
                "body": {
                  "size": 115,
                  "data": "PGRpdiBkaXI9Imx0ciI-VGhpcyBpcyBhIHRlc3QuPGltZyB3aWR0aD0iMTIwIiBoZWlnaHQ9IjEyMCIgc3JjPSJjaWQ6aWlfamExcWY5MGwwXzE1ZmMyMzUzOWJkMDA3MjciPjxicj7igIs8L2Rpdj4NCg=="
                }
              }
            ]
          },
          {
            "partId": "0.1",
            "mimeType": "image/jpeg",
            "filename": "starfish.jpg",
            "headers": [
              {
                "name": "Content-Type",
                "value": "image/jpeg; name=\"starfish.jpg\""
              },
              {
                "name": "Content-Disposition",
                "value": "inline; filename=\"starfish.jpg\""
              },
              {
                "name": "Content-Transfer-Encoding",
                "value": "base64"
              },
              {
                "name": "Content-ID",
                "value": "\u003cii_ja1qf90l0_15fc23539bd00727\u003e"
              },
              {
                "name": "X-Attachment-Id",
                "value": "ii_ja1qf90l0_15fc23539bd00727"
              },
              {
                "name": "MIME-Version",
                "value": "1.0"
              },
              {
                "name": "Date",
                "value": "Wed, 15 Nov 2017 16:23:08 -0800"
              },
              {
                "name": "Message-ID",
                "value": "\u003cCAPhjyBMicJ9k-EXY5Efcvp8o_z_J3x8_G=mnC_znLXnyBogemQ@mail.gmail.com\u003e"
              },
              {
                "name": "Subject",
                "value": "Attachment"
              },
              {
                "name": "From",
                "value": "Matt Fausey \u003cmatt@chilkat.io\u003e"
              }
            ],
            "body": {
              "attachmentId": "ANGjdJ_ueuzzM9bpQ8l_tD27oDfn-smCe77viIAF1wy8ZeI09tK5vAuSS6V9Bahow4ikqaGleXqdQBeq8xhFkJ7WN-KKBSKvOfT2yD-XMlA1FC8AOKS2A8tBi5dDDbFrbnns33rfqEIRFAISslhdE481uB6sYfVoYjpuDThtW6Y96YA0Aso0P9txbKJwcwOeD0RxDav_vPzDjHuRuOYsGxbVTxesMB2eiMMw3i1y-WAu-pyWHqF0xMFehZmd7d-krKKdvI4UEgtzD9siXnnaH6MG55ykjBsjFRsmjKc-4K5fHeYHOf586Pkej5K238A",
              "size": 6229
            }
          }
        ]
      },
      {
        "partId": "1",
        "mimeType": "application/pdf",
        "filename": "helloWorld.pdf",
        "headers": [
          {
            "name": "Content-Type",
            "value": "application/pdf; name=\"helloWorld.pdf\""
          },
          {
            "name": "Content-Disposition",
            "value": "attachment; filename=\"helloWorld.pdf\""
          },
          {
            "name": "Content-Transfer-Encoding",
            "value": "base64"
          },
          {
            "name": "X-Attachment-Id",
            "value": "f_ja1qgya31"
          }
        ],
        "body": {
          "attachmentId": "ANGjdJ934ScA-kvdsvBvOjO9z-RYmRLyO8PvqOCzwkk-smeLVpKqeoxXNIn9FL_RbzA6qmTWKQTJehrBVmX4NqYtFsivmwRfzIWj-A-SqvJATRhjEGOntUeBS0fPZ9xy_enmH5Qgi0iSdBmwAn1CBnNghEKdMjvxTMjLxqn2mSO4gvxIh8ahJZDMN1p8KMYO98dM2XfyNndltnK-sd_DizNR9BQu10w5WSTLeFfOh61SxGKeq4Dt3W5USQlx7dEG7dRiBDvjFM6wo2HVeSnGEVphOwx80t1MQdhRN-YOHCAahIqy7Z0-BkPIIjTeUtl_pST8oMi0IgJoo9x1oomY3JZp2OxWuNyOVWDGVsDavGHc6wRhwK5pJrDceCWxNdPEwExeYA84WOAFXE2D6I3m",
          "size": 934
        }
      }
    ]
  },
  "sizeEstimate": 11716
}