Zig Requires Chilkat v11.0.0+
Zig
Shopify Private Authentication for Private Apps
See more Shopify Examples
Shopify private authentication is for interacting with your own store through private applications. It uses HTTP "Basic" authentication with your Shopify private application key and secret key.This example demonstrates how to send a private authenticated request using Chilkat Http, and then the same using Chilkat Rest.
Chilkat Zig Downloads
const std = @import("std");
const chilkat = @import("chilkat");
pub fn main(init: std.process.Init) !void {
const alloc = init.arena.allocator();
// This example requires the Chilkat API to have been previously unlocked.
// See Global Unlock Sample for sample code.
// First demonstrate sending a simple request using Shopify private authentication w/ the Chilkat Http API.
const http = try chilkat.Http.init();
defer http.deinit();
// To use HTTP Basic Authentication with any HTTP request, we simply set the Login, Password, and BasicAuth properties.
// Important: All HTTP requests using Basic authentication must be over SSL/TLS.
http.setLogin("SHOPIFY_PRIVATE_API_KEY");
http.setPassword("SHOPIFY_PRIVATE_API_SECRET_KEY");
http.setBasicAuth(true);
// Make sure to replace "chilkat" with your store name.
const resp = try chilkat.HttpResponse.init();
defer resp.deinit();
http.httpNoBody("GET", "https://chilkat.myshopify.com/admin/products.json", resp) catch {
std.debug.print("{s}\n", .{try http.getLastErrorText(alloc)});
return;
};
// Examine the response code.
if (resp.getStatusCode() != 200) {
std.debug.print("Received error response code: {d}\n", .{resp.getStatusCode()});
std.debug.print("Response body:\n", .{});
std.debug.print("{s}\n", .{try resp.getBodyStr(alloc)});
return;
}
// Success.
std.debug.print("Success.\n", .{});
// Examine the JSON response
const sb_json = try chilkat.StringBuilder.init();
defer sb_json.deinit();
resp.getBodySb(sb_json) catch {};
const json = try chilkat.JsonObject.init();
defer json.deinit();
json.loadSb(sb_json) catch {};
json.setEmitCompact(false);
std.debug.print("{s}\n", .{try json.emit(alloc)});
// -------------------------------------------------
// Now let's do the same using the Chilkat Rest API.
const rest = try chilkat.Rest.init();
defer rest.deinit();
// Provide the private app credentials:
rest.setAuthBasic("SHOPIFY_PRIVATE_API_KEY", "SHOPIFY_PRIVATE_API_SECRET_KEY") catch {};
// Connect to the shopify server.
const b_tls = true;
const port = 443;
const b_auto_reconnect = true;
// Make sure to replace "chilkat" with your store name.
rest.connect("chilkat.myshopify.com", port, b_tls, b_auto_reconnect) catch {
std.debug.print("{s}\n", .{try rest.getLastErrorText(alloc)});
return;
};
sb_json.clear();
rest.fullRequestNoBodySb("GET", "/admin/products.json", sb_json) catch {
std.debug.print("{s}\n", .{try rest.getLastErrorText(alloc)});
return;
};
if (rest.getResponseStatusCode() != 200) {
std.debug.print("Received error response code: {d}\n", .{rest.getResponseStatusCode()});
std.debug.print("Response body:\n", .{});
std.debug.print("{s}\n", .{try sb_json.getAsString(alloc)});
return;
}
// Success...
json.loadSb(sb_json) catch {};
std.debug.print("{s}\n", .{try json.emit(alloc)});
}