Zig Requires Chilkat v11.0.0+
Zig
Get the Photos for a User
See more Facebook Examples
Demonstrates how to get the photos that the user has uploaded.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.
// 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 by setting a limit.
rest.addQueryParam("limit", "5") catch {};
// Gets 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.
const response_json = 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)});
// A sample JSON response is shown below.
// This is the code to parse the JSON response.
const dtime = try chilkat.DateTime.init();
defer dtime.deinit();
const b_local_time = true;
const dt = try chilkat.DtObj.init();
defer dt.deinit();
var i: i32 = 0;
const num_items = json.sizeOfArray("data");
while (i < num_items) {
json.setI(i);
std.debug.print("--- {d}\n", .{i});
if (json.stringOf(alloc, "data[i].name")) |name| {
std.debug.print("name: {s}\n", .{name});
} else |_| {}
std.debug.print("id: {s}\n", .{try json.stringOf(alloc, "data[i].id")});
// We can load the created_time into a CkDateTime...
dtime.setFromTimestamp(try json.stringOf(alloc, "data[i].created_time")) catch {};
dtime.toDtObj(b_local_time, dt);
std.debug.print("{d}/{d}/{d} {d}:{d}\n", .{ dt.getMonth(), dt.getDay(), dt.getYear(), dt.getHour(), dt.getMinute() });
i = i + 1;
}
// We can get the paging information as follows:
std.debug.print("URL for next page: {s}\n", .{try json.stringOf(alloc, "paging.next")});
std.debug.print("before cursor: {s}\n", .{try json.stringOf(alloc, "paging.cursors.before")});
std.debug.print("after cursor: {s}\n", .{try json.stringOf(alloc, "paging.cursors.after")});
// This is a sample JSON response:
// {
// "data": [
// {
// "created_time": "2016-09-29T20:46:18+0000",
// "name": "Ignore my posts -- I'm doing some testing for Facebook related programming...",
// "id": "10210199026347451"
// },
// {
// "created_time": "2016-09-19T02:00:42+0000",
// "id": "10210091531240138"
// },
// {
// "created_time": "2016-09-19T02:00:42+0000",
// "id": "10210091520620125"
// },
// {
// "created_time": "2016-09-19T01:59:46+0000",
// "name": "I would've went for a swim had it not been for the sign",
// "id": "10210091522299917"
// },
// {
// "created_time": "2016-09-12T00:37:35+0000",
// "id": "10210023316834798"
// }
// ],
// "paging": {
// "cursors": {
// "before": "MTAyMTAxOTkwMjYzNDc0NTEZD",
// "after": "MTAyMTAwMjMzMTU4MzQ3OTgZD"
// },
// "next": "https:\/\/graph.facebook.com\/v2.7\/10224048320139890\/photos?type=uploaded&limit=5&after=MTAyMTAwMjMzMTU4MzQ3OTgZD"
// }
// }
//
}