Zig
Zig
Google Contacts - Adding/Updating a photo for a contact
See more Google APIs Examples
Demonstrates how to upload a photo for a Google Contact.Chilkat Zig Downloads
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 upload a photo for the contact, send the following:
// PUT /m8/feeds/photos/media/default/contactId
// If-match: Etag
// Content-Type: image/*
// ...
// [Photo data bytes]
rest.addHeader("GData-Version", "3.0") catch {};
rest.addHeader("If-Match", "*") catch {};
rest.addHeader("Content-Type", "image/*") catch {};
const sb_path = try chilkat.StringBuilder.init();
defer sb_path.deinit();
sb_path.append("/m8/feeds/photos/media/default/{contactId}") catch {};
// Upload a photo for the contact having contactId = "1ea2e4fe0ef24e09"
_ = sb_path.replace("{contactId}", "1ea2e4fe0ef24e09");
// Let's get our photo data..
const png_data = try chilkat.BinData.init();
defer png_data.deinit();
try png_data.loadFile("qa_data/png/dude.png");
const sb_response_body = try chilkat.StringBuilder.init();
defer sb_response_body.deinit();
rest.fullRequestBd("PUT", try sb_path.getAsString(alloc), png_data, sb_response_body) catch {
std.debug.print("{s}\n", .{try rest.getLastErrorText(alloc)});
return;
};
// A successful response will have a status code equal to 200.
if (rest.getResponseStatusCode() != 200) {
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)});
return;
}
// If the 200 success response was received.
std.debug.print("Response Body:\n", .{});
std.debug.print("{s}\n", .{try sb_response_body.getAsString(alloc)});
std.debug.print("Success.\n", .{});
}