Lua Requires Chilkat v11.0.0+
Lua
Mark a Gmail message as read or archive it
See more GMail REST API Examples
Choose read or archive and supply a Gmail message ID. Marking a message as read removes UNREAD. Archiving removes INBOX while keeping the message available elsewhere in the mailbox. Each run performs the selected action on one message. Removing a label that is already absent is safe to repeat.
Authorize the application with https://www.googleapis.com/auth/gmail.modify. The example reads the access token from gmail-token.json in a private working directory. Authorize a desktop application.
Chilkat Lua Downloads
local chilkat = require("chilkat")
-- Assumes Chilkat is unlocked. See Global Unlock Sample.
local token = chilkat.JsonObject.new()
local success = token:load_file("gmail-token.json")
if not success then
print("Could not read gmail-token.json from the private working directory.")
return
end
if (token:json_type_of("access_token") ~= 1) or (token:string_of_equals("access_token", "", true)) then
print("An OAuth access token is required.")
return
end
local http = chilkat.Http.new()
http.require_ssl_cert_verify = true
http.auth_token = token:string_of("access_token")
local resp = chilkat.HttpResponse.new()
local reply = chilkat.JsonObject.new()
local action = "read"
local request = chilkat.JsonObject.new()
if action == "read" then
success = request:update_string("removeLabelIds[0]", "UNREAD")
if not success then
print(request.last_error_text)
return
end
else
if action ~= "archive" then
print("Choose read or archive.")
return
end
success = request:update_string("removeLabelIds[0]", "INBOX")
if not success then
print(request.last_error_text)
return
end
end
local url = chilkat.StringBuilder.new()
url:append("https://gmail.googleapis.com/gmail/v1/users/me/messages/")
url:append("GMAIL_MESSAGE_ID")
url:append("/modify")
success = http:http_json("POST", url:get_as_string(), request, "application/json", resp)
if resp.status_code == 0 then
print("Gmail did not return an HTTP response.")
return
end
if (not success) or ((resp.status_code < 200) or (resp.status_code >= 300)) then
print("Gmail HTTP " .. resp.status_code)
success = reply:load(resp.body_str)
if success then
print(reply:string_of("error.message"))
end
return
end
success = reply:load(resp.body_str)
if not success then
print("Expected a Gmail JSON response.")
return
end
if (reply:json_type_of("id") ~= 1) or (reply:string_of_equals("id", "", true)) then
print("Expected id in the response.")
return
end
print("Completed " .. action .. " for message " .. reply:string_of("id"))