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 <C_CkRest.h>
#include <C_CkOAuth2.h>
#include <C_CkStringBuilder.h>
#include <C_CkJsonObject.h>

void ChilkatSample(void)
    {
    HCkRest rest;
    BOOL success;
    HCkOAuth2 oauth2;
    HCkStringBuilder sbJson;
    HCkJsonObject json;
    int errorCode;
    const char *errorMessage;
    int i;
    int count_i;
    const char *domain;
    const char *reason;
    const char *message;
    const char *extendedHelp;

    rest = CkRest_Create();

    //   Provide a previously obtained OAuth2 access token.
    oauth2 = CkOAuth2_Create();
    CkOAuth2_putAccessToken(oauth2,"OAUTH2_ACCESS_TOKEN");
    CkRest_SetAuthOAuth2(rest,oauth2);

    success = CkRest_Connect(rest,"www.googleapis.com",443,TRUE,TRUE);
    if (success != TRUE) {
        printf("%s\n",CkRest_lastErrorText(rest));
        CkRest_Dispose(rest);
        CkOAuth2_Dispose(oauth2);
        return;
    }

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

    sbJson = CkStringBuilder_Create();
    success = CkRest_FullRequestNoBodySb(rest,"GET","/gmail/v1/users/userId/profile",sbJson);
    if (success != TRUE) {
        printf("%s\n",CkRest_lastErrorText(rest));
        CkRest_Dispose(rest);
        CkOAuth2_Dispose(oauth2);
        CkStringBuilder_Dispose(sbJson);
        return;
    }

    if (CkRest_getResponseStatusCode(rest) != 200) {
        printf("Received error response code: %d\n",CkRest_getResponseStatusCode(rest));
        printf("Response body:\n");
        printf("%s\n",CkStringBuilder_getAsString(sbJson));
        CkRest_Dispose(rest);
        CkOAuth2_Dispose(oauth2);
        CkStringBuilder_Dispose(sbJson);
        return;
    }

    json = CkJsonObject_Create();
    CkJsonObject_LoadSb(json,sbJson);

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

    errorCode = CkJsonObject_IntOf(json,"error.code");
    errorMessage = CkJsonObject_stringOf(json,"error.message");
    i = 0;
    count_i = CkJsonObject_SizeOfArray(json,"error.errors");
    while (i < count_i) {
        CkJsonObject_putI(json,i);
        domain = CkJsonObject_stringOf(json,"error.errors[i].domain");
        reason = CkJsonObject_stringOf(json,"error.errors[i].reason");
        message = CkJsonObject_stringOf(json,"error.errors[i].message");
        extendedHelp = CkJsonObject_stringOf(json,"error.errors[i].extendedHelp");
        i = i + 1;
    }

    printf("Example Completed.\n");


    CkRest_Dispose(rest);
    CkOAuth2_Dispose(oauth2);
    CkStringBuilder_Dispose(sbJson);
    CkJsonObject_Dispose(json);

    }

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."
 }
}