Objective-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


#import <CkoRest.h>
#import <CkoOAuth2.h>
#import <CkoStringBuilder.h>
#import <CkoJsonObject.h>
#import <NSString.h>

CkoRest *rest = [[CkoRest alloc] init];
BOOL success;

//   Provide a previously obtained OAuth2 access token.
CkoOAuth2 *oauth2 = [[CkoOAuth2 alloc] init];
oauth2.AccessToken = @"OAUTH2_ACCESS_TOKEN";
[rest SetAuthOAuth2: oauth2];

success = [rest Connect: @"www.googleapis.com" port: [NSNumber numberWithInt: 443] tls: YES autoReconnect: YES];
if (success != YES) {
    NSLog(@"%@",rest.LastErrorText);
    return;
}

[rest AddPathParam: @"userId" value: @"matt@chilkat.io"];

CkoStringBuilder *sbJson = [[CkoStringBuilder alloc] init];
success = [rest FullRequestNoBodySb: @"GET" uriPath: @"/gmail/v1/users/userId/profile" sb: sbJson];
if (success != YES) {
    NSLog(@"%@",rest.LastErrorText);
    return;
}

if ([rest.ResponseStatusCode intValue] != 200) {
    NSLog(@"%@%d",@"Received error response code: ",[rest.ResponseStatusCode intValue]);
    NSLog(@"%@",@"Response body:");
    NSLog(@"%@",[sbJson GetAsString]);
    return;
}

CkoJsonObject *json = [[CkoJsonObject alloc] init];
[json LoadSb: sbJson];

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

errorCode = [[json IntOf: @"error.code"] intValue];
errorMessage = [json StringOf: @"error.message"];
i = 0;
count_i = [[json SizeOfArray: @"error.errors"] intValue];
while (i < count_i) {
    json.I = [NSNumber numberWithInt: i];
    domain = [json StringOf: @"error.errors[i].domain"];
    reason = [json StringOf: @"error.errors[i].reason"];
    message = [json StringOf: @"error.errors[i].message"];
    extendedHelp = [json StringOf: @"error.errors[i].extendedHelp"];
    i = i + 1;
}

NSLog(@"%@",@"Example Completed.");

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