Sample code for 30+ languages & platforms
Tcl

Google People API - List the User's Contacts

See more Google People Examples

Gets a list of people in the user's contacts.

Chilkat Tcl Downloads

Tcl

load ./chilkat.dll

set success 0

# This example assumes the Chilkat API to have been previously unlocked.
# See Global Unlock Sample for sample code.

# It is assumed we previously obtained an OAuth2 access token.
# This example loads the JSON access token file 

# originally obtained by this example: Get Google People API OAuth2 Access Token
# or refreshed by this example: Refresh Google People API OAuth2 Access Token

set jsonToken [new_CkJsonObject]

set success [CkJsonObject_LoadFile $jsonToken "qa_data/tokens/googlePeople.json"]
if {$success != 1} then {
    puts "Failed to load googleContacts.json"
    delete_CkJsonObject $jsonToken
    exit
}

set http [new_CkHttp]

CkHttp_put_AuthToken $http [CkJsonObject_stringOf $jsonToken "access_token"]

set sbResponseBody [new_CkStringBuilder]

set success [CkHttp_QuickGetSb $http "https://people.googleapis.com/v1/people/me/connections?personFields=names,addresses,emailAddresses" $sbResponseBody]
if {$success == 0} then {
    puts [CkHttp_lastErrorText $http]
    puts [CkStringBuilder_getAsString $sbResponseBody]
    delete_CkJsonObject $jsonToken
    delete_CkHttp $http
    delete_CkStringBuilder $sbResponseBody
    exit
}

# Sample JSON Response

# Use this online tool to generate parsing code from sample JSON: 
# Generate Parsing Code from JSON

# {
#   "connections": [
#     {
#       "resourceName": "people/c8871101035606120608",
#       "etag": "%EgkBAj0JQhBANy4aBAECBQciDERLSGdVTFNPbzNJPQ==",
#       "names": [
#         {
#           "metadata": {
#             "primary": true,
#             "source": {
#               "type": "CONTACT",
#               "id": "7b1c7b6409e718a0"
#             }
#           },
#           "displayName": "Chilkat Cloud",
#           "familyName": "Cloud",
#           "givenName": "Chilkat",
#           "displayNameLastFirst": "Cloud, Chilkat",
#           "unstructuredName": "Chilkat Cloud"
#         }
#       ],
#       "emailAddresses": [
#         {
#           "metadata": {
#             "primary": true,
#             "source": {
#               "type": "CONTACT",
#               "id": "7b1c7b6409e718a0"
#             }
#           },
#           "value": "support@***.com"
#         }
#       ]
#     },
#     {
#       "resourceName": "people/c7607335470312011517",
#       "etag": "%EgkBAj0JQhBANy4aBAECBQciDGZETUtHVTVMazI4PQ==",
#       "names": [
#         {
#           "metadata": {
#             "primary": true,
#             "source": {
#               "type": "CONTACT",
#               "id": "6992af4e0b2d36fd"
#             }
#           },
#           "displayName": "Matt Smith",
#           "familyName": "Smith",
#           "givenName": "Matt",
#           "displayNameLastFirst": "Smith, Matt",
#           "unstructuredName": "Matt Smith"
#         }
#       ],
#       "addresses": [
#         {
#           "metadata": {
#             "primary": true,
#             "source": {
#               "type": "CONTACT",
#               "id": "6992af4e0b2d36fd"
#             }
#           },
#           "formattedValue": "2222 E Foorest Ave\nWheaton, IL 60999\nUS",
#           "streetAddress": "2222 E Foorest Ave",
#           "city": "Wheaton",
#           "region": "IL",
#           "postalCode": "60999",
#           "country": "US",
#           "countryCode": "US"
#         }
#       ],
#       "emailAddresses": [
#         {
#           "metadata": {
#             "primary": true,
#             "source": {
#               "type": "CONTACT",
#               "id": "6992af4e0b2d36fd"
#             }
#           },
#           "value": "matt@***.com"
#         },
#         {
#           "metadata": {
#             "source": {
#               "type": "CONTACT",
#               "id": "6992af4e0b2d36fd"
#             }
#           },
#           "value": "admin@***.com"
#         }
#       ]
#     }
#   ],
#   "totalPeople": 2,
#   "totalItems": 2
# }

