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

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.

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()!
    success = http.httpNoBody(verb: "GET", url: "https://gmail.googleapis.com/gmail/v1/users/me/messages?maxResults=5", 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
    }

    let ids = CkoStringTable()!
    var count: Int = reply.size(ofArray: "messages").intValue
    var i: Int = 0
    while (i < count) {
        reply.i = i
        success = ids.append(value: reply.string(of: "messages[i].id"))
        if success != true {
            print("Could not retain a message ID.")
            return
        }

        i = i + 1
    }

    let url = CkoStringBuilder()!
    i = 0
    while (i < ids.count.intValue) {
        url.setString(value: "https://gmail.googleapis.com/gmail/v1/users/me/messages/")
        url.append(value: ids.string(at: i))
        url.append(value: "?format=metadata&metadataHeaders=From&metadataHeaders=Subject&metadataHeaders=Date")
        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
        }

        print("Message: \(ids.string(at: i)!)")
        var h: Int = 0
        while (h < reply.size(ofArray: "payload.headers").intValue) {
            reply.i = h
            print("\(reply.string(of: "payload.headers[i].name")!): \(reply.string(of: "payload.headers[i].value")!)")
            h = h + 1
        }

        i = i + 1
    }

    print("Messages inspected: \(ids.count.intValue)")

}