Sample code for 30+ languages & platforms
Zig

Google People API - List the User's Contacts

See more Google People Examples

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

Chilkat Zig Downloads

Zig
const std = @import("std");
const chilkat = @import("chilkat");

pub fn main(init: std.process.Init) !void {
    const alloc = init.arena.allocator();

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

    const json_token = try chilkat.JsonObject.init();
    defer json_token.deinit();
    json_token.loadFile("qa_data/tokens/googlePeople.json") catch {
        std.debug.print("Failed to load googleContacts.json\n", .{});
        return;
    };

    const http = try chilkat.Http.init();
    defer http.deinit();

    http.setAuthToken(try json_token.stringOf(alloc, "access_token"));

    const sb_response_body = try chilkat.StringBuilder.init();
    defer sb_response_body.deinit();
    http.quickGetSb("https://people.googleapis.com/v1/people/me/connections?personFields=names,addresses,emailAddresses", sb_response_body) catch {
        std.debug.print("{s}\n", .{try http.getLastErrorText(alloc)});
        std.debug.print("{s}\n", .{try sb_response_body.getAsString(alloc)});
        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
    // }

    const json = try chilkat.JsonObject.init();
    defer json.deinit();
    json.setEmitCompact(false);
    json.load(try sb_response_body.getAsString(alloc)) catch {};
    std.debug.print("{s}\n", .{try json.emit(alloc)});

    var resource_name: [:0]const u8 = "";
    var etag: [:0]const u8 = "";
    var j: i32 = 0;
    var count_j: i32 = 0;
    var metadata_primary: bool = false;
    var metadata_source_type: [:0]const u8 = "";
    var metadata_source_id: [:0]const u8 = "";
    var display_name: [:0]const u8 = "";
    var family_name: [:0]const u8 = "";
    var given_name: [:0]const u8 = "";
    var display_name_last_first: [:0]const u8 = "";
    var unstructured_name: [:0]const u8 = "";
    var value: [:0]const u8 = "";
    var formatted_value: [:0]const u8 = "";
    var street_address: [:0]const u8 = "";
    var city: [:0]const u8 = "";
    var region: [:0]const u8 = "";
    var postal_code: [:0]const u8 = "";
    var country: [:0]const u8 = "";
    var country_code: [:0]const u8 = "";

    _ = json.intOf("totalPeople");
    _ = json.intOf("totalItems");
    var i: i32 = 0;
    const count_i = json.sizeOfArray("connections");
    while (i < count_i) {
        json.setI(i);
        resource_name = try json.stringOf(alloc, "connections[i].resourceName");
        etag = try json.stringOf(alloc, "connections[i].etag");
        j = 0;
        count_j = json.sizeOfArray("connections[i].names");
        while (j < count_j) {
            json.setJ(j);
            metadata_primary = json.boolOf("connections[i].names[j].metadata.primary");
            metadata_source_type = try json.stringOf(alloc, "connections[i].names[j].metadata.source.type");
            metadata_source_id = try json.stringOf(alloc, "connections[i].names[j].metadata.source.id");
            display_name = try json.stringOf(alloc, "connections[i].names[j].displayName");
            family_name = try json.stringOf(alloc, "connections[i].names[j].familyName");
            given_name = try json.stringOf(alloc, "connections[i].names[j].givenName");
            display_name_last_first = try json.stringOf(alloc, "connections[i].names[j].displayNameLastFirst");
            unstructured_name = try json.stringOf(alloc, "connections[i].names[j].unstructuredName");
            j = j + 1;
        }

        j = 0;
        count_j = json.sizeOfArray("connections[i].emailAddresses");
        while (j < count_j) {
            json.setJ(j);
            metadata_primary = json.boolOf("connections[i].emailAddresses[j].metadata.primary");
            metadata_source_type = try json.stringOf(alloc, "connections[i].emailAddresses[j].metadata.source.type");
            metadata_source_id = try json.stringOf(alloc, "connections[i].emailAddresses[j].metadata.source.id");
            value = try json.stringOf(alloc, "connections[i].emailAddresses[j].value");
            j = j + 1;
        }

        j = 0;
        count_j = json.sizeOfArray("connections[i].addresses");
        while (j < count_j) {
            json.setJ(j);
            metadata_primary = json.boolOf("connections[i].addresses[j].metadata.primary");
            metadata_source_type = try json.stringOf(alloc, "connections[i].addresses[j].metadata.source.type");
            metadata_source_id = try json.stringOf(alloc, "connections[i].addresses[j].metadata.source.id");
            formatted_value = try json.stringOf(alloc, "connections[i].addresses[j].formattedValue");
            street_address = try json.stringOf(alloc, "connections[i].addresses[j].streetAddress");
            city = try json.stringOf(alloc, "connections[i].addresses[j].city");
            region = try json.stringOf(alloc, "connections[i].addresses[j].region");
            postal_code = try json.stringOf(alloc, "connections[i].addresses[j].postalCode");
            country = try json.stringOf(alloc, "connections[i].addresses[j].country");
            country_code = try json.stringOf(alloc, "connections[i].addresses[j].countryCode");
            j = j + 1;
        }

        i = i + 1;
    }
}