Sample code for 30+ languages & platforms
Zig

Refresh WiX Access Token

See more WiX Examples

Request a new access token each time you call a WiX API. Use the refresh token together with your secret key, to request refresh tokens

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.

    const http = try chilkat.Http.init();
    defer http.deinit();

    // Implements the following CURL command:

    // curl -X POST \
    //   https://www.wix.com/oauth/access \
    //   -H 'Content-Type: application/json' \
    //   -d '{
    //     "grant_type": "refresh_token",
    //     "client_id": <CLIENT_ID>,
    //     "client_secret": <CLIENT_SECRET>,
    //     "refresh_token": <REFRESH_TOKEN>
    // }'

    // It is assumed we previously obtained an OAuth2 access token.
    // This example loads the JSON access token file
    // saved by this example: Get WiX OAuth2 Access Token

    const json_token = try chilkat.JsonObject.init();
    defer json_token.deinit();
    json_token.loadFile("qa_data/tokens/wix.json") catch {
        std.debug.print("Failed to load square.json\n", .{});
        return;
    };

    // Get the "refresh_token"
    var refresh_token: [:0]const u8 = try json_token.stringOf(alloc, "refresh_token");

    // The following JSON is sent in the request body.

    // {
    //   "grant_type": "refresh_token",
    //   "client_id": <APP_ID>,
    //   "client_secret": <APP_SECRET>,
    //   "refresh_token": <REFRESH_TOKEN>
    // }

    const json = try chilkat.JsonObject.init();
    defer json.deinit();
    json.updateString("grant_type", "refresh_token") catch {};
    json.updateString("client_id", "CLIENT_ID") catch {};
    json.updateString("client_secret", "CLIENT_SECRET") catch {};
    json.updateString("refresh_token", refresh_token) catch {};

    const resp = try chilkat.HttpResponse.init();
    defer resp.deinit();
    http.httpJson("POST", "https://www.wix.com/oauth/access", json, "application/json", resp) catch {
        std.debug.print("{s}\n", .{try http.getLastErrorText(alloc)});
        return;
    };

    const sb_response_body = try chilkat.StringBuilder.init();
    defer sb_response_body.deinit();
    resp.getBodySb(sb_response_body) catch {};
    const j_resp = try chilkat.JsonObject.init();
    defer j_resp.deinit();
    j_resp.loadSb(sb_response_body) catch {};
    j_resp.setEmitCompact(false);

    std.debug.print("Response Body:\n", .{});
    std.debug.print("{s}\n", .{try j_resp.emit(alloc)});

    const resp_status_code = resp.getStatusCode();
    std.debug.print("Response Status Code = {d}\n", .{resp_status_code});
    if (resp_status_code >= 400) {
        std.debug.print("Response Header:\n", .{});
        std.debug.print("{s}\n", .{try resp.getHeader(alloc)});
        std.debug.print("Failed.\n", .{});
        return;
    }

    // Sample JSON response:

    // {
    //   "refresh_token": "OAUTH2.eyJraWQ ... vnB4cQ",
    //   "access_token": "OAUTH2.eyJra ... la18lrw"
    // }

    refresh_token = try j_resp.stringOf(alloc, "refresh_token");
    _ = try j_resp.stringOf(alloc, "access_token");

    // Save the new JSON access token response to a file.
    sb_response_body.writeFile("qa_data/tokens/wix.json", "utf-8", false) catch {};
}