Sample code for 30+ languages & platforms
Zig

Paging User Photos with Cursor

See more Facebook Examples

Demonstrates how to iterate over the pages of user photos using a cursor.

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.

    // This example assumes a previously obtained an access token
    const oauth2 = try chilkat.OAuth2.init();
    defer oauth2.deinit();
    oauth2.setAccessToken("FACEBOOK-ACCESS-TOKEN");

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

    // Connect to Facebook.
    rest.connect("graph.facebook.com", 443, true, true) catch {
        std.debug.print("{s}\n", .{try rest.getLastErrorText(alloc)});
        return;
    };

    // Provide the authentication credentials (i.e. the access key)
    rest.setAuthOAuth2(oauth2) catch {};

    // Indicate that we only want the photos the user has personally uploaded.
    rest.addQueryParam("type", "uploaded") catch {};

    // We could limit the number of photos per page using the "limit" field.
    rest.addQueryParam("limit", "20") catch {};

    // Get the 1st page of photos. (Not the actual image data, but the information about each photo.)
    // See https://developers.facebook.com/docs/graph-api/reference/user/photos/ for more information.
    var response_json: [:0]const u8 = rest.fullRequestNoBody(alloc, "GET", "/v2.7/me/photos") catch {
        std.debug.print("{s}\n", .{try rest.getLastErrorText(alloc)});
        return;
    };

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

    // See Parsing the Facebook User Photos for code showing how to parse the JSON photos content.
    //

    // Get the "after" cursor.
    var after_cursor: [:0]const u8 = try json.stringOf(alloc, "paging.cursors.after");
    while (json.getLastMethodSuccess()) {
        std.debug.print("after cursor: {s}\n", .{after_cursor});

        // Prepare for getting the next page of photos.
        // We can continue using the same REST object.
        // If already connected, we'll continue using the existing connection.
        // Otherwise, a new connection will automatically be made if needed.
        rest.clearAllQueryParams() catch {};
        rest.addQueryParam("type", "uploaded") catch {};
        rest.addQueryParam("limit", "20") catch {};
        rest.addQueryParam("after", after_cursor) catch {};

        response_json = rest.fullRequestNoBody(alloc, "GET", "/v2.7/me/photos") catch {
            std.debug.print("{s}\n", .{try rest.getLastErrorText(alloc)});
            return;
        };

        json.load(response_json) catch {};
        // See Parsing the Facebook User Photos for code showing how to parse the JSON photos content.

        std.debug.print("{s}\n", .{try json.emit(alloc)});

        // Get the cursor for the next page.
        after_cursor = try json.stringOf(alloc, "paging.cursors.after");
    }

    std.debug.print("No more pages of photos.\n", .{});
}