Sample code for 30+ languages & platforms
Objective-C Requires Chilkat v11.0.0+

Find a Gmail label ID by name

See more GMail REST API Examples

Look up a label using its exact display name, including any parent/child path. Labels and their IDs belong to the authenticated mailbox. Use Gmail’s returned ID when applying a user label to messages.

Authorize the application with https://www.googleapis.com/auth/gmail.labels. The example reads the access token from gmail-token.json in a private working directory. Authorize a desktop application.

Google documentation

Chilkat Objective-C Downloads

Objective-C
#import <CkoJsonObject.h>
#import <CkoHttp.h>
#import <CkoHttpResponse.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/labels" 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;
}

if ([[reply JsonTypeOf: @"labels"] intValue] != 4) {
    NSLog(@"%@",@"Expected a list of labels.");
    return;
}

CkoJsonObject *label = [[CkoJsonObject alloc] init];
success = [reply FindRecord2: @"labels" relPath: @"name" value: @"To review" caseSensitive: YES jsonObj: label];
if (success != YES) {
    NSLog(@"%@",@"No label with that exact name. Check the name or create the label first.");
    return;
}

NSLog(@"%@%@",@"Label ID: ",[label StringOf: @"id"]);