Sample code for 30+ languages & platforms
Tcl

Download Google Contact Photo

See more Google APIs Examples

Demonstrates how to download Google Contact's photo.

Chilkat Tcl Downloads

Tcl

load ./chilkat.dll

set success 0

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

# --------------------------------------------------------------------------------------------------------
# Note: The code for setting up the Chilkat REST object and making the initial connection can be done once.
# Once connected, the REST object may be re-used for many REST API calls.
# (It's a good idea to put the connection setup code in a separate function/subroutine.)
# --------------------------------------------------------------------------------------------------------

# It is assumed we previously obtained an OAuth2 access token.
# This example loads the JSON access token file 
# saved by this example: Get Google Contacts OAuth2 Access Token

set jsonToken [new_CkJsonObject]

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

set gAuth [new_CkAuthGoogle]

CkAuthGoogle_put_AccessToken $gAuth [CkJsonObject_stringOf $jsonToken "access_token"]

set rest [new_CkRest]

# Connect using TLS.
set bAutoReconnect 1
set success [CkRest_Connect $rest "www.google.com" 443 1 $bAutoReconnect]

# Provide the authentication credentials (i.e. the access token)
CkRest_SetAuthGoogle $rest $gAuth

# ----------------------------------------------
# OK, the REST connection setup is completed..
# ----------------------------------------------

# To get the photo, send the following:

# 	GET /m8/feeds/photos/media/default/contactId

CkRest_AddHeader $rest "GData-Version" "3.0"

set sbPath [new_CkStringBuilder]

# Get the photo for the contact having contactId = "1ea2e4fe0ef24e09"
set contactId "1ea2e4fe0ef24e09"
CkStringBuilder_SetString $sbPath "/m8/feeds/photos/media/default/{contactId}"
set numReplacements [CkStringBuilder_Replace $sbPath "{contactId}" $contactId]

set imageData [new_CkBinData]

set success [CkRest_FullRequestNoBodyBd $rest "GET" [CkStringBuilder_getAsString $sbPath] $imageData]
if {$success != 1} then {
    puts [CkRest_lastErrorText $rest]
    delete_CkJsonObject $jsonToken
    delete_CkAuthGoogle $gAuth
    delete_CkRest $rest
    delete_CkStringBuilder $sbPath
    delete_CkBinData $imageData
    exit
}

# A 404 response indicates the contact has no photo.
# (We could've first fetched the contact information, parsed out the 
# photo etag, and then if no photo etag existed, we'd know the contact has no
# photo.  Or... we can just try to download the photo and if a 404 is received,
# we know there's no photo.  Much simpler.)
if {[CkRest_get_ResponseStatusCode $rest] == 404} then {
    puts "This contact has no photo."
    delete_CkJsonObject $jsonToken
    delete_CkAuthGoogle $gAuth
    delete_CkRest $rest
    delete_CkStringBuilder $sbPath
    delete_CkBinData $imageData
    exit
}

# A successful response will have a status code equal to 200.
if {[CkRest_get_ResponseStatusCode $rest] != 200} then {
    # If the response was not successful, then the response body
    # does not contain image data.  Instead it contains XML.
    set sbResponseBody [new_CkStringBuilder]

    CkStringBuilder_AppendBd $sbResponseBody $imageData "utf-8" 0 0

    puts "response status code = [CkRest_get_ResponseStatusCode $rest]"
    puts "response status text = [CkRest_responseStatusText $rest]"
    puts "response header: [CkRest_responseHeader $rest]"
    puts "response body: [CkStringBuilder_getAsString $sbResponseBody]"
    puts "request startline: [CkRest_lastRequestStartLine $rest]"
    puts "request header: [CkRest_lastRequestHeader $rest]"
    delete_CkJsonObject $jsonToken
    delete_CkAuthGoogle $gAuth
    delete_CkRest $rest
    delete_CkStringBuilder $sbPath
    delete_CkBinData $imageData
    delete_CkStringBuilder $sbResponseBody
    exit
}

# Examine the content-type in the response header so we know what file
# extension to use (.jpg, .png, etc.)
set sbContentType [new_CkStringBuilder]

CkStringBuilder_Append $sbContentType [CkRest_responseHdrByName $rest "Content-Type"]

if {[CkStringBuilder_ContentsEqual $sbContentType "image/jpeg" 0] == 1} then {
    CkBinData_WriteFile $imageData "qa_output/contact_photo.jpg"
}

if {[CkStringBuilder_ContentsEqual $sbContentType "image/png" 0] == 1} then {
    CkBinData_WriteFile $imageData "qa_output/contact_photo.png"
}

puts "Content-Type: [CkStringBuilder_getAsString $sbContentType]"
puts "Success."

delete_CkJsonObject $jsonToken
delete_CkAuthGoogle $gAuth
delete_CkRest $rest
delete_CkStringBuilder $sbPath
delete_CkBinData $imageData
delete_CkStringBuilder $sbResponseBody
delete_CkStringBuilder $sbContentType