Tcl Requires Chilkat v11.0.0+
Tcl
Find a Gmail label ID by name
See more GMail REST API Examples
Look up a label using its exact display name, including any parent/child path. Labels and their IDs belong to the authenticated mailbox. Use Gmail’s returned ID when applying a user label to messages.
Authorize the application with https://www.googleapis.com/auth/gmail.labels. The example reads the access token from gmail-token.json in a private working directory. Authorize a desktop application.
Chilkat Tcl Downloads
load ./chilkat.dll
set success 0
# Assumes Chilkat is unlocked. See Global Unlock Sample.
set token [new_CkJsonObject]
set success [CkJsonObject_LoadFile $token "gmail-token.json"]
if {$success != 1} then {
puts "Could not read gmail-token.json from the private working directory."
delete_CkJsonObject $token
exit
}
if {expr [[CkJsonObject_JsonTypeOf $token "access_token"] != 1] || [[CkJsonObject_StringOfEquals $token "access_token" "" 1] == 1]} then {
puts "An OAuth access token is required."
delete_CkJsonObject $token
exit
}
set http [new_CkHttp]
CkHttp_put_RequireSslCertVerify $http 1
CkHttp_put_AuthToken $http [CkJsonObject_stringOf $token "access_token"]
set resp [new_CkHttpResponse]
set reply [new_CkJsonObject]
set success [CkHttp_HttpNoBody $http "GET" "https://gmail.googleapis.com/gmail/v1/users/me/labels" $resp]
if {[CkHttpResponse_get_StatusCode $resp] == 0} then {
puts "Gmail did not return an HTTP response."
delete_CkJsonObject $token
delete_CkHttp $http
delete_CkHttpResponse $resp
delete_CkJsonObject $reply
exit
}
if {expr [$success != 1] || [expr [[CkHttpResponse_get_StatusCode $resp] < 200] || [[CkHttpResponse_get_StatusCode $resp] >= 300]]} then {
puts "Gmail HTTP [CkHttpResponse_get_StatusCode $resp]"
set success [CkJsonObject_Load $reply [CkHttpResponse_bodyStr $resp]]
if {$success == 1} then {
puts [CkJsonObject_stringOf $reply error.message]
}
delete_CkJsonObject $token
delete_CkHttp $http
delete_CkHttpResponse $resp
delete_CkJsonObject $reply
exit
}
set success [CkJsonObject_Load $reply [CkHttpResponse_bodyStr $resp]]
if {$success != 1} then {
puts "Expected a Gmail JSON response."
delete_CkJsonObject $token
delete_CkHttp $http
delete_CkHttpResponse $resp
delete_CkJsonObject $reply
exit
}
if {[CkJsonObject_JsonTypeOf $reply "labels"] != 4} then {
puts "Expected a list of labels."
delete_CkJsonObject $token
delete_CkHttp $http
delete_CkHttpResponse $resp
delete_CkJsonObject $reply
exit
}
set label [new_CkJsonObject]
set success [CkJsonObject_FindRecord2 $reply "labels" "name" "To review" 1 $label]
if {$success != 1} then {
puts "No label with that exact name. Check the name or create the label first."
delete_CkJsonObject $token
delete_CkHttp $http
delete_CkHttpResponse $resp
delete_CkJsonObject $reply
delete_CkJsonObject $label
exit
}
puts "Label ID: [CkJsonObject_stringOf $label id]"
delete_CkJsonObject $token
delete_CkHttp $http
delete_CkHttpResponse $resp
delete_CkJsonObject $reply
delete_CkJsonObject $label