Sample code for 30+ languages & platforms
Swift Requires Chilkat v11.0.0+

List Gmail message IDs across all pages

See more GMail REST API Examples

List message and thread IDs for the authenticated user. Continue with nextPageToken until it is absent. A message ID is a Gmail identifier, distinct from the RFC Message-ID header. Use it in the reading examples. Spam and Trash are excluded by default.

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

Google documentation

Chilkat Swift Downloads

Swift

func chilkatTest() {
    var success: Bool = false

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

    let token = CkoJsonObject()!
    success = token.loadFile(path: "gmail-token.json")
    if success != true {
        print("Could not read gmail-token.json from the private working directory.")
        return
    }

    if (token.jsonType(of: "access_token").intValue != 1) || (token.string(ofEquals: "access_token", value: "", caseSensitive: true) == true) {
        print("An OAuth access token is required.")
        return
    }

    let http = CkoHttp()!
    http.requireSslCertVerify = true
    http.authToken = token.string(of: "access_token")
    let resp = CkoHttpResponse()!
    let reply = CkoJsonObject()!
    var total: Int = 0
    let baseUrl = CkoStringBuilder()!
    baseUrl.append(value: "https://gmail.googleapis.com/gmail/v1/users/me/messages?maxResults=100")
    let url = CkoStringBuilder()!
    let pageToken = CkoStringBuilder()!
    var more: Bool = true
    while (more == true) {
        url.setString(value: baseUrl.getAsString())
        if pageToken.length.intValue > 0 {
            url.append(value: "&pageToken=")
            url.append(value: http.urlEncode(str: pageToken.getAsString()))
        }

        success = http.httpNoBody(verb: "GET", url: url.getAsString(), response: resp)
        if resp.statusCode.intValue == 0 {
            print("Gmail did not return an HTTP response.")
            return
        }

        if (success != true) || (resp.statusCode.intValue < 200) || (resp.statusCode.intValue >= 300) {
            print("Gmail HTTP \(resp.statusCode.intValue)")
            success = reply.load(json: resp.bodyStr)
            if success == true {
                print("\(reply.string(of: "error.message")!)")
            }

            return
        }

        success = reply.load(json: resp.bodyStr)
        if success != true {
            print("Expected a Gmail JSON response.")
            return
        }

        //  Gmail can omit messages when the search or mailbox is empty.
        var count: Int = reply.size(ofArray: "messages").intValue
        var i: Int = 0
        while (i < count) {
            reply.i = i
            print("\(reply.string(of: "messages[i].id")!)  thread \(reply.string(of: "messages[i].threadId")!)")
            total = total + 1
            i = i + 1
        }

        more = false
        if reply.jsonType(of: "nextPageToken").intValue == 1 {
            pageToken.setString(value: reply.string(of: "nextPageToken"))
            more = pageToken.length.intValue > 0
        }

    }

    print("Messages listed: \(total)")

}