Tcl GMail: Get User's GMail Profile

Back to Index

Gets the current user's Gmail profile.

Documentation: https://developers.google.com/gmail/api/v1/reference/users/getProfile



load ./chilkat.dll

set rest [new_CkRest]

#   Provide a previously obtained OAuth2 access token.
set oauth2 [new_CkOAuth2]

CkOAuth2_put_AccessToken $oauth2 "OAUTH2_ACCESS_TOKEN"
CkRest_SetAuthOAuth2 $rest $oauth2

set success [CkRest_Connect $rest "www.googleapis.com" 443 1 1]
if {[expr $success != 1]} then {
    puts [CkRest_lastErrorText $rest]
    delete_CkRest $rest
    delete_CkOAuth2 $oauth2
    exit
}

CkRest_AddPathParam $rest "userId" "matt@chilkat.io"

set sbJson [new_CkStringBuilder]

set success [CkRest_FullRequestNoBodySb $rest "GET" "/gmail/v1/users/userId/profile" $sbJson]
if {[expr $success != 1]} then {
    puts [CkRest_lastErrorText $rest]
    delete_CkRest $rest
    delete_CkOAuth2 $oauth2
    delete_CkStringBuilder $sbJson
    exit
}

if {[expr [CkRest_ResponseStatusCode $rest] != 200]} then {
    puts "Received error response code: [CkRest_ResponseStatusCode $rest]"
    puts "Response body:"
    puts [CkStringBuilder_getAsString $sbJson]
    delete_CkRest $rest
    delete_CkOAuth2 $oauth2
    delete_CkStringBuilder $sbJson
    exit
}

set json [new_CkJsonObject]

CkJsonObject_LoadSb $json $sbJson

#  The following code parses the JSON response.
#  A sample JSON response is shown below the sample code.

set emailAddress [CkJsonObject_stringOf $json "emailAddress"]
set messagesTotal [CkJsonObject_IntOf $json "messagesTotal"]
set threadsTotal [CkJsonObject_IntOf $json "threadsTotal"]
set historyId [CkJsonObject_stringOf $json "historyId"]

puts "Example Completed."

delete_CkRest $rest
delete_CkOAuth2 $oauth2
delete_CkStringBuilder $sbJson
delete_CkJsonObject $json

Sample JSON Response Body

{
  "emailAddress": "matt@chilkat.io",
  "messagesTotal": 3,
  "threadsTotal": 3,
  "historyId": "1257"
}