Sample code for 30+ languages & platforms
PureBasic

Move a GMail Message to Trash

See more GMail REST API Examples

Moves a specific GMail email message to trash.

Chilkat PureBasic Downloads

PureBasic
IncludeFile "CkHttpResponse.pb"
IncludeFile "CkHttp.pb"

Procedure ChilkatExample()

    success.i = 0

    ; This example requires the Chilkat API to have been previously unlocked.
    ; See Global Unlock Sample for sample code.

    http.i = CkHttp::ckCreate()
    If http.i = 0
        Debug "Failed to create object."
        ProcedureReturn
    EndIf

    CkHttp::setCkAuthToken(http, "GMAIL-ACCESS-TOKEN")

    ; The id of the GMail message to move to Trash.
    id.s = "16678c485e7f0a0c"
    userId.s = "me"

    CkHttp::ckSetUrlVar(http,"userId","me")
    CkHttp::ckSetUrlVar(http,"id",id)

    ; Move to trash by POSTing w/ an empty request body.
    url.s = "https://www.googleapis.com/gmail/v1/users/{$userId}/messages/{$id}/trash"
    resp.i = CkHttpResponse::ckCreate()
    If resp.i = 0
        Debug "Failed to create object."
        ProcedureReturn
    EndIf

    success = CkHttp::ckHttpStr(http,"POST",url,"","","",resp)
    If success = 0
        Debug CkHttp::ckLastErrorText(http)
        CkHttp::ckDispose(http)
        CkHttpResponse::ckDispose(resp)
        ProcedureReturn
    EndIf

    Debug "status = " + Str(CkHttpResponse::ckStatusCode(resp))

    ; A 200 response status indicate success.
    If CkHttpResponse::ckStatusCode(resp) <> 200
        Debug CkHttpResponse::ckBodyStr(resp)
        Debug "Failed."
        CkHttp::ckDispose(http)
        CkHttpResponse::ckDispose(resp)
        ProcedureReturn
    EndIf

    ; A successful repsonse contains JSON that looks like this:

    ; {
    ;  "id": "16678c485e7f0a0c",
    ;  "threadId": "16678c485e7f0a0c",
    ;  "labelIds": [
    ;   "TRASH",
    ;   "CATEGORY_SOCIAL"
    ;  ]
    ; }

    Debug "response body:"
    Debug CkHttpResponse::ckBodyStr(resp)

    Debug "Message moved to trash!"


    CkHttp::ckDispose(http)
    CkHttpResponse::ckDispose(resp)


    ProcedureReturn
EndProcedure