Unicode C GMail: Get a specific message (format="minimal")

Back to Index

Gets a specific message using format=minimal.

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;
    int sizeEstimate;
    int i;
    int count_i;
    const wchar_t *strVal;

    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"15fc237e79da4174");

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

    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");
    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;
    }

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


    CkRestW_Dispose(rest);
    CkOAuth2W_Dispose(oauth2);
    CkStringBuilderW_Dispose(sbJson);
    CkJsonObjectW_Dispose(json);

    }

Sample JSON Response Body

{
  "id": "15fc237e79da4174",
  "threadId": "15fc237e79da4174",
  "labelIds": [
    "IMPORTANT",
    "SENT",
    "INBOX"
  ],
  "snippet": "This is a test email with 2 attachments.",
  "historyId": "1582",
  "internalDate": "1510791964000",
  "sizeEstimate": 11153
}