Unicode C++ GMail: Parse GMail REST API Error Response

Back to Index

Gets the current user's Gmail profile, but shows an error response and how to parse it..

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


#include <CkRestW.h>
#include <CkOAuth2W.h>
#include <CkStringBuilderW.h>
#include <CkJsonObjectW.h>

void ChilkatSample(void)
    {
    CkRestW rest;
    bool success;

    //   Provide a previously obtained OAuth2 access token.
    CkOAuth2W oauth2;
    oauth2.put_AccessToken(L"OAUTH2_ACCESS_TOKEN");
    rest.SetAuthOAuth2(oauth2);

    success = rest.Connect(L"www.googleapis.com",443,true,true);
    if (success != true) {
        wprintf(L"%s\n",rest.lastErrorText());
        return;
    }

    rest.AddPathParam(L"userId",L"matt@chilkat.io");

    CkStringBuilderW sbJson;
    success = rest.FullRequestNoBodySb(L"GET",L"/gmail/v1/users/userId/profile",sbJson);
    if (success != true) {
        wprintf(L"%s\n",rest.lastErrorText());
        return;
    }

    if (rest.get_ResponseStatusCode() != 200) {
        wprintf(L"Received error response code: %d\n",rest.get_ResponseStatusCode());
        wprintf(L"Response body:\n");
        wprintf(L"%s\n",sbJson.getAsString());
        return;
    }

    CkJsonObjectW json;
    json.LoadSb(sbJson);

    //  The following code parses the JSON response.
    //  A sample JSON response is shown below the sample code.
    int errorCode;
    const wchar_t *errorMessage = 0;
    int i;
    int count_i;
    const wchar_t *domain = 0;
    const wchar_t *reason = 0;
    const wchar_t *message = 0;
    const wchar_t *extendedHelp = 0;

    errorCode = json.IntOf(L"error.code");
    errorMessage = json.stringOf(L"error.message");
    i = 0;
    count_i = json.SizeOfArray(L"error.errors");
    while (i < count_i) {
        json.put_I(i);
        domain = json.stringOf(L"error.errors[i].domain");
        reason = json.stringOf(L"error.errors[i].reason");
        message = json.stringOf(L"error.errors[i].message");
        extendedHelp = json.stringOf(L"error.errors[i].extendedHelp");
        i = i + 1;
    }

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

Sample JSON Response Body

{
 "error": {
  "errors": [
   {
    "domain": "usageLimits",
    "reason": "accessNotConfigured",
    "message": "Access Not Configured. Gmail API has not been used in project 377697329735 before or it is disabled. Enable it by visiting https://console.developers.google.com/apis/api/gmail.googleapis.com/overview?project=377697329735 then retry. If you enabled this API recently, wait a few minutes for the action to propagate to our systems and retry.",
    "extendedHelp": "https://console.developers.google.com/apis/api/gmail.googleapis.com/overview?project=377697329735"
   }
  ],
  "code": 403,
  "message": "Access Not Configured. Gmail API has not been used in project 377697329735 before or it is disabled. Enable it by visiting https://console.developers.google.com/apis/api/gmail.googleapis.com/overview?project=377697329735 then retry. If you enabled this API recently, wait a few minutes for the action to propagate to our systems and retry."
 }
}