Zig
Zig
List Files in Google Drive
See more Google Drive Examples
Demonstrates how to list files in Google Drive.See Google Drive Files list for more optional HTTP parameters.
Chilkat Zig Downloads
const std = @import("std");
const chilkat = @import("chilkat");
pub fn main(init: std.process.Init) !void {
const alloc = init.arena.allocator();
// It requires the Chilkat API to have been previously unlocked.
// See Global Unlock Sample for sample code.
// This example uses a previously obtained access token having permission for the
// Google Drive scope.
const g_auth = try chilkat.AuthGoogle.init();
defer g_auth.deinit();
g_auth.setAccessToken("GOOGLE-DRIVE-ACCESS-TOKEN");
const rest = try chilkat.Rest.init();
defer rest.deinit();
// Connect using TLS.
const b_auto_reconnect = true;
try rest.connect("www.googleapis.com", 443, true, b_auto_reconnect);
// Provide the authentication credentials (i.e. the access token)
rest.setAuthGoogle(g_auth) catch {};
// Get 4 results per page. (The default page size is 100, with a max of 1000.
rest.addQueryParam("pageSize", "4") catch {};
// This uses the Google Drive V3 API... (not V2)
const json_response = rest.fullRequestNoBody(alloc, "GET", "/drive/v3/files") 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("request header = {s}\n", .{try rest.getLastRequestHeader(alloc)});
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 JSON: {s}\n", .{json_response});
return;
}
// A successful response looks like this:
// {
// "kind": "drive#fileList",
// "files": [
// {
// "kind": "drive#file",
// "id": "0B53Q6OSTWYolenpjTEU4ekJlQUU",
// "name": "test",
// "mimeType": "application/vnd.google-apps.folder"
// },
// {
// "kind": "drive#file",
// "id": "0B53Q6OSTWYolRm4ycjZtdXhRaEE",
// "name": "starfish4.jpg",
// "mimeType": "image/jpeg"
// },
// {
// "kind": "drive#file",
// "id": "0B53Q6OSTWYolMWt2VzN0Qlo1UjA",
// "name": "hamlet2.xml",
// "mimeType": "text/xml"
// },
// ...
// {
// "kind": "drive#file",
// "id": "0B53Q6OSTWYolc3RhcnRlcl9maWxlX2Rhc2hlclYw",
// "name": "Getting started",
// "mimeType": "application/pdf"
// }
// ]
// }
// Iterate over each file in the response and show the name, id, and mimeType.
const json = try chilkat.JsonObject.init();
defer json.deinit();
json.load(json_response) catch {};
// Show the full JSON response.
json.setEmitCompact(false);
std.debug.print("{s}\n", .{try json.emit(alloc)});
const num_files = json.sizeOfArray("files");
var i: i32 = 0;
while (i < num_files) {
json.setI(i);
std.debug.print("name: {s}\n", .{try json.stringOf(alloc, "files[i].name")});
std.debug.print("id: {s}\n", .{try json.stringOf(alloc, "files[i].id")});
std.debug.print("mimeType: {s}\n", .{try json.stringOf(alloc, "files[i].mimeType")});
std.debug.print("-\n", .{});
i = i + 1;
}
}