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

Move one Gmail message to Trash

See more GMail REST API Examples

Supply the Gmail message ID, then post an empty body to its trash endpoint. Gmail returns the updated message. While the message remains in Trash it can be restored with messages.untrash.

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.

Google documentation

Chilkat Xbase++ Downloads

Xbase++
LOCAL nSuccess
LOCAL oToken
LOCAL oHttp
LOCAL oResp
LOCAL oReply
LOCAL oUrl

nSuccess := 0

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

oToken := CreateObject("Chilkat.JsonObject")
nSuccess := oToken:LoadFile("gmail-token.json")
IF (nSuccess != 1)
    ? "Could not read gmail-token.json from the private working directory."
    oToken:destroy()
    RETURN
ENDIF

IF ((oToken:JsonTypeOf("access_token") != 1) .OR. (oToken:StringOfEquals("access_token", "", 1) == 1))
    ? "An OAuth access token is required."
    oToken:destroy()
    RETURN
ENDIF

oHttp := CreateObject("Chilkat.Http")
oHttp:RequireSslCertVerify := 1
oHttp:AuthToken := oToken:StringOf("access_token")
oResp := CreateObject("Chilkat.HttpResponse")
oReply := CreateObject("Chilkat.JsonObject")
oUrl := CreateObject("Chilkat.StringBuilder")
oUrl:Append("https://gmail.googleapis.com/gmail/v1/users/me/messages/")
oUrl:Append("GMAIL_MESSAGE_ID")
oUrl:Append("/trash")
nSuccess := oHttp:HttpNoBody("POST", oUrl:GetAsString(), oResp)
IF (oResp:StatusCode == 0)
    ? "Gmail did not return an HTTP response."
    oToken:destroy()
    oHttp:destroy()
    oResp:destroy()
    oReply:destroy()
    oUrl:destroy()
    RETURN
ENDIF

IF ((nSuccess != 1) .OR. (oResp:StatusCode < 200) .OR. (oResp:StatusCode >= 300))
    ? "Gmail HTTP " + Str(oResp:StatusCode)
    nSuccess := oReply:Load(oResp:BodyStr)
    IF (nSuccess == 1)
        ? oReply:StringOf("error.message")
    ENDIF

    oToken:destroy()
    oHttp:destroy()
    oResp:destroy()
    oReply:destroy()
    oUrl:destroy()
    RETURN
ENDIF

nSuccess := oReply:Load(oResp:BodyStr)
IF (nSuccess != 1)
    ? "Expected a Gmail JSON response."
    oToken:destroy()
    oHttp:destroy()
    oResp:destroy()
    oReply:destroy()
    oUrl:destroy()
    RETURN
ENDIF

IF ((oReply:JsonTypeOf("id") != 1) .OR. (oReply:StringOfEquals("id", "", 1) == 1))
    ? "Expected id in the response."
    oToken:destroy()
    oHttp:destroy()
    oResp:destroy()
    oReply:destroy()
    oUrl:destroy()
    RETURN
ENDIF

? "Moved to Trash: " + oReply:StringOf("id")

oToken:destroy()
oHttp:destroy()
oResp:destroy()
oReply:destroy()
oUrl:destroy()