Zig
Zig
Configure Google Service Account Authentication for REST
See more REST Examples
Demonstrates Rest.SetAuthGoogle, which associates an AuthGoogle provider so Chilkat supplies a Google OAuth 2.0 bearer token on each request. The provider is configured with a service account JSON key and the required OAuth scope.
The file path is relative to the application's current working directory. An absolute path may also be used. Supply the path appropriate to your own environment.
Background. The AuthGoogle provider performs a service-account (server-to-server) OAuth flow. For interactive, browser-based user consent, the OAuth2 provider is used instead.
Chilkat Zig Downloads
const std = @import("std");
const chilkat = @import("chilkat");
pub fn main(init: std.process.Init) !void {
const alloc = init.arena.allocator();
const rest = try chilkat.Rest.init();
defer rest.deinit();
const b_tls = true;
const b_auto_reconnect = true;
rest.connect("example.com", 443, b_tls, b_auto_reconnect) catch {
std.debug.print("{s}\n", .{try rest.getLastErrorText(alloc)});
return;
};
// Configure Google service-account authentication. Load the service account JSON key and set the
// required OAuth scope(s).
const g_auth = try chilkat.AuthGoogle.init();
defer g_auth.deinit();
const sb_json_key = try chilkat.StringBuilder.init();
defer sb_json_key.deinit();
sb_json_key.loadFile("qa_data/service_account.json", "utf-8") catch {
std.debug.print("Failed to load the service account JSON key.\n", .{});
return;
};
const json_key = sb_json_key.getAsString(alloc) catch {
std.debug.print("{s}\n", .{try sb_json_key.getLastErrorText(alloc)});
return;
};
g_auth.setJsonKey(json_key);
// A space-delimited list of the requested permissions.
g_auth.setScope("https://www.googleapis.com/auth/cloud-platform");
// Associate the provider so Chilkat supplies a Google bearer token on each request.
rest.setAuthGoogle(g_auth) catch {
std.debug.print("{s}\n", .{try rest.getLastErrorText(alloc)});
return;
};
const response_text = rest.fullRequestNoBody(alloc, "GET", "/storage/v1/b?project=my-project") catch {
std.debug.print("{s}\n", .{try rest.getLastErrorText(alloc)});
return;
};
std.debug.print("{s}\n", .{response_text});
}