Sample code for 30+ languages & platforms
Zig

GeoOp Exchange Refresh Token for New Access Token

See more GeoOp Examples

Demonstrates how to use the /oauth2/token endpoint to exchange it for a new access token once the current access token has expired.

Note: This example requires Chilkat v9.5.0.65 or greater.

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 assumes the Chilkat API to have been previously unlocked.
    // See Global Unlock Sample for sample code.

    // This example also assumes that OAuth2 access and refresh tokens were previously fetched.
    // and saved in a JSON file.

    // First get our previously obtained refresh token.
    // { .... "refresh_token":"e6dqdG....mzjpT04w==", .... }
    const json_token = try chilkat.JsonObject.init();
    defer json_token.deinit();
    try json_token.loadFile("qa_data/tokens/geoop.json");

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

    // Connect to GeoOp...
    const b_auto_reconnect = true;
    rest.connect("login.geoop.com", 443, true, b_auto_reconnect) catch {
        std.debug.print("{s}\n", .{try rest.getLastErrorText(alloc)});
        return;
    };

    // Set the X-Version header.
    rest.addHeader("X-Version", "1.0") catch {};

    // Provide the required form params to get the new access token
    std.debug.print("refresh_token = {s}\n", .{try json_token.stringOf(alloc, "refresh_token")});
    rest.addQueryParam("refresh_token", try json_token.stringOf(alloc, "refresh_token")) catch {};
    rest.addQueryParam("grant_type", "refresh_token") catch {};
    rest.addQueryParam("client_id", "GEOOP-CLIENT-ID") catch {};
    rest.addQueryParam("client_secret", "GEOOP-CLIENT-SECRET") catch {};

    const response_body = rest.fullRequestFormUrlEncoded(alloc, "POST", "/oauth2/token") catch {
        std.debug.print("{s}\n", .{try rest.getLastErrorText(alloc)});
        return;
    };

    // If the response status code did not indicate success, then see what happened..
    if (rest.getResponseStatusCode() != 200) {
        std.debug.print("Request Header: \n", .{});
        std.debug.print("{s}\n", .{try rest.getLastRequestHeader(alloc)});
        std.debug.print("----\n", .{});
        std.debug.print("Response StatusCode = {d}\n", .{rest.getResponseStatusCode()});
        std.debug.print("Response StatusLine: {s}\n", .{try rest.getResponseStatusText(alloc)});
        std.debug.print("Response Header:\n", .{});
        std.debug.print("{s}\n", .{try rest.getResponseHeader(alloc)});
        std.debug.print("{s}\n", .{response_body});
        return;
    }

    const json = try chilkat.JsonObject.init();
    defer json.deinit();
    json.setEmitCompact(false);
    json.load(response_body) catch {};

    // Show the full JSON response.  It should contain the new access token...
    std.debug.print("{s}\n", .{try json.emit(alloc)});
}