Sample code for 30+ languages & platforms
Unicode C

Google People API - List the User's Contacts

See more Google People Examples

Gets a list of people in the user's contacts.

Chilkat Unicode C Downloads

Unicode C
#include <C_CkJsonObjectW.h>
#include <C_CkHttpW.h>
#include <C_CkStringBuilderW.h>

void ChilkatSample(void)
    {
    BOOL success;
    HCkJsonObjectW jsonToken;
    HCkHttpW http;
    HCkStringBuilderW sbResponseBody;
    HCkJsonObjectW json;
    const wchar_t *resourceName;
    const wchar_t *etag;
    int j;
    int count_j;
    BOOL metadataPrimary;
    const wchar_t *metadataSourceType;
    const wchar_t *metadataSourceId;
    const wchar_t *displayName;
    const wchar_t *familyName;
    const wchar_t *givenName;
    const wchar_t *displayNameLastFirst;
    const wchar_t *unstructuredName;
    const wchar_t *value;
    const wchar_t *formattedValue;
    const wchar_t *streetAddress;
    const wchar_t *city;
    const wchar_t *region;
    const wchar_t *postalCode;
    const wchar_t *country;
    const wchar_t *countryCode;
    int totalPeople;
    int totalItems;
    int i;
    int count_i;

    success = FALSE;

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

    //  It is assumed we previously obtained an OAuth2 access token.
    //  This example loads the JSON access token file 

    //  originally obtained by this example: Get Google People API OAuth2 Access Token
    //  or refreshed by this example: Refresh Google People API OAuth2 Access Token

    jsonToken = CkJsonObjectW_Create();
    success = CkJsonObjectW_LoadFile(jsonToken,L"qa_data/tokens/googlePeople.json");
    if (success != TRUE) {
        wprintf(L"Failed to load googleContacts.json\n");
        CkJsonObjectW_Dispose(jsonToken);
        return;
    }

    http = CkHttpW_Create();

    CkHttpW_putAuthToken(http,CkJsonObjectW_stringOf(jsonToken,L"access_token"));

    sbResponseBody = CkStringBuilderW_Create();
    success = CkHttpW_QuickGetSb(http,L"https://people.googleapis.com/v1/people/me/connections?personFields=names,addresses,emailAddresses",sbResponseBody);
    if (success == FALSE) {
        wprintf(L"%s\n",CkHttpW_lastErrorText(http));
        wprintf(L"%s\n",CkStringBuilderW_getAsString(sbResponseBody));
        CkJsonObjectW_Dispose(jsonToken);
        CkHttpW_Dispose(http);
        CkStringBuilderW_Dispose(sbResponseBody);
        return;
    }

    //  Sample JSON Response

    //  Use this online tool to generate parsing code from sample JSON: 
    //  Generate Parsing Code from JSON

    //  {
    //    "connections": [
    //      {
    //        "resourceName": "people/c8871101035606120608",
    //        "etag": "%EgkBAj0JQhBANy4aBAECBQciDERLSGdVTFNPbzNJPQ==",
    //        "names": [
    //          {
    //            "metadata": {
    //              "primary": true,
    //              "source": {
    //                "type": "CONTACT",
    //                "id": "7b1c7b6409e718a0"
    //              }
    //            },
    //            "displayName": "Chilkat Cloud",
    //            "familyName": "Cloud",
    //            "givenName": "Chilkat",
    //            "displayNameLastFirst": "Cloud, Chilkat",
    //            "unstructuredName": "Chilkat Cloud"
    //          }
    //        ],
    //        "emailAddresses": [
    //          {
    //            "metadata": {
    //              "primary": true,
    //              "source": {
    //                "type": "CONTACT",
    //                "id": "7b1c7b6409e718a0"
    //              }
    //            },
    //            "value": "support@***.com"
    //          }
    //        ]
    //      },
    //      {
    //        "resourceName": "people/c7607335470312011517",
    //        "etag": "%EgkBAj0JQhBANy4aBAECBQciDGZETUtHVTVMazI4PQ==",
    //        "names": [
    //          {
    //            "metadata": {
    //              "primary": true,
    //              "source": {
    //                "type": "CONTACT",
    //                "id": "6992af4e0b2d36fd"
    //              }
    //            },
    //            "displayName": "Matt Smith",
    //            "familyName": "Smith",
    //            "givenName": "Matt",
    //            "displayNameLastFirst": "Smith, Matt",
    //            "unstructuredName": "Matt Smith"
    //          }
    //        ],
    //        "addresses": [
    //          {
    //            "metadata": {
    //              "primary": true,
    //              "source": {
    //                "type": "CONTACT",
    //                "id": "6992af4e0b2d36fd"
    //              }
    //            },
    //            "formattedValue": "2222 E Foorest Ave\nWheaton, IL 60999\nUS",
    //            "streetAddress": "2222 E Foorest Ave",
    //            "city": "Wheaton",
    //            "region": "IL",
    //            "postalCode": "60999",
    //            "country": "US",
    //            "countryCode": "US"
    //          }
    //        ],
    //        "emailAddresses": [
    //          {
    //            "metadata": {
    //              "primary": true,
    //              "source": {
    //                "type": "CONTACT",
    //                "id": "6992af4e0b2d36fd"
    //              }
    //            },
    //            "value": "matt@***.com"
    //          },
    //          {
    //            "metadata": {
    //              "source": {
    //                "type": "CONTACT",
    //                "id": "6992af4e0b2d36fd"
    //              }
    //            },
    //            "value": "admin@***.com"
    //          }
    //        ]
    //      }
    //    ],
    //    "totalPeople": 2,
    //    "totalItems": 2
    //  }

    json = CkJsonObjectW_Create();
    CkJsonObjectW_putEmitCompact(json,FALSE);
    CkJsonObjectW_Load(json,CkStringBuilderW_getAsString(sbResponseBody));
    wprintf(L"%s\n",CkJsonObjectW_emit(json));

    //  Chilkat functions returning "const char *" return a pointer to temporary internal memory owned and managed by Chilkat.
    //  See this example explaining how this memory should be used: const char * functions.

    totalPeople = CkJsonObjectW_IntOf(json,L"totalPeople");
    totalItems = CkJsonObjectW_IntOf(json,L"totalItems");
    i = 0;
    count_i = CkJsonObjectW_SizeOfArray(json,L"connections");
    while (i < count_i) {
        CkJsonObjectW_putI(json,i);
        resourceName = CkJsonObjectW_stringOf(json,L"connections[i].resourceName");
        etag = CkJsonObjectW_stringOf(json,L"connections[i].etag");
        j = 0;
        count_j = CkJsonObjectW_SizeOfArray(json,L"connections[i].names");
        while (j < count_j) {
            CkJsonObjectW_putJ(json,j);
            metadataPrimary = CkJsonObjectW_BoolOf(json,L"connections[i].names[j].metadata.primary");
            metadataSourceType = CkJsonObjectW_stringOf(json,L"connections[i].names[j].metadata.source.type");
            metadataSourceId = CkJsonObjectW_stringOf(json,L"connections[i].names[j].metadata.source.id");
            displayName = CkJsonObjectW_stringOf(json,L"connections[i].names[j].displayName");
            familyName = CkJsonObjectW_stringOf(json,L"connections[i].names[j].familyName");
            givenName = CkJsonObjectW_stringOf(json,L"connections[i].names[j].givenName");
            displayNameLastFirst = CkJsonObjectW_stringOf(json,L"connections[i].names[j].displayNameLastFirst");
            unstructuredName = CkJsonObjectW_stringOf(json,L"connections[i].names[j].unstructuredName");
            j = j + 1;
        }

        j = 0;
        count_j = CkJsonObjectW_SizeOfArray(json,L"connections[i].emailAddresses");
        while (j < count_j) {
            CkJsonObjectW_putJ(json,j);
            metadataPrimary = CkJsonObjectW_BoolOf(json,L"connections[i].emailAddresses[j].metadata.primary");
            metadataSourceType = CkJsonObjectW_stringOf(json,L"connections[i].emailAddresses[j].metadata.source.type");
            metadataSourceId = CkJsonObjectW_stringOf(json,L"connections[i].emailAddresses[j].metadata.source.id");
            value = CkJsonObjectW_stringOf(json,L"connections[i].emailAddresses[j].value");
            j = j + 1;
        }

        j = 0;
        count_j = CkJsonObjectW_SizeOfArray(json,L"connections[i].addresses");
        while (j < count_j) {
            CkJsonObjectW_putJ(json,j);
            metadataPrimary = CkJsonObjectW_BoolOf(json,L"connections[i].addresses[j].metadata.primary");
            metadataSourceType = CkJsonObjectW_stringOf(json,L"connections[i].addresses[j].metadata.source.type");
            metadataSourceId = CkJsonObjectW_stringOf(json,L"connections[i].addresses[j].metadata.source.id");
            formattedValue = CkJsonObjectW_stringOf(json,L"connections[i].addresses[j].formattedValue");
            streetAddress = CkJsonObjectW_stringOf(json,L"connections[i].addresses[j].streetAddress");
            city = CkJsonObjectW_stringOf(json,L"connections[i].addresses[j].city");
            region = CkJsonObjectW_stringOf(json,L"connections[i].addresses[j].region");
            postalCode = CkJsonObjectW_stringOf(json,L"connections[i].addresses[j].postalCode");
            country = CkJsonObjectW_stringOf(json,L"connections[i].addresses[j].country");
            countryCode = CkJsonObjectW_stringOf(json,L"connections[i].addresses[j].countryCode");
            j = j + 1;
        }

        i = i + 1;
    }



    CkJsonObjectW_Dispose(jsonToken);
    CkHttpW_Dispose(http);
    CkStringBuilderW_Dispose(sbResponseBody);
    CkJsonObjectW_Dispose(json);

    }