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

Access a Google Workspace mailbox with a service account

See more GMail REST API Examples

A Google Workspace super administrator must authorize the service account client ID for https://www.googleapis.com/auth/gmail.metadata in Admin console domain-wide delegation. Enable the Gmail API in the Cloud project. Set SubEmailAddress to a Gmail-enabled user in that organization. The profile response identifies the mailbox represented by me.

Store the downloaded JSON service-account key in gmail-service-account.json in a private working directory. AuthGoogle signs a JWT with the key and exchanges it for an access token. Repeat that exchange when the token expires. For other operations, change Scope and grant that scope in the Admin console. This workflow is for organization-controlled applications. Personal Gmail accounts can use offline authorization with a refresh token.

Google documentation

Chilkat Objective-C Downloads

Objective-C
#import <CkoJsonObject.h>
#import <CkoAuthGoogle.h>
#import <CkoSocket.h>
#import <CkoHttp.h>
#import <CkoHttpResponse.h>

BOOL success = NO;

//  Assumes Chilkat is unlocked. See Global Unlock Sample.

CkoJsonObject *key = [[CkoJsonObject alloc] init];
success = [key LoadFile: @"gmail-service-account.json"];
if (success != YES) {
    NSLog(@"%@",@"Could not load the service-account key from the private directory.");
    return;
}

CkoAuthGoogle *auth = [[CkoAuthGoogle alloc] init];
auth.JsonKey = [key Emit];
auth.Scope = @"https://www.googleapis.com/auth/gmail.metadata";
auth.SubEmailAddress = @"user@example.com";
auth.ExpireNumSeconds = [NSNumber numberWithInt:3600];

//  Obtain a token for the named Workspace user. Keep the computer clock accurate.
CkoSocket *connection = [[CkoSocket alloc] init];
connection.RequireSslCertVerify = YES;
success = [connection Connect: @"www.googleapis.com" port: [NSNumber numberWithInt: 443] ssl: YES maxWaitMs: [NSNumber numberWithInt: 10000]];
if (success != YES) {
    NSLog(@"%@",@"Could not establish the Google token connection.");
    return;
}

success = [auth ObtainAccessToken: connection];
[connection Close: [NSNumber numberWithInt: 1000]];
if (success != YES) {
    NSLog(@"%@",@"Token exchange failed. Check the key, delegated scopes, user, and computer clock.");
    return;
}

CkoHttp *http = [[CkoHttp alloc] init];
http.RequireSslCertVerify = YES;
http.AuthToken = auth.AccessToken;
CkoHttpResponse *resp = [[CkoHttpResponse alloc] init];
CkoJsonObject *reply = [[CkoJsonObject alloc] init];
success = [http HttpNoBody: @"GET" url: @"https://gmail.googleapis.com/gmail/v1/users/me/profile" 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: @"emailAddress"] intValue] != 1) || ([reply StringOfEquals: @"emailAddress" value: @"" caseSensitive: YES] == YES)) {
    NSLog(@"%@",@"Expected emailAddress in the response.");
    return;
}

NSLog(@"%@%@",@"Authorized mailbox: ",[reply StringOf: @"emailAddress"]);
NSLog(@"%@%d",@"Messages: ",[[reply IntOf: @"messagesTotal"] intValue]);