Xbase++
Xbase++
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 Xbase++ Downloads
LOCAL nSuccess
LOCAL oHttp
LOCAL cUserId
LOCAL cQuery
LOCAL cUrl
LOCAL oSb
LOCAL oJson
LOCAL oJson2
LOCAL i
LOCAL nNumMessages
LOCAL cId
LOCAL cLabelId
LOCAL oResp
nSuccess := 0
// This example requires the Chilkat API to have been previously unlocked.
// See Global Unlock Sample for sample code.
oHttp := CreateObject("Chilkat.Http")
oHttp:AuthToken := "GMAIL-ACCESS-TOKEN"
cUserId := "me"
oHttp:SetUrlVar("userId", cUserId)
cQuery := "subject:questions"
oHttp:SetUrlVar("query", cQuery)
cUrl := "https://www.googleapis.com/gmail/v1/users/{$userId}/messages?q={$query}"
oSb := CreateObject("Chilkat.StringBuilder")
nSuccess := oHttp:QuickGetSb(cUrl, oSb)
IF (nSuccess == 0)
? oHttp:LastErrorText
oHttp:destroy()
oSb:destroy()
RETURN
ENDIF
oJson := CreateObject("Chilkat.JsonObject")
oJson:LoadSb(oSb)
oJson:EmitCompact := 0
? oJson:Emit()
IF (oHttp:LastStatus != 200)
? "Failed."
oHttp:destroy()
oSb:destroy()
oJson:destroy()
RETURN
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.
oJson2 := CreateObject("Chilkat.JsonObject")
i := 0
nNumMessages := oJson:SizeOfArray("messages")
DO WHILE (i < nNumMessages)
oJson:I := i
cId := oJson:StringOf("messages[i].id")
oJson2:I := i
oJson2:UpdateString("ids[i]", cId)
i := i + 1
ENDDO
// 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"
cLabelId := "Label_43"
oJson2:UpdateString("addLabelIds[0]", cLabelId)
oJson2:UpdateNewArray("removeLabelIds")
oJson2:EmitCompact := 0
? oJson2:Emit()
// Send the batchModify
cUrl := "https://www.googleapis.com/gmail/v1/users/{$userId}/messages/batchModify"
oResp := CreateObject("Chilkat.HttpResponse")
nSuccess := oHttp:HttpJson("POST", cUrl, oJson2, "application/json", oResp)
IF (nSuccess == 0)
? oHttp:LastErrorText
oHttp:destroy()
oSb:destroy()
oJson:destroy()
oJson2:destroy()
oResp:destroy()
RETURN
ENDIF
? "status = " + Str(oResp:StatusCode)
// A 204 response status indicate success.
IF (oResp:StatusCode != 204)
? oResp:BodyStr
? "Failed."
oHttp:destroy()
oSb:destroy()
oJson:destroy()
oJson2:destroy()
oResp:destroy()
RETURN
ENDIF
// The 204 response has an empty response body..
? "BatchModify success!"
oHttp:destroy()
oSb:destroy()
oJson:destroy()
oJson2:destroy()
oResp:destroy()