Tcl 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



load ./chilkat.dll

set rest [new_CkRest]

#   Provide a previously obtained OAuth2 access token.
set oauth2 [new_CkOAuth2]

CkOAuth2_put_AccessToken $oauth2 "OAUTH2_ACCESS_TOKEN"
CkRest_SetAuthOAuth2 $rest $oauth2

set success [CkRest_Connect $rest "www.googleapis.com" 443 1 1]
if {[expr $success != 1]} then {
    puts [CkRest_lastErrorText $rest]
    delete_CkRest $rest
    delete_CkOAuth2 $oauth2
    exit
}

CkRest_AddPathParam $rest "messageId" "15fc23688c553739"

CkRest_AddQueryParam $rest "format" "full"

set sbJson [new_CkStringBuilder]

set success [CkRest_FullRequestNoBodySb $rest "GET" "/gmail/v1/users/me/messages/messageId" $sbJson]
if {[expr $success != 1]} then {
    puts [CkRest_lastErrorText $rest]
    delete_CkRest $rest
    delete_CkOAuth2 $oauth2
    delete_CkStringBuilder $sbJson
    exit
}

if {[expr [CkRest_ResponseStatusCode $rest] != 200]} then {
    puts "Received error response code: [CkRest_ResponseStatusCode $rest]"
    puts "Response body:"
    puts [CkStringBuilder_getAsString $sbJson]
    delete_CkRest $rest
    delete_CkOAuth2 $oauth2
    delete_CkStringBuilder $sbJson
    exit
}

set json [new_CkJsonObject]

CkJsonObject_LoadSb $json $sbJson

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

# json1 is a CkJsonObject

set id [CkJsonObject_stringOf $json "id"]
set threadId [CkJsonObject_stringOf $json "threadId"]
set snippet [CkJsonObject_stringOf $json "snippet"]
set historyId [CkJsonObject_stringOf $json "historyId"]
set internalDate [CkJsonObject_stringOf $json "internalDate"]
set payloadPartId [CkJsonObject_stringOf $json "payload.partId"]
set payloadMimeType [CkJsonObject_stringOf $json "payload.mimeType"]
set payloadFilename [CkJsonObject_stringOf $json "payload.filename"]
set payloadBodySize [CkJsonObject_IntOf $json "payload.body.size"]
set sizeEstimate [CkJsonObject_IntOf $json "sizeEstimate"]
set i 0
set count_i [CkJsonObject_SizeOfArray $json "labelIds"]
while {[expr $i < $count_i]} {
    CkJsonObject_put_I $json $i
    set strVal [CkJsonObject_stringOf $json "labelIds[i]"]
    set i [expr $i + 1]
}
set i 0
set count_i [CkJsonObject_SizeOfArray $json "payload.headers"]
while {[expr $i < $count_i]} {
    CkJsonObject_put_I $json $i
    set name [CkJsonObject_stringOf $json "payload.headers[i].name"]
    set value [CkJsonObject_stringOf $json "payload.headers[i].value"]
    set i [expr $i + 1]
}
set i 0
set count_i [CkJsonObject_SizeOfArray $json "payload.parts"]
while {[expr $i < $count_i]} {
    CkJsonObject_put_I $json $i
    set partId [CkJsonObject_stringOf $json "payload.parts[i].partId"]
    set mimeType [CkJsonObject_stringOf $json "payload.parts[i].mimeType"]
    set filename [CkJsonObject_stringOf $json "payload.parts[i].filename"]
    set bodySize [CkJsonObject_IntOf $json "payload.parts[i].body.size"]
    set bodyAttachmentId [CkJsonObject_stringOf $json "payload.parts[i].body.attachmentId"]
    set j 0
    set count_j [CkJsonObject_SizeOfArray $json "payload.parts[i].headers"]
    while {[expr $j < $count_j]} {
        CkJsonObject_put_J $json $j
        set name [CkJsonObject_stringOf $json "payload.parts[i].headers[j].name"]
        set value [CkJsonObject_stringOf $json "payload.parts[i].headers[j].value"]
        set j [expr $j + 1]
    }
    set j 0
    set count_j [CkJsonObject_SizeOfArray $json "payload.parts[i].parts"]
    while {[expr $j < $count_j]} {
        CkJsonObject_put_J $json $j
        set partId [CkJsonObject_stringOf $json "payload.parts[i].parts[j].partId"]
        set mimeType [CkJsonObject_stringOf $json "payload.parts[i].parts[j].mimeType"]
        set filename [CkJsonObject_stringOf $json "payload.parts[i].parts[j].filename"]
        set bodySize [CkJsonObject_IntOf $json "payload.parts[i].parts[j].body.size"]
        set bodyAttachmentId [CkJsonObject_stringOf $json "payload.parts[i].parts[j].body.attachmentId"]
        set k 0
        set count_k [CkJsonObject_SizeOfArray $json "payload.parts[i].parts[j].headers"]
        while {[expr $k < $count_k]} {
            CkJsonObject_put_K $json $k
            set name [CkJsonObject_stringOf $json "payload.parts[i].parts[j].headers[k].name"]
            set value [CkJsonObject_stringOf $json "payload.parts[i].parts[j].headers[k].value"]
            set k [expr $k + 1]
        }
        set k 0
        set count_k [CkJsonObject_SizeOfArray $json "payload.parts[i].parts[j].parts"]
        while {[expr $k < $count_k]} {
            CkJsonObject_put_K $json $k
            set partId [CkJsonObject_stringOf $json "payload.parts[i].parts[j].parts[k].partId"]
            set mimeType [CkJsonObject_stringOf $json "payload.parts[i].parts[j].parts[k].mimeType"]
            set filename [CkJsonObject_stringOf $json "payload.parts[i].parts[j].parts[k].filename"]
            set bodySize [CkJsonObject_IntOf $json "payload.parts[i].parts[j].parts[k].body.size"]
            set bodyData [CkJsonObject_stringOf $json "payload.parts[i].parts[j].parts[k].body.data"]

            set json1 [CkJsonObject_ObjectOf $json "payload.parts[i].parts[j].parts[k]"]
            set i1 0
            set count_i1 [CkJsonObject_SizeOfArray $json1 "headers"]
            while {[expr $i1 < $count_i1]} {
                CkJsonObject_put_I $json1 $i1
                set name [CkJsonObject_stringOf $json1 "headers[i].name"]
                set value [CkJsonObject_stringOf $json1 "headers[i].value"]
                set i1 [expr $i1 + 1]
            }
            delete_CkJsonObject $json1

            set k [expr $k + 1]
        }
        set j [expr $j + 1]
    }
    set i [expr $i + 1]
}

puts "Example Completed."

delete_CkRest $rest
delete_CkOAuth2 $oauth2
delete_CkStringBuilder $sbJson
delete_CkJsonObject $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
}