Sample code for 30+ languages & platforms
AutoIt

Download Google Contact Photo

See more Google APIs Examples

Demonstrates how to download Google Contact's photo.

Chilkat AutoIt Downloads

AutoIt
Local $bSuccess = False

;  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

$oJsonToken = ObjCreate("Chilkat.JsonObject")
$bSuccess = $oJsonToken.LoadFile("qa_data/tokens/googleContacts.json")
If ($bSuccess <> True) Then
    ConsoleWrite("Failed to load googleContacts.json" & @CRLF)
    Exit
EndIf

$oGAuth = ObjCreate("Chilkat.AuthGoogle")
$oGAuth.AccessToken = $oJsonToken.StringOf("access_token")

$oRest = ObjCreate("Chilkat.Rest")

;  Connect using TLS.
Local $bAutoReconnect = True
$bSuccess = $oRest.Connect("www.google.com",443,True,$bAutoReconnect)

;  Provide the authentication credentials (i.e. the access token)
$oRest.SetAuthGoogle($oGAuth)

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

;  To get the photo, send the following:

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

$oRest.AddHeader("GData-Version","3.0")

$oSbPath = ObjCreate("Chilkat.StringBuilder")
;  Get the photo for the contact having contactId = "1ea2e4fe0ef24e09"
Local $sContactId = "1ea2e4fe0ef24e09"
$oSbPath.SetString("/m8/feeds/photos/media/default/{contactId}")
Local $iNumReplacements = $oSbPath.Replace("{contactId}",$sContactId)

$oImageData = ObjCreate("Chilkat.BinData")
$bSuccess = $oRest.FullRequestNoBodyBd("GET",$oSbPath.GetAsString(),$oImageData)
If ($bSuccess <> True) Then
    ConsoleWrite($oRest.LastErrorText & @CRLF)
    Exit
EndIf

;  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 ($oRest.ResponseStatusCode = 404) Then
    ConsoleWrite("This contact has no photo." & @CRLF)
    Exit
EndIf

;  A successful response will have a status code equal to 200.
If ($oRest.ResponseStatusCode <> 200) Then
    ;  If the response was not successful, then the response body
    ;  does not contain image data.  Instead it contains XML.
    $oSbResponseBody = ObjCreate("Chilkat.StringBuilder")
    $oSbResponseBody.AppendBd($oImageData,"utf-8",0,0)

    ConsoleWrite("response status code = " & $oRest.ResponseStatusCode & @CRLF)
    ConsoleWrite("response status text = " & $oRest.ResponseStatusText & @CRLF)
    ConsoleWrite("response header: " & $oRest.ResponseHeader & @CRLF)
    ConsoleWrite("response body: " & $oSbResponseBody.GetAsString() & @CRLF)
    ConsoleWrite("request startline: " & $oRest.LastRequestStartLine & @CRLF)
    ConsoleWrite("request header: " & $oRest.LastRequestHeader & @CRLF)
    Exit
EndIf

;  Examine the content-type in the response header so we know what file
;  extension to use (.jpg, .png, etc.)
$oSbContentType = ObjCreate("Chilkat.StringBuilder")
$oSbContentType.Append($oRest.ResponseHdrByName("Content-Type"))

If ($oSbContentType.ContentsEqual("image/jpeg",False) = True) Then
    $oImageData.WriteFile("qa_output/contact_photo.jpg")
EndIf

If ($oSbContentType.ContentsEqual("image/png",False) = True) Then
    $oImageData.WriteFile("qa_output/contact_photo.png")
EndIf

ConsoleWrite("Content-Type: " & $oSbContentType.GetAsString() & @CRLF)
ConsoleWrite("Success." & @CRLF)