Sample code for 30+ languages & platforms
Tcl

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 Tcl Downloads

Tcl

load ./chilkat.dll

set success 0

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

set http [new_CkHttp]

CkHttp_put_AuthToken $http "GMAIL-ACCESS-TOKEN"

set userId "me"
CkHttp_SetUrlVar $http "userId" $userId

set query "subject:questions"
CkHttp_SetUrlVar $http "query" $query

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

set sb [new_CkStringBuilder]

set success [CkHttp_QuickGetSb $http $url $sb]
if {$success == 0} then {
    puts [CkHttp_lastErrorText $http]
    delete_CkHttp $http
    delete_CkStringBuilder $sb
    exit
}

set json [new_CkJsonObject]

CkJsonObject_LoadSb $json $sb
CkJsonObject_put_EmitCompact $json 0
puts [CkJsonObject_emit $json]

if {[CkHttp_get_LastStatus $http] != 200} then {
    puts "Failed."
    delete_CkHttp $http
    delete_CkStringBuilder $sb
    delete_CkJsonObject $json
    exit
}

# 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.

set json2 [new_CkJsonObject]

set i 0
set numMessages [CkJsonObject_SizeOfArray $json "messages"]
while {$i < $numMessages} {
    CkJsonObject_put_I $json $i
    set id [CkJsonObject_stringOf $json "messages[i].id"]
    CkJsonObject_put_I $json2 $i
    CkJsonObject_UpdateString $json2 "ids[i]" $id
    set i [expr $i + 1]
}

# 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"
set labelId "Label_43"
CkJsonObject_UpdateString $json2 "addLabelIds[0]" $labelId
CkJsonObject_UpdateNewArray $json2 "removeLabelIds"

CkJsonObject_put_EmitCompact $json2 0
puts [CkJsonObject_emit $json2]

# Send the batchModify
set url "https://www.googleapis.com/gmail/v1/users/{$userId}/messages/batchModify"
set resp [new_CkHttpResponse]

set success [CkHttp_HttpJson $http "POST" $url $json2 "application/json" $resp]
if {$success == 0} then {
    puts [CkHttp_lastErrorText $http]
    delete_CkHttp $http
    delete_CkStringBuilder $sb
    delete_CkJsonObject $json
    delete_CkJsonObject $json2
    delete_CkHttpResponse $resp
    exit
}

puts "status = [CkHttpResponse_get_StatusCode $resp]"

# A 204 response status indicate success.
if {[CkHttpResponse_get_StatusCode $resp] != 204} then {
    puts [CkHttpResponse_bodyStr $resp]
    puts "Failed."
    delete_CkHttp $http
    delete_CkStringBuilder $sb
    delete_CkJsonObject $json
    delete_CkJsonObject $json2
    delete_CkHttpResponse $resp
    exit
}

# The 204 response has an empty response body..

puts "BatchModify success!"

delete_CkHttp $http
delete_CkStringBuilder $sb
delete_CkJsonObject $json
delete_CkJsonObject $json2
delete_CkHttpResponse $resp