Swift Requires Chilkat v11.0.0+
Swift
Create a user label in Gmail
See more GMail REST API Examples
Create a label with a chosen display name. Gmail returns the ID used to apply the label to messages. If a user label with that name already exists, look up its ID instead of creating a duplicate. System label names are reserved.
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.
Chilkat Swift Downloads
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()!
let request = CkoJsonObject()!
request.updateString(jsonPath: "name", value: "To review")
request.updateString(jsonPath: "labelListVisibility", value: "labelShow")
request.updateString(jsonPath: "messageListVisibility", value: "show")
success = http.httpJson(verb: "POST", url: "https://gmail.googleapis.com/gmail/v1/users/me/labels", json: request, contentType: "application/json", 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
}
if (reply.jsonType(of: "id").intValue != 1) || (reply.string(ofEquals: "id", value: "", caseSensitive: true) == true) {
print("Expected id in the response.")
return
}
print("Created label: \(reply.string(of: "name")!)")
print("Label ID: \(reply.string(of: "id")!)")
}