Zig Requires Chilkat v11.5.0+
Zig
Box: Refresh and Save Rotated OAuth Tokens
See more Box Examples
Refresh the user token saved by the authorization example. Box refresh tokens are single-use and expire after 60 days. Serialize refreshes for a token pair and save the replacement immediately; do not run concurrent refresh jobs.
See the Box API guide for setup and the complete workflow.
Chilkat Zig Downloads
const std = @import("std");
const chilkat = @import("chilkat");
pub fn main(init: std.process.Init) !void {
const alloc = init.arena.allocator();
var success: bool = false;
// Requires Chilkat 11.5.0 or later and a previously unlocked Chilkat bundle.
// See Global Unlock Sample.
// Guide: Box API Guide.
success = false;
const tokens = try chilkat.JsonObject.init();
defer tokens.deinit();
tokens.loadFile("qa_data/tokens/box_oauth.json") catch {
std.debug.print("{s}\n", .{try tokens.getLastErrorText(alloc)});
return;
};
const oauth2 = try chilkat.OAuth2.init();
defer oauth2.deinit();
oauth2.setTokenEndpoint("https://api.box.com/oauth2/token");
oauth2.setClientId("BOX_CLIENT_ID");
oauth2.setClientSecret("BOX_CLIENT_SECRET");
oauth2.setRefreshToken(try tokens.stringOf(alloc, "refresh_token"));
oauth2.refreshAccessToken() catch {
std.debug.print("{s}\n", .{try oauth2.getLastErrorText(alloc)});
return;
};
tokens.load(try oauth2.getAccessTokenResponse(alloc)) catch {
std.debug.print("{s}\n", .{try tokens.getLastErrorText(alloc)});
return;
};
// The previous refresh token is now invalid. Preserve this response even if
// your normal token-store write fails. Do not retry with the old refresh token.
tokens.writeFile("qa_data/tokens/box_oauth.json") catch {
std.debug.print("{s}\n", .{try tokens.getLastErrorText(alloc)});
return;
};
std.debug.print("New access and refresh tokens saved. Do not log their values.\n", .{});
}