Unicode C 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


#include <C_CkRestW.h>
#include <C_CkOAuth2W.h>
#include <C_CkStringBuilderW.h>
#include <C_CkJsonObjectW.h>

void ChilkatSample(void)
    {
    HCkRestW rest;
    BOOL success;
    HCkOAuth2W oauth2;
    HCkStringBuilderW sbJson;
    HCkJsonObjectW json;
    const wchar_t *id;
    const wchar_t *threadId;
    const wchar_t *snippet;
    const wchar_t *historyId;
    const wchar_t *internalDate;
    const wchar_t *payloadPartId;
    const wchar_t *payloadMimeType;
    const wchar_t *payloadFilename;
    int payloadBodySize;
    int sizeEstimate;
    int i;
    int count_i;
    const wchar_t *strVal;
    const wchar_t *name;
    const wchar_t *value;
    const wchar_t *partId;
    const wchar_t *mimeType;
    const wchar_t *filename;
    int bodySize;
    const wchar_t *bodyAttachmentId;
    int j;
    int count_j;
    int k;
    int count_k;
    const wchar_t *bodyData;
    int i1;
    int count_i1;
    HCkJsonObjectW json1;

    rest = CkRestW_Create();

    //   Provide a previously obtained OAuth2 access token.
    oauth2 = CkOAuth2W_Create();
    CkOAuth2W_putAccessToken(oauth2,L"OAUTH2_ACCESS_TOKEN");
    CkRestW_SetAuthOAuth2(rest,oauth2);

    success = CkRestW_Connect(rest,L"www.googleapis.com",443,TRUE,TRUE);
    if (success != TRUE) {
        wprintf(L"%s\n",CkRestW_lastErrorText(rest));
        CkRestW_Dispose(rest);
        CkOAuth2W_Dispose(oauth2);
        return;
    }

    CkRestW_AddPathParam(rest,L"messageId",L"15fc23688c553739");

    CkRestW_AddQueryParam(rest,L"format",L"full");

    sbJson = CkStringBuilderW_Create();
    success = CkRestW_FullRequestNoBodySb(rest,L"GET",L"/gmail/v1/users/me/messages/messageId",sbJson);
    if (success != TRUE) {
        wprintf(L"%s\n",CkRestW_lastErrorText(rest));
        CkRestW_Dispose(rest);
        CkOAuth2W_Dispose(oauth2);
        CkStringBuilderW_Dispose(sbJson);
        return;
    }

    if (CkRestW_getResponseStatusCode(rest) != 200) {
        wprintf(L"Received error response code: %d\n",CkRestW_getResponseStatusCode(rest));
        wprintf(L"Response body:\n");
        wprintf(L"%s\n",CkStringBuilderW_getAsString(sbJson));
        CkRestW_Dispose(rest);
        CkOAuth2W_Dispose(oauth2);
        CkStringBuilderW_Dispose(sbJson);
        return;
    }

    json = CkJsonObjectW_Create();
    CkJsonObjectW_LoadSb(json,sbJson);

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

    id = CkJsonObjectW_stringOf(json,L"id");
    threadId = CkJsonObjectW_stringOf(json,L"threadId");
    snippet = CkJsonObjectW_stringOf(json,L"snippet");
    historyId = CkJsonObjectW_stringOf(json,L"historyId");
    internalDate = CkJsonObjectW_stringOf(json,L"internalDate");
    payloadPartId = CkJsonObjectW_stringOf(json,L"payload.partId");
    payloadMimeType = CkJsonObjectW_stringOf(json,L"payload.mimeType");
    payloadFilename = CkJsonObjectW_stringOf(json,L"payload.filename");
    payloadBodySize = CkJsonObjectW_IntOf(json,L"payload.body.size");
    sizeEstimate = CkJsonObjectW_IntOf(json,L"sizeEstimate");
    i = 0;
    count_i = CkJsonObjectW_SizeOfArray(json,L"labelIds");
    while (i < count_i) {
        CkJsonObjectW_putI(json,i);
        strVal = CkJsonObjectW_stringOf(json,L"labelIds[i]");
        i = i + 1;
    }

    i = 0;
    count_i = CkJsonObjectW_SizeOfArray(json,L"payload.headers");
    while (i < count_i) {
        CkJsonObjectW_putI(json,i);
        name = CkJsonObjectW_stringOf(json,L"payload.headers[i].name");
        value = CkJsonObjectW_stringOf(json,L"payload.headers[i].value");
        i = i + 1;
    }

    i = 0;
    count_i = CkJsonObjectW_SizeOfArray(json,L"payload.parts");
    while (i < count_i) {
        CkJsonObjectW_putI(json,i);
        partId = CkJsonObjectW_stringOf(json,L"payload.parts[i].partId");
        mimeType = CkJsonObjectW_stringOf(json,L"payload.parts[i].mimeType");
        filename = CkJsonObjectW_stringOf(json,L"payload.parts[i].filename");
        bodySize = CkJsonObjectW_IntOf(json,L"payload.parts[i].body.size");
        bodyAttachmentId = CkJsonObjectW_stringOf(json,L"payload.parts[i].body.attachmentId");
        j = 0;
        count_j = CkJsonObjectW_SizeOfArray(json,L"payload.parts[i].headers");
        while (j < count_j) {
            CkJsonObjectW_putJ(json,j);
            name = CkJsonObjectW_stringOf(json,L"payload.parts[i].headers[j].name");
            value = CkJsonObjectW_stringOf(json,L"payload.parts[i].headers[j].value");
            j = j + 1;
        }

        j = 0;
        count_j = CkJsonObjectW_SizeOfArray(json,L"payload.parts[i].parts");
        while (j < count_j) {
            CkJsonObjectW_putJ(json,j);
            partId = CkJsonObjectW_stringOf(json,L"payload.parts[i].parts[j].partId");
            mimeType = CkJsonObjectW_stringOf(json,L"payload.parts[i].parts[j].mimeType");
            filename = CkJsonObjectW_stringOf(json,L"payload.parts[i].parts[j].filename");
            bodySize = CkJsonObjectW_IntOf(json,L"payload.parts[i].parts[j].body.size");
            bodyAttachmentId = CkJsonObjectW_stringOf(json,L"payload.parts[i].parts[j].body.attachmentId");
            k = 0;
            count_k = CkJsonObjectW_SizeOfArray(json,L"payload.parts[i].parts[j].headers");
            while (k < count_k) {
                CkJsonObjectW_putK(json,k);
                name = CkJsonObjectW_stringOf(json,L"payload.parts[i].parts[j].headers[k].name");
                value = CkJsonObjectW_stringOf(json,L"payload.parts[i].parts[j].headers[k].value");
                k = k + 1;
            }

            k = 0;
            count_k = CkJsonObjectW_SizeOfArray(json,L"payload.parts[i].parts[j].parts");
            while (k < count_k) {
                CkJsonObjectW_putK(json,k);
                partId = CkJsonObjectW_stringOf(json,L"payload.parts[i].parts[j].parts[k].partId");
                mimeType = CkJsonObjectW_stringOf(json,L"payload.parts[i].parts[j].parts[k].mimeType");
                filename = CkJsonObjectW_stringOf(json,L"payload.parts[i].parts[j].parts[k].filename");
                bodySize = CkJsonObjectW_IntOf(json,L"payload.parts[i].parts[j].parts[k].body.size");
                bodyData = CkJsonObjectW_stringOf(json,L"payload.parts[i].parts[j].parts[k].body.data");

                json1 = CkJsonObjectW_ObjectOf(json,L"payload.parts[i].parts[j].parts[k]");
                i1 = 0;
                count_i1 = CkJsonObjectW_SizeOfArray(json1,L"headers");
                while (i1 < count_i1) {
                    CkJsonObjectW_putI(json1,i1);
                    name = CkJsonObjectW_stringOf(json1,L"headers[i].name");
                    value = CkJsonObjectW_stringOf(json1,L"headers[i].value");
                    i1 = i1 + 1;
                }

                CkJsonObjectW_Dispose(json1);
                k = k + 1;
            }

            j = j + 1;
        }

        i = i + 1;
    }

    wprintf(L"Example Completed.\n");


    CkRestW_Dispose(rest);
    CkOAuth2W_Dispose(oauth2);
    CkStringBuilderW_Dispose(sbJson);
    CkJsonObjectW_Dispose(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
}