PowerBuilder
PowerBuilder
Google Contacts - Create New Contact
See more Google APIs Examples
Demonstrates how to create a new contact for the Google Contacts API.Chilkat PowerBuilder Downloads
integer li_rc
integer li_Success
oleobject loo_Xml
oleobject loo_JsonToken
oleobject loo_GAuth
oleobject loo_Rest
integer li_BAutoReconnect
oleobject loo_SbRequestBody
oleobject loo_SbResponseBody
li_Success = 0
// This example requires the Chilkat API to have been previously unlocked.
// See Global Unlock Sample for sample code.
// First create a new contact XML.
loo_Xml = create oleobject
li_rc = loo_Xml.ConnectToNewObject("Chilkat.Xml")
if li_rc < 0 then
destroy loo_Xml
MessageBox("Error","Connecting to COM object failed")
return
end if
loo_Xml.Tag = "atom:entry"
loo_Xml.AddAttribute("xmlns:atom","http://www.w3.org/2005/Atom")
loo_Xml.AddAttribute("xmlns:gd","http://schemas.google.com/g/2005")
loo_Xml.UpdateAttrAt("atom:category",1,"scheme","http://schemas.google.com/g/2005#kind")
loo_Xml.UpdateAttrAt("atom:category",1,"term","http://schemas.google.com/contact/2008#contact")
loo_Xml.UpdateChildContent("gd:name|gd:givenName","Elizabeth")
loo_Xml.UpdateChildContent("gd:name|gd:familyName","Bennet")
loo_Xml.UpdateChildContent("gd:name|gd:fullName","Elizabeth Bennet")
loo_Xml.UpdateAttrAt("atom:content",1,"type","text")
loo_Xml.UpdateChildContent("atom:content","Notes")
loo_Xml.UpdateAttrAt("gd:email",1,"rel","http://schemas.google.com/g/2005#work")
loo_Xml.UpdateAttrAt("gd:email",1,"primary","true")
loo_Xml.UpdateAttrAt("gd:email",1,"address","liz@gmail.com")
loo_Xml.UpdateAttrAt("gd:email",1,"displayName","E. Bennet")
loo_Xml.UpdateAttrAt("gd:email",1,"rel","http://schemas.google.com/g/2005#home")
loo_Xml.UpdateAttrAt("gd:email",1,"address","liz@example.org")
loo_Xml.UpdateAttrAt("gd:phoneNumber",1,"rel","http://schemas.google.com/g/2005#work")
loo_Xml.UpdateAttrAt("gd:phoneNumber",1,"primary","true")
loo_Xml.UpdateChildContent("gd:phoneNumber","(206)555-1212")
loo_Xml.UpdateAttrAt("gd:phoneNumber",1,"rel","http://schemas.google.com/g/2005#home")
loo_Xml.UpdateChildContent("gd:phoneNumber","(206)555-1213")
loo_Xml.UpdateAttrAt("gd:im",1,"address","liz@gmail.com")
loo_Xml.UpdateAttrAt("gd:im",1,"protocol","http://schemas.google.com/g/2005#GOOGLE_TALK")
loo_Xml.UpdateAttrAt("gd:im",1,"primary","true")
loo_Xml.UpdateAttrAt("gd:im",1,"rel","http://schemas.google.com/g/2005#home")
loo_Xml.UpdateAttrAt("gd:structuredPostalAddress",1,"rel","http://schemas.google.com/g/2005#work")
loo_Xml.UpdateAttrAt("gd:structuredPostalAddress",1,"primary","true")
loo_Xml.UpdateChildContent("gd:structuredPostalAddress|gd:city","Mountain View")
loo_Xml.UpdateChildContent("gd:structuredPostalAddress|gd:street","1600 Amphitheatre Pkwy")
loo_Xml.UpdateChildContent("gd:structuredPostalAddress|gd:region","CA")
loo_Xml.UpdateChildContent("gd:structuredPostalAddress|gd:postcode","94043")
loo_Xml.UpdateChildContent("gd:structuredPostalAddress|gd:country","United States")
loo_Xml.UpdateChildContent("gd:structuredPostalAddress|gd:formattedAddress","1600 Amphitheatre Pkwy Mountain View")
Write-Debug loo_Xml.GetXml()
// 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
loo_JsonToken = create oleobject
li_rc = loo_JsonToken.ConnectToNewObject("Chilkat.JsonObject")
li_Success = loo_JsonToken.LoadFile("qa_data/tokens/googleContacts.json")
if li_Success <> 1 then
Write-Debug "Failed to load googleContacts.json"
destroy loo_Xml
destroy loo_JsonToken
return
end if
loo_GAuth = create oleobject
li_rc = loo_GAuth.ConnectToNewObject("Chilkat.AuthGoogle")
loo_GAuth.AccessToken = loo_JsonToken.StringOf("access_token")
loo_Rest = create oleobject
li_rc = loo_Rest.ConnectToNewObject("Chilkat.Rest")
// Connect using TLS.
li_BAutoReconnect = 1
li_Success = loo_Rest.Connect("www.google.com",443,1,li_BAutoReconnect)
// Provide the authentication credentials (i.e. the access token)
loo_Rest.SetAuthGoogle(loo_GAuth)
// ----------------------------------------------
// 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
loo_Rest.AddHeader("Content-Type","application/atom+xml")
loo_Rest.AddHeader("GData-Version","3.0")
loo_SbRequestBody = create oleobject
li_rc = loo_SbRequestBody.ConnectToNewObject("Chilkat.StringBuilder")
loo_SbResponseBody = create oleobject
li_rc = loo_SbResponseBody.ConnectToNewObject("Chilkat.StringBuilder")
loo_Xml.GetXmlSb(loo_SbRequestBody)
li_Success = loo_Rest.FullRequestSb("POST","/m8/feeds/contacts/default/full",loo_SbRequestBody,loo_SbResponseBody)
if li_Success <> 1 then
Write-Debug loo_Rest.LastErrorText
destroy loo_Xml
destroy loo_JsonToken
destroy loo_GAuth
destroy loo_Rest
destroy loo_SbRequestBody
destroy loo_SbResponseBody
return
end if
// A successful response will have a status code equal to 201.
if loo_Rest.ResponseStatusCode <> 201 then
Write-Debug "response status code = " + string(loo_Rest.ResponseStatusCode)
Write-Debug "response status text = " + loo_Rest.ResponseStatusText
Write-Debug "response header: " + loo_Rest.ResponseHeader
Write-Debug "response body: " + loo_SbResponseBody.GetAsString()
destroy loo_Xml
destroy loo_JsonToken
destroy loo_GAuth
destroy loo_Rest
destroy loo_SbRequestBody
destroy loo_SbResponseBody
return
end if
// If the 201 response was received, then the contact was successfully created,
// and there is no response body.
Write-Debug "Contact created."
destroy loo_Xml
destroy loo_JsonToken
destroy loo_GAuth
destroy loo_Rest
destroy loo_SbRequestBody
destroy loo_SbResponseBody