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


#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: @"messageId" value: @"15fc237e79da4174"];

[rest AddQueryParam: @"format" value: @"minimal"];

CkoStringBuilder *sbJson = [[CkoStringBuilder alloc] init];
success = [rest FullRequestNoBodySb: @"GET" uriPath: @"/gmail/v1/users/me/messages/messageId" 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.
NSString *id = 0;
NSString *threadId = 0;
NSString *snippet = 0;
NSString *historyId = 0;
NSString *internalDate = 0;
int sizeEstimate;
int i;
int count_i;
NSString *strVal = 0;

id = [json StringOf: @"id"];
threadId = [json StringOf: @"threadId"];
snippet = [json StringOf: @"snippet"];
historyId = [json StringOf: @"historyId"];
internalDate = [json StringOf: @"internalDate"];
sizeEstimate = [[json IntOf: @"sizeEstimate"] intValue];
i = 0;
count_i = [[json SizeOfArray: @"labelIds"] intValue];
while (i < count_i) {
    json.I = [NSNumber numberWithInt: i];
    strVal = [json StringOf: @"labelIds[i]"];
    i = i + 1;
}

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

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
}