Sample code for 30+ languages & platforms
Tcl

Find a Label ID by Name

See more GMail REST API Examples

Lookup the ID of a GMail label by the label name.

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 url "https://www.googleapis.com/gmail/v1/users/{$userId}/labels"

# Get the list of GMail labels as JSON.
set sb [new_CkStringBuilder]

set success [CkHttp_QuickGetSb $http $url $sb]
if {$success != 1} 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
}

# The JSON returned looks like this:
# {
#   "labels": [
#     {
#       "id": "Label_5",
#       "name": "QA",
#       "messageListVisibility": "show",
#       "labelListVisibility": "labelShow",
#       "type": "user"
#     },
#     {
#       "id": "Label_21",
#       "name": "[Gmail]/testFolder",
#       "type": "user"
#     },
#     {
#       "id": "CATEGORY_PERSONAL",
#       "name": "CATEGORY_PERSONAL",
#       "type": "system"
#     },
# 	...

# The name of the label is generally known because it's what we visually see.
# The id is what we need to get.  Assuming the name is unique,
# find the JSON record having name=<desired name>
# For example...
# jRecord is a CkJsonObject
set jRecord [CkJsonObject_FindRecord $json "labels" "name" "QA" 0]
if {[CkJsonObject_get_LastMethodSuccess $json] == 1} then {
    puts "The id of QA is: [CkJsonObject_stringOf $jRecord id]"
    delete_CkJsonObject $jRecord

}

set jRecord [CkJsonObject_FindRecord $json "labels" "name" "[Gmail]/testFolder" 0]
if {[CkJsonObject_get_LastMethodSuccess $json] == 1} then {
    puts "{The id of [Gmail]/testFolder is: }[CkJsonObject_stringOf $jRecord id]"
    delete_CkJsonObject $jRecord

}

set jRecord [CkJsonObject_FindRecord $json "labels" "name" "questions" 0]
if {[CkJsonObject_get_LastMethodSuccess $json] == 1} then {
    puts "The id of questions is: [CkJsonObject_stringOf $jRecord id]"
    delete_CkJsonObject $jRecord

}

# Output:

# The id of QA is: Label_5
# The id of [Gmail]/testFolder is: Label_21
# The id of questions is: Label_43

delete_CkHttp $http
delete_CkStringBuilder $sb
delete_CkJsonObject $json