set json [new_CkJsonObject]

CkJsonObject_put_EmitCompact $json 0
CkJsonObject_Load $json [CkStringBuilder_getAsString $sbResponseBody]
puts [CkJsonObject_emit $json]

set totalPeople [CkJsonObject_IntOf $json "totalPeople"]
set totalItems [CkJsonObject_IntOf $json "totalItems"]
set i 0
set count_i [CkJsonObject_SizeOfArray $json "connections"]
while {$i < $count_i} {
    CkJsonObject_put_I $json $i
    set resourceName [CkJsonObject_stringOf $json "connections[i].resourceName"]
    set etag [CkJsonObject_stringOf $json "connections[i].etag"]
    set j 0
    set count_j [CkJsonObject_SizeOfArray $json "connections[i].names"]
    while {$j < $count_j} {
        CkJsonObject_put_J $json $j
        set metadataPrimary [CkJsonObject_BoolOf $json "connections[i].names[j].metadata.primary"]
        set metadataSourceType [CkJsonObject_stringOf $json "connections[i].names[j].metadata.source.type"]
        set metadataSourceId [CkJsonObject_stringOf $json "connections[i].names[j].metadata.source.id"]
        set displayName [CkJsonObject_stringOf $json "connections[i].names[j].displayName"]
        set familyName [CkJsonObject_stringOf $json "connections[i].names[j].familyName"]
        set givenName [CkJsonObject_stringOf $json "connections[i].names[j].givenName"]
        set displayNameLastFirst [CkJsonObject_stringOf $json "connections[i].names[j].displayNameLastFirst"]
        set unstructuredName [CkJsonObject_stringOf $json "connections[i].names[j].unstructuredName"]
        set j [expr $j + 1]
    }
    set j 0
    set count_j [CkJsonObject_SizeOfArray $json "connections[i].emailAddresses"]
    while {$j < $count_j} {
        CkJsonObject_put_J $json $j
        set metadataPrimary [CkJsonObject_BoolOf $json "connections[i].emailAddresses[j].metadata.primary"]
        set metadataSourceType [CkJsonObject_stringOf $json "connections[i].emailAddresses[j].metadata.source.type"]
        set metadataSourceId [CkJsonObject_stringOf $json "connections[i].emailAddresses[j].metadata.source.id"]
        set value [CkJsonObject_stringOf $json "connections[i].emailAddresses[j].value"]
        set j [expr $j + 1]
    }
    set j 0
    set count_j [CkJsonObject_SizeOfArray $json "connections[i].addresses"]
    while {$j < $count_j} {
        CkJsonObject_put_J $json $j
        set metadataPrimary [CkJsonObject_BoolOf $json "connections[i].addresses[j].metadata.primary"]
        set metadataSourceType [CkJsonObject_stringOf $json "connections[i].addresses[j].metadata.source.type"]
        set metadataSourceId [CkJsonObject_stringOf $json "connections[i].addresses[j].metadata.source.id"]
        set formattedValue [CkJsonObject_stringOf $json "connections[i].addresses[j].formattedValue"]
        set streetAddress [CkJsonObject_stringOf $json "connections[i].addresses[j].streetAddress"]
        set city [CkJsonObject_stringOf $json "connections[i].addresses[j].city"]
        set region [CkJsonObject_stringOf $json "connections[i].addresses[j].region"]
        set postalCode [CkJsonObject_stringOf $json "connections[i].addresses[j].postalCode"]
        set country [CkJsonObject_stringOf $json "connections[i].addresses[j].country"]
        set countryCode [CkJsonObject_stringOf $json "connections[i].addresses[j].countryCode"]
        set j [expr $j + 1]
    }
    set i [expr $i + 1]
}

delete_CkJsonObject $jsonToken
delete_CkHttp $http
delete_CkStringBuilder $sbResponseBody
delete_CkJsonObject $json