Tcl Requires Chilkat v11.0.0+
Tcl
Create a user label in Gmail
See more GMail REST API Examples
Create a label with a chosen display name. Gmail returns the ID used to apply the label to messages. If a user label with that name already exists, look up its ID instead of creating a duplicate. System label names are reserved.
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 request [new_CkJsonObject]
CkJsonObject_UpdateString $request "name" "To review"
CkJsonObject_UpdateString $request "labelListVisibility" "labelShow"
CkJsonObject_UpdateString $request "messageListVisibility" "show"
set success [CkHttp_HttpJson $http "POST" "https://gmail.googleapis.com/gmail/v1/users/me/labels" $request "application/json" $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
delete_CkJsonObject $request
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
delete_CkJsonObject $request
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
delete_CkJsonObject $request
exit
}
if {expr [[CkJsonObject_JsonTypeOf $reply "id"] != 1] || [[CkJsonObject_StringOfEquals $reply "id" "" 1] == 1]} then {
puts "Expected id in the response."
delete_CkJsonObject $token
delete_CkHttp $http
delete_CkHttpResponse $resp
delete_CkJsonObject $reply
delete_CkJsonObject $request
exit
}
puts "Created label: [CkJsonObject_stringOf $reply name]"
puts "Label ID: [CkJsonObject_stringOf $reply id]"
delete_CkJsonObject $token
delete_CkHttp $http
delete_CkHttpResponse $resp
delete_CkJsonObject $reply
delete_CkJsonObject $request