Sample code for 30+ languages & platforms
Zig

Create Google Photos Album

See more Google Photos Examples

Demonstrates how to create a Google Photos album.

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.

    // Get the previously obtained access token.
    // See Get Google Photos Access Token.

    const json_token = try chilkat.JsonObject.init();
    defer json_token.deinit();
    json_token.loadFile("qa_data/tokens/googlePhotos.json") catch {
        std.debug.print("{s}\n", .{try json_token.getLastErrorText(alloc)});
        return;
    };

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

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

    // Create an album named "animals"
    const json = try chilkat.JsonObject.init();
    defer json.deinit();
    json.updateString("album.title", "animals") catch {};

    const resp = try chilkat.HttpResponse.init();
    defer resp.deinit();
    http.httpJson("POST", "https://photoslibrary.googleapis.com/v1/albums", json, "application/json", resp) catch {
        std.debug.print("{s}\n", .{try http.getLastErrorText(alloc)});
        return;
    };

    // Show the response body.
    std.debug.print("{s}\n", .{try resp.getBodyStr(alloc)});

    // Examine the response status code.  Success is indicated by a status code of 200.
    std.debug.print("response status code: {d}\n", .{resp.getStatusCode()});

    json.load(try resp.getBodyStr(alloc)) catch {};

    // Sample response:

    // {
    //   "id": "AKcbugHaQTvUKSi3M2RQxOhxhdEaLc5mfUcqFoIU_kpQaROyUD70BcFt7_mnz5PcwwsjPKeKnLHN",
    //   "title": "animals",
    //   "productUrl": "https://photos.google.com/lr/album/AKcbugHaQTvUKSi3M2RQxOhxhdEaLc5mfUcqFoIU_kpQaROyUD70BcFt7_mnz5PcwwsjPKeKnLHN",
    //   "isWriteable": true
    // }

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

    const id = try json.stringOf(alloc, "id");
    _ = try json.stringOf(alloc, "title");
    _ = try json.stringOf(alloc, "productUrl");
    _ = json.boolOf("isWriteable");

    std.debug.print("id = {s}\n", .{id});
}