Objective-C Requires Chilkat v11.0.0+
Objective-C
Read selected headers for a page of Gmail messages
See more GMail REST API Examples
List up to five messages and fetch only From, Subject, and Date for each one. Request metadataHeaders once per header name. Read the returned headers by name because order and presence vary. This header preview leaves the MIME body on Gmail.
Authorize the application with https://www.googleapis.com/auth/gmail.metadata. The example reads the access token from gmail-token.json in a private working directory. Authorize a desktop application.
Chilkat Objective-C Downloads
#import <CkoJsonObject.h>
#import <CkoHttp.h>
#import <CkoHttpResponse.h>
#import <CkoStringTable.h>
#import <CkoStringBuilder.h>
BOOL success = NO;
// Assumes Chilkat is unlocked. See Global Unlock Sample.
CkoJsonObject *token = [[CkoJsonObject alloc] init];
success = [token LoadFile: @"gmail-token.json"];
if (success != YES) {
NSLog(@"%@",@"Could not read gmail-token.json from the private working directory.");
return;
}
if (([[token JsonTypeOf: @"access_token"] intValue] != 1) || ([token StringOfEquals: @"access_token" value: @"" caseSensitive: YES] == YES)) {
NSLog(@"%@",@"An OAuth access token is required.");
return;
}
CkoHttp *http = [[CkoHttp alloc] init];
http.RequireSslCertVerify = YES;
http.AuthToken = [token StringOf: @"access_token"];
CkoHttpResponse *resp = [[CkoHttpResponse alloc] init];
CkoJsonObject *reply = [[CkoJsonObject alloc] init];
success = [http HttpNoBody: @"GET" url: @"https://gmail.googleapis.com/gmail/v1/users/me/messages?maxResults=5" response: resp];
if ([resp.StatusCode intValue] == 0) {
NSLog(@"%@",@"Gmail did not return an HTTP response.");
return;
}
if ((success != YES) || ([resp.StatusCode intValue] < 200) || ([resp.StatusCode intValue] >= 300)) {
NSLog(@"%@%d",@"Gmail HTTP ",[resp.StatusCode intValue]);
success = [reply Load: resp.BodyStr];
if (success == YES) {
NSLog(@"%@",[reply StringOf: @"error.message"]);
}
return;
}
success = [reply Load: resp.BodyStr];
if (success != YES) {
NSLog(@"%@",@"Expected a Gmail JSON response.");
return;
}
CkoStringTable *ids = [[CkoStringTable alloc] init];
int count = [[reply SizeOfArray: @"messages"] intValue];
int i = 0;
while ((i < count)) {
reply.I = [NSNumber numberWithInt: i];
success = [ids Append: [reply StringOf: @"messages[i].id"]];
if (success != YES) {
NSLog(@"%@",@"Could not retain a message ID.");
return;
}
i = i + 1;
}
CkoStringBuilder *url = [[CkoStringBuilder alloc] init];
i = 0;
while ((i < [ids.Count intValue])) {
[url SetString: @"https://gmail.googleapis.com/gmail/v1/users/me/messages/"];
[url Append: [ids StringAt: [NSNumber numberWithInt: i]]];
[url Append: @"?format=metadata&metadataHeaders=From&metadataHeaders=Subject&metadataHeaders=Date"];
success = [http HttpNoBody: @"GET" url: [url GetAsString] response: resp];
if ([resp.StatusCode intValue] == 0) {
NSLog(@"%@",@"Gmail did not return an HTTP response.");
return;
}
if ((success != YES) || ([resp.StatusCode intValue] < 200) || ([resp.StatusCode intValue] >= 300)) {
NSLog(@"%@%d",@"Gmail HTTP ",[resp.StatusCode intValue]);
success = [reply Load: resp.BodyStr];
if (success == YES) {
NSLog(@"%@",[reply StringOf: @"error.message"]);
}
return;
}
success = [reply Load: resp.BodyStr];
if (success != YES) {
NSLog(@"%@",@"Expected a Gmail JSON response.");
return;
}
NSLog(@"%@%@",@"Message: ",[ids StringAt: [NSNumber numberWithInt: i]]);
int h = 0;
while ((h < [[reply SizeOfArray: @"payload.headers"] intValue])) {
reply.I = [NSNumber numberWithInt: h];
NSLog(@"%@%@%@",[reply StringOf: @"payload.headers[i].name"],@": ",[reply StringOf: @"payload.headers[i].value"]);
h = h + 1;
}
i = i + 1;
}
NSLog(@"%@%d",@"Messages inspected: ",[ids.Count intValue]);