Tcl Requires Chilkat v11.0.0+
Tcl
List Gmail labels and their IDs
See more GMail REST API Examples
Read the mailbox label catalog and display each label’s type, ID, and name. System labels include INBOX and TRASH. Use the returned ID in message modification requests. Fetch an individual label if you also need its message or thread counts.
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 i 0
while {$i < [CkJsonObject_SizeOfArray $reply "labels"]} {
CkJsonObject_put_I $reply $i
puts [CkJsonObject_stringOf $reply {labels[i].type}] [CkJsonObject_stringOf $reply {labels[i].id}] [CkJsonObject_stringOf $reply {labels[i].name}]
set i [expr $i + 1]
}
delete_CkJsonObject $token
delete_CkHttp $http
delete_CkHttpResponse $resp
delete_CkJsonObject $reply