Sample code for 30+ languages & platforms
Zig

Download Google Contact Photo

See more Google APIs Examples

Demonstrates how to download Google Contact's photo.

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 requires the Chilkat API to have been previously unlocked.
    // See Global Unlock Sample for sample code.

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

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

    const g_auth = try chilkat.AuthGoogle.init();
    defer g_auth.deinit();
    g_auth.setAccessToken(try json_token.stringOf(alloc, "access_token"));

    const rest = try chilkat.Rest.init();
    defer rest.deinit();

    // Connect using TLS.
    const b_auto_reconnect = true;
    try rest.connect("www.google.com", 443, true, b_auto_reconnect);

    // Provide the authentication credentials (i.e. the access token)
    rest.setAuthGoogle(g_auth) catch {};

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

    // To get the photo, send the following:

    //     GET /m8/feeds/photos/media/default/contactId

    rest.addHeader("GData-Version", "3.0") catch {};

    const sb_path = try chilkat.StringBuilder.init();
    defer sb_path.deinit();
    // Get the photo for the contact having contactId = "1ea2e4fe0ef24e09"
    const contact_id = "1ea2e4fe0ef24e09";
    sb_path.setString("/m8/feeds/photos/media/default/{contactId}") catch {};
    _ = sb_path.replace("{contactId}", contact_id);

    const image_data = try chilkat.BinData.init();
    defer image_data.deinit();
    rest.fullRequestNoBodyBd("GET", try sb_path.getAsString(alloc), image_data) catch {
        std.debug.print("{s}\n", .{try rest.getLastErrorText(alloc)});
        return;
    };

    // A 404 response indicates the contact has no photo.
    // (We could've first fetched the contact information, parsed out the
    // photo etag, and then if no photo etag existed, we'd know the contact has no
    // photo.  Or... we can just try to download the photo and if a 404 is received,
    // we know there's no photo.  Much simpler.)
    if (rest.getResponseStatusCode() == 404) {
        std.debug.print("This contact has no photo.\n", .{});
        return;
    }

    // A successful response will have a status code equal to 200.
    if (rest.getResponseStatusCode() != 200) {
        // If the response was not successful, then the response body
        // does not contain image data.  Instead it contains XML.
        const sb_response_body = try chilkat.StringBuilder.init();
        defer sb_response_body.deinit();
        sb_response_body.appendBd(image_data, "utf-8", 0, 0) catch {};

        std.debug.print("response status code = {d}\n", .{rest.getResponseStatusCode()});
        std.debug.print("response status text = {s}\n", .{try rest.getResponseStatusText(alloc)});
        std.debug.print("response header: {s}\n", .{try rest.getResponseHeader(alloc)});
        std.debug.print("response body: {s}\n", .{try sb_response_body.getAsString(alloc)});
        std.debug.print("request startline: {s}\n", .{try rest.getLastRequestStartLine(alloc)});
        std.debug.print("request header: {s}\n", .{try rest.getLastRequestHeader(alloc)});
        return;
    }

    // Examine the content-type in the response header so we know what file
    // extension to use (.jpg, .png, etc.)
    const sb_content_type = try chilkat.StringBuilder.init();
    defer sb_content_type.deinit();
    sb_content_type.append(try rest.responseHdrByName(alloc, "Content-Type")) catch {};

    if (sb_content_type.contentsEqual("image/jpeg", false)) {
        image_data.writeFile("qa_output/contact_photo.jpg") catch {};
    }

    if (sb_content_type.contentsEqual("image/png", false)) {
        image_data.writeFile("qa_output/contact_photo.png") catch {};
    }

    std.debug.print("Content-Type: {s}\n", .{try sb_content_type.getAsString(alloc)});
    std.debug.print("Success.\n", .{});
}