Unicode C
Unicode C
Google Contacts - Create New Contact
See more Google APIs Examples
Demonstrates how to create a new contact for the Google Contacts API.Chilkat Unicode C Downloads
#include <C_CkXmlW.h>
#include <C_CkJsonObjectW.h>
#include <C_CkAuthGoogleW.h>
#include <C_CkRestW.h>
#include <C_CkStringBuilderW.h>
void ChilkatSample(void)
{
BOOL success;
HCkXmlW xml;
HCkJsonObjectW jsonToken;
HCkAuthGoogleW gAuth;
HCkRestW rest;
BOOL bAutoReconnect;
HCkStringBuilderW sbRequestBody;
HCkStringBuilderW sbResponseBody;
success = FALSE;
// This example requires the Chilkat API to have been previously unlocked.
// See Global Unlock Sample for sample code.
// First create a new contact XML.
xml = CkXmlW_Create();
CkXmlW_putTag(xml,L"atom:entry");
CkXmlW_AddAttribute(xml,L"xmlns:atom",L"http://www.w3.org/2005/Atom");
CkXmlW_AddAttribute(xml,L"xmlns:gd",L"http://schemas.google.com/g/2005");
CkXmlW_UpdateAttrAt(xml,L"atom:category",TRUE,L"scheme",L"http://schemas.google.com/g/2005#kind");
CkXmlW_UpdateAttrAt(xml,L"atom:category",TRUE,L"term",L"http://schemas.google.com/contact/2008#contact");
CkXmlW_UpdateChildContent(xml,L"gd:name|gd:givenName",L"Elizabeth");
CkXmlW_UpdateChildContent(xml,L"gd:name|gd:familyName",L"Bennet");
CkXmlW_UpdateChildContent(xml,L"gd:name|gd:fullName",L"Elizabeth Bennet");
CkXmlW_UpdateAttrAt(xml,L"atom:content",TRUE,L"type",L"text");
CkXmlW_UpdateChildContent(xml,L"atom:content",L"Notes");
CkXmlW_UpdateAttrAt(xml,L"gd:email",TRUE,L"rel",L"http://schemas.google.com/g/2005#work");
CkXmlW_UpdateAttrAt(xml,L"gd:email",TRUE,L"primary",L"true");
CkXmlW_UpdateAttrAt(xml,L"gd:email",TRUE,L"address",L"liz@gmail.com");
CkXmlW_UpdateAttrAt(xml,L"gd:email",TRUE,L"displayName",L"E. Bennet");
CkXmlW_UpdateAttrAt(xml,L"gd:email",TRUE,L"rel",L"http://schemas.google.com/g/2005#home");
CkXmlW_UpdateAttrAt(xml,L"gd:email",TRUE,L"address",L"liz@example.org");
CkXmlW_UpdateAttrAt(xml,L"gd:phoneNumber",TRUE,L"rel",L"http://schemas.google.com/g/2005#work");
CkXmlW_UpdateAttrAt(xml,L"gd:phoneNumber",TRUE,L"primary",L"true");
CkXmlW_UpdateChildContent(xml,L"gd:phoneNumber",L"(206)555-1212");
CkXmlW_UpdateAttrAt(xml,L"gd:phoneNumber",TRUE,L"rel",L"http://schemas.google.com/g/2005#home");
CkXmlW_UpdateChildContent(xml,L"gd:phoneNumber",L"(206)555-1213");
CkXmlW_UpdateAttrAt(xml,L"gd:im",TRUE,L"address",L"liz@gmail.com");
CkXmlW_UpdateAttrAt(xml,L"gd:im",TRUE,L"protocol",L"http://schemas.google.com/g/2005#GOOGLE_TALK");
CkXmlW_UpdateAttrAt(xml,L"gd:im",TRUE,L"primary",L"true");
CkXmlW_UpdateAttrAt(xml,L"gd:im",TRUE,L"rel",L"http://schemas.google.com/g/2005#home");
CkXmlW_UpdateAttrAt(xml,L"gd:structuredPostalAddress",TRUE,L"rel",L"http://schemas.google.com/g/2005#work");
CkXmlW_UpdateAttrAt(xml,L"gd:structuredPostalAddress",TRUE,L"primary",L"true");
CkXmlW_UpdateChildContent(xml,L"gd:structuredPostalAddress|gd:city",L"Mountain View");
CkXmlW_UpdateChildContent(xml,L"gd:structuredPostalAddress|gd:street",L"1600 Amphitheatre Pkwy");
CkXmlW_UpdateChildContent(xml,L"gd:structuredPostalAddress|gd:region",L"CA");
CkXmlW_UpdateChildContent(xml,L"gd:structuredPostalAddress|gd:postcode",L"94043");
CkXmlW_UpdateChildContent(xml,L"gd:structuredPostalAddress|gd:country",L"United States");
CkXmlW_UpdateChildContent(xml,L"gd:structuredPostalAddress|gd:formattedAddress",L"1600 Amphitheatre Pkwy Mountain View");
wprintf(L"%s\n",CkXmlW_getXml(xml));
// 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
jsonToken = CkJsonObjectW_Create();
success = CkJsonObjectW_LoadFile(jsonToken,L"qa_data/tokens/googleContacts.json");
if (success != TRUE) {
wprintf(L"Failed to load googleContacts.json\n");
CkXmlW_Dispose(xml);
CkJsonObjectW_Dispose(jsonToken);
return;
}
gAuth = CkAuthGoogleW_Create();
CkAuthGoogleW_putAccessToken(gAuth,CkJsonObjectW_stringOf(jsonToken,L"access_token"));
rest = CkRestW_Create();
// Connect using TLS.
bAutoReconnect = TRUE;
success = CkRestW_Connect(rest,L"www.google.com",443,TRUE,bAutoReconnect);
// Provide the authentication credentials (i.e. the access token)
CkRestW_SetAuthGoogle(rest,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
CkRestW_AddHeader(rest,L"Content-Type",L"application/atom+xml");
CkRestW_AddHeader(rest,L"GData-Version",L"3.0");
sbRequestBody = CkStringBuilderW_Create();
sbResponseBody = CkStringBuilderW_Create();
CkXmlW_GetXmlSb(xml,sbRequestBody);
success = CkRestW_FullRequestSb(rest,L"POST",L"/m8/feeds/contacts/default/full",sbRequestBody,sbResponseBody);
if (success != TRUE) {
wprintf(L"%s\n",CkRestW_lastErrorText(rest));
CkXmlW_Dispose(xml);
CkJsonObjectW_Dispose(jsonToken);
CkAuthGoogleW_Dispose(gAuth);
CkRestW_Dispose(rest);
CkStringBuilderW_Dispose(sbRequestBody);
CkStringBuilderW_Dispose(sbResponseBody);
return;
}
// A successful response will have a status code equal to 201.
if (CkRestW_getResponseStatusCode(rest) != 201) {
wprintf(L"response status code = %d\n",CkRestW_getResponseStatusCode(rest));
wprintf(L"response status text = %s\n",CkRestW_responseStatusText(rest));
wprintf(L"response header: %s\n",CkRestW_responseHeader(rest));
wprintf(L"response body: %s\n",CkStringBuilderW_getAsString(sbResponseBody));
CkXmlW_Dispose(xml);
CkJsonObjectW_Dispose(jsonToken);
CkAuthGoogleW_Dispose(gAuth);
CkRestW_Dispose(rest);
CkStringBuilderW_Dispose(sbRequestBody);
CkStringBuilderW_Dispose(sbResponseBody);
return;
}
// If the 201 response was received, then the contact was successfully created,
// and there is no response body.
wprintf(L"Contact created.\n");
CkXmlW_Dispose(xml);
CkJsonObjectW_Dispose(jsonToken);
CkAuthGoogleW_Dispose(gAuth);
CkRestW_Dispose(rest);
CkStringBuilderW_Dispose(sbRequestBody);
CkStringBuilderW_Dispose(sbResponseBody);
}