Sample code for 30+ languages & platforms
PureBasic

BatchModify - Add a Label to each Message in Search Results

See more GMail REST API Examples

Searchs GMail for messages meeting a criteria and adds a label to each message found.

Chilkat PureBasic Downloads

PureBasic
IncludeFile "CkHttpResponse.pb"
IncludeFile "CkHttp.pb"
IncludeFile "CkStringBuilder.pb"
IncludeFile "CkJsonObject.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")

    userId.s = "me"
    CkHttp::ckSetUrlVar(http,"userId",userId)

    query.s = "subject:questions"
    CkHttp::ckSetUrlVar(http,"query",query)

    url.s = "https://www.googleapis.com/gmail/v1/users/{$userId}/messages?q={$query}"

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

    success = CkHttp::ckQuickGetSb(http,url,sb)
    If success = 0
        Debug CkHttp::ckLastErrorText(http)
        CkHttp::ckDispose(http)
        CkStringBuilder::ckDispose(sb)
        ProcedureReturn
    EndIf

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

    CkJsonObject::ckLoadSb(json,sb)
    CkJsonObject::setCkEmitCompact(json, 0)
    Debug CkJsonObject::ckEmit(json)

    If CkHttp::ckLastStatus(http) <> 200
        Debug "Failed."
        CkHttp::ckDispose(http)
        CkStringBuilder::ckDispose(sb)
        CkJsonObject::ckDispose(json)
        ProcedureReturn
    EndIf

    ; If successful, the received JSON looks like this:
    ; {
    ;   "messages": [
    ;     {
    ;       "id": "166f583051d36144",
    ;       "threadId": "166f583051d36144"
    ;     },
    ;     {
    ;       "id": "166f5815e1f36144",
    ;       "threadId": "166f5815e1f36144"
    ;     },
    ; 	...
    ;     {
    ;       "id": "166f580639e36144",
    ;       "threadId": "166f580639e36144"
    ;     },
    ;     {
    ;       "id": "15dbc2e28ec789c6",
    ;       "threadId": "15dbc2e28ec789c6"
    ;     }
    ;   ],
    ;   "nextPageToken": "13434766102274844688",
    ;   "resultSizeEstimate": 103
    ; }
    ; 

    ; Next, we'll be sending an HTTP POST to add the label "questions" to each message in the
    ; search results.  The JSON to be sent for the batchModify is this:

    ; 	{
    ; 	  "ids": [
    ; 	    string
    ; 	  ],
    ; 	  "addLabelIds": [
    ; 	    string
    ; 	  ],
    ; 	  "removeLabelIds": [
    ; 	    string
    ; 	  ]
    ; 	}

    ; We'll omit "removeLabelIds" because we're not removing any labels.
    ; We are parsing the JSON search results, and at the same time building the batchModify JSON.

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

    i.i = 0
    numMessages.i = CkJsonObject::ckSizeOfArray(json,"messages")
    While (i < numMessages)
        CkJsonObject::setCkI(json, i)
        id.s = CkJsonObject::ckStringOf(json,"messages[i].id")
        CkJsonObject::setCkI(json2, i)
        CkJsonObject::ckUpdateString(json2,"ids[i]",id)
        i = i + 1
    Wend

    ; We need the id of the label (not the name).
    ; I know the name of the label is "questions", but I need to know the id.
    ; See this example: Get Label Id by Name
    ; The id of my label named "questions" is "Label_43"
    labelId.s = "Label_43"
    CkJsonObject::ckUpdateString(json2,"addLabelIds[0]",labelId)
    CkJsonObject::ckUpdateNewArray(json2,"removeLabelIds")

    CkJsonObject::setCkEmitCompact(json2, 0)
    Debug CkJsonObject::ckEmit(json2)

    ; Send the batchModify
    url = "https://www.googleapis.com/gmail/v1/users/{$userId}/messages/batchModify"
    resp.i = CkHttpResponse::ckCreate()
    If resp.i = 0
        Debug "Failed to create object."
        ProcedureReturn
    EndIf

    success = CkHttp::ckHttpJson(http,"POST",url,json2,"application/json",resp)
    If success = 0
        Debug CkHttp::ckLastErrorText(http)
        CkHttp::ckDispose(http)
        CkStringBuilder::ckDispose(sb)
        CkJsonObject::ckDispose(json)
        CkJsonObject::ckDispose(json2)
        CkHttpResponse::ckDispose(resp)
        ProcedureReturn
    EndIf

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

    ; A 204 response status indicate success.
    If CkHttpResponse::ckStatusCode(resp) <> 204
        Debug CkHttpResponse::ckBodyStr(resp)
        Debug "Failed."
        CkHttp::ckDispose(http)
        CkStringBuilder::ckDispose(sb)
        CkJsonObject::ckDispose(json)
        CkJsonObject::ckDispose(json2)
        CkHttpResponse::ckDispose(resp)
        ProcedureReturn
    EndIf

    ; The 204 response has an empty response body..

    Debug "BatchModify success!"


    CkHttp::ckDispose(http)
    CkStringBuilder::ckDispose(sb)
    CkJsonObject::ckDispose(json)
    CkJsonObject::ckDispose(json2)
    CkHttpResponse::ckDispose(resp)


    ProcedureReturn
EndProcedure