Sample code for 30+ languages & platforms
DataFlex

Google Contacts - Create New Contact

See more Google APIs Examples

Demonstrates how to create a new contact for the Google Contacts API.

Chilkat DataFlex Downloads

DataFlex
Use ChilkatAx-win32.pkg

Procedure Test
    Boolean iSuccess
    Handle hoXml
    Handle hoJsonToken
    Variant vGAuth
    Handle hoGAuth
    Handle hoRest
    Boolean iBAutoReconnect
    Variant vSbRequestBody
    Handle hoSbRequestBody
    Variant vSbResponseBody
    Handle hoSbResponseBody
    String sTemp1
    Integer iTemp1

    Move False To iSuccess

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

    // First create a new contact XML.
    Get Create (RefClass(cComChilkatXml)) To hoXml
    If (Not(IsComObjectCreated(hoXml))) Begin
        Send CreateComObject of hoXml
    End
    Set ComTag Of hoXml To "atom:entry"
    Get ComAddAttribute Of hoXml "xmlns:atom" "http://www.w3.org/2005/Atom" To iSuccess
    Get ComAddAttribute Of hoXml "xmlns:gd" "http://schemas.google.com/g/2005" To iSuccess
    Get ComUpdateAttrAt Of hoXml "atom:category" True "scheme" "http://schemas.google.com/g/2005#kind" To iSuccess
    Get ComUpdateAttrAt Of hoXml "atom:category" True "term" "http://schemas.google.com/contact/2008#contact" To iSuccess
    Send ComUpdateChildContent To hoXml "gd:name|gd:givenName" "Elizabeth"
    Send ComUpdateChildContent To hoXml "gd:name|gd:familyName" "Bennet"
    Send ComUpdateChildContent To hoXml "gd:name|gd:fullName" "Elizabeth Bennet"
    Get ComUpdateAttrAt Of hoXml "atom:content" True "type" "text" To iSuccess
    Send ComUpdateChildContent To hoXml "atom:content" "Notes"
    Get ComUpdateAttrAt Of hoXml "gd:email" True "rel" "http://schemas.google.com/g/2005#work" To iSuccess
    Get ComUpdateAttrAt Of hoXml "gd:email" True "primary" "true" To iSuccess
    Get ComUpdateAttrAt Of hoXml "gd:email" True "address" "liz@gmail.com" To iSuccess
    Get ComUpdateAttrAt Of hoXml "gd:email" True "displayName" "E. Bennet" To iSuccess
    Get ComUpdateAttrAt Of hoXml "gd:email" True "rel" "http://schemas.google.com/g/2005#home" To iSuccess
    Get ComUpdateAttrAt Of hoXml "gd:email" True "address" "liz@example.org" To iSuccess
    Get ComUpdateAttrAt Of hoXml "gd:phoneNumber" True "rel" "http://schemas.google.com/g/2005#work" To iSuccess
    Get ComUpdateAttrAt Of hoXml "gd:phoneNumber" True "primary" "true" To iSuccess
    Send ComUpdateChildContent To hoXml "gd:phoneNumber" "(206)555-1212"
    Get ComUpdateAttrAt Of hoXml "gd:phoneNumber" True "rel" "http://schemas.google.com/g/2005#home" To iSuccess
    Send ComUpdateChildContent To hoXml "gd:phoneNumber" "(206)555-1213"
    Get ComUpdateAttrAt Of hoXml "gd:im" True "address" "liz@gmail.com" To iSuccess
    Get ComUpdateAttrAt Of hoXml "gd:im" True "protocol" "http://schemas.google.com/g/2005#GOOGLE_TALK" To iSuccess
    Get ComUpdateAttrAt Of hoXml "gd:im" True "primary" "true" To iSuccess
    Get ComUpdateAttrAt Of hoXml "gd:im" True "rel" "http://schemas.google.com/g/2005#home" To iSuccess
    Get ComUpdateAttrAt Of hoXml "gd:structuredPostalAddress" True "rel" "http://schemas.google.com/g/2005#work" To iSuccess
    Get ComUpdateAttrAt Of hoXml "gd:structuredPostalAddress" True "primary" "true" To iSuccess
    Send ComUpdateChildContent To hoXml "gd:structuredPostalAddress|gd:city" "Mountain View"
    Send ComUpdateChildContent To hoXml "gd:structuredPostalAddress|gd:street" "1600 Amphitheatre Pkwy"
    Send ComUpdateChildContent To hoXml "gd:structuredPostalAddress|gd:region" "CA"
    Send ComUpdateChildContent To hoXml "gd:structuredPostalAddress|gd:postcode" "94043"
    Send ComUpdateChildContent To hoXml "gd:structuredPostalAddress|gd:country" "United States"
    Send ComUpdateChildContent To hoXml "gd:structuredPostalAddress|gd:formattedAddress" "1600 Amphitheatre Pkwy Mountain View"

    Get ComGetXml Of hoXml To sTemp1
    Showln sTemp1

    // Created the following XML:

    // 	<?xml version="1.0" encoding="utf-8" ?>
    // 	<atom:entry xmlns:atom="http://www.w3.org/2005/Atom" xmlns:gd="http://schemas.google.com/g/2005">
    // 	    <atom:category scheme="http://schemas.google.com/g/2005#kind" term="http://schemas.google.com/contact/2008#contact" />
    // 	    <gd:name>
    // 	        <gd:givenName>Elizabeth</gd:givenName>
    // 	        <gd:familyName>Bennet</gd:familyName>
    // 	        <gd:fullName>Elizabeth Bennet</gd:fullName>
    // 	    </gd:name>
    // 	    <atom:content type="text">Notes</atom:content>
    // 	    <gd:email primary="true" displayName="E. Bennet" rel="http://schemas.google.com/g/2005#home" address="liz@example.org" />
    // 	    <gd:phoneNumber primary="true" rel="http://schemas.google.com/g/2005#home">(206)555-1213</gd:phoneNumber>
    // 	    <gd:im address="liz@gmail.com" protocol="http://schemas.google.com/g/2005#GOOGLE_TALK" primary="true" rel="http://schemas.google.com/g/2005#home" />
    // 	    <gd:structuredPostalAddress rel="http://schemas.google.com/g/2005#work" primary="true">
    // 	        <gd:city>Mountain View</gd:city>
    // 	        <gd:street>1600 Amphitheatre Pkwy</gd:street>
    // 	        <gd:region>CA</gd:region>
    // 	        <gd:postcode>94043</gd:postcode>
    // 	        <gd:country>United States</gd:country>
    // 	        <gd:formattedAddress>1600 Amphitheatre Pkwy Mountain View</gd:formattedAddress>
    // 	    </gd:structuredPostalAddress>
    // 	</atom:entry>

    // --------------------------------------------------------------------------------------------------------
    // 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

    Get Create (RefClass(cComChilkatJsonObject)) To hoJsonToken
    If (Not(IsComObjectCreated(hoJsonToken))) Begin
        Send CreateComObject of hoJsonToken
    End
    Get ComLoadFile Of hoJsonToken "qa_data/tokens/googleContacts.json" To iSuccess
    If (iSuccess <> True) Begin
        Showln "Failed to load googleContacts.json"
        Procedure_Return
    End

    Get Create (RefClass(cComChilkatAuthGoogle)) To hoGAuth
    If (Not(IsComObjectCreated(hoGAuth))) Begin
        Send CreateComObject of hoGAuth
    End
    Get ComStringOf Of hoJsonToken "access_token" To sTemp1
    Set ComAccessToken Of hoGAuth To sTemp1

    Get Create (RefClass(cComChilkatRest)) To hoRest
    If (Not(IsComObjectCreated(hoRest))) Begin
        Send CreateComObject of hoRest
    End

    // Connect using TLS.
    Move True To iBAutoReconnect
    Get ComConnect Of hoRest "www.google.com" 443 True iBAutoReconnect To iSuccess

    // Provide the authentication credentials (i.e. the access token)
    Get pvComObject of hoGAuth to vGAuth
    Get ComSetAuthGoogle Of hoRest vGAuth To iSuccess

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

    // To create a contact, we need to send the following:

    // 	POST /m8/feeds/contacts/default/full
    // 	Content-Type: application/atom+xml
    // 	GData-Version: 3.0

    Get ComAddHeader Of hoRest "Content-Type" "application/atom+xml" To iSuccess
    Get ComAddHeader Of hoRest "GData-Version" "3.0" To iSuccess

    Get Create (RefClass(cComChilkatStringBuilder)) To hoSbRequestBody
    If (Not(IsComObjectCreated(hoSbRequestBody))) Begin
        Send CreateComObject of hoSbRequestBody
    End
    Get Create (RefClass(cComChilkatStringBuilder)) To hoSbResponseBody
    If (Not(IsComObjectCreated(hoSbResponseBody))) Begin
        Send CreateComObject of hoSbResponseBody
    End
    Get pvComObject of hoSbRequestBody to vSbRequestBody
    Get ComGetXmlSb Of hoXml vSbRequestBody To iSuccess
    Get pvComObject of hoSbRequestBody to vSbRequestBody
    Get pvComObject of hoSbResponseBody to vSbResponseBody
    Get ComFullRequestSb Of hoRest "POST" "/m8/feeds/contacts/default/full" vSbRequestBody vSbResponseBody To iSuccess
    If (iSuccess <> True) Begin
        Get ComLastErrorText Of hoRest To sTemp1
        Showln sTemp1
        Procedure_Return
    End

    // A successful response will have a status code equal to 201.
    Get ComResponseStatusCode Of hoRest To iTemp1
    If (iTemp1 <> 201) Begin
        Get ComResponseStatusCode Of hoRest To iTemp1
        Showln "response status code = " iTemp1
        Get ComResponseStatusText Of hoRest To sTemp1
        Showln "response status text = " sTemp1
        Get ComResponseHeader Of hoRest To sTemp1
        Showln "response header: " sTemp1
        Get ComGetAsString Of hoSbResponseBody To sTemp1
        Showln "response body: " sTemp1
        Procedure_Return
    End

    // If the 201 response was received, then the contact was successfully created,
    // and there is no response body.
    Showln "Contact created."


End_Procedure