Sample code for 30+ languages & platforms
Zig

WooCommerce OAuth 1.0a “one-legged” Authentication

See more REST Examples

Demonstrates how to use OAuth 1.0a "one legged" authentication with Woo Commerce.

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();

    // Demonstrates how to do OAuth1 authentication for a Wordpress site using Woo Commerce.

    // This example requires the Chilkat API to have been previously unlocked.
    // See Global Unlock Sample for sample code.

    // Prepare an OAuth 1.0 object for use with the Chilkat REST API.
    const oauth1 = try chilkat.OAuth1.init();
    defer oauth1.deinit();
    oauth1.setConsumerKey("WOO_COMMERCE_CONSUMER_KEY");
    oauth1.setConsumerSecret("WOO_COMMERCE_CONSUMER_SECRET");

    // The signature method can be HMAC-SHA1 or HMAC-SHA256
    oauth1.setSignatureMethod("HMAC-SHA256");

    // The OauthUrl property will need to be updated each time a request is sent.
    // The domain here must match the domain passed to the Connect method (below).
    // The domain must be exact.  For example, "www.your-wordpress-site.com" vs. "your-wordpress-site.com".
    // One might work while the other does not..
    oauth1.setOauthUrl("http://your-wordpress-site.com/wc-api/v3/orders");

    // The OAuthMethod property will be set automatically when the REST request is sent.
    // Setting it here is not actually necessary.
    oauth1.setOauthMethod("GET");

    // Generate an initial nonce so that Chilkat knows the desired size of the nonce.
    try oauth1.genNonce(32);

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

    // Tell the REST object to use the OAuth1 object for authentication.
    // Also, indicate that the OAuth authentication parameters should be query parameters
    // and not located within the Authorization header.
    const b_use_query_params = true;
    try rest.setAuthOAuth1(oauth1, b_use_query_params);

    // Make the initial connection (without sending a request yet) to the WooCommerce endpoint at your Wordpress blog.
    const b_tls = false;
    const port = 80;
    const b_auto_reconnect = true;
    rest.connect("your-wordpress-site.com", port, b_tls, b_auto_reconnect) catch {
        std.debug.print("{s}\n", .{try rest.getLastErrorText(alloc)});
        return;
    };

    // Send a GET request to list orders.

    // When the request is sent, the OAuth1 object's Timestamp and Nonce properties are automatically
    // regenerated.  Also, the OAuth1 object's OauthMethod property is automatically set to the HTTP method
    // used for the request (in this case it is "GET").
    const response_json = rest.fullRequestNoBody(alloc, "GET", "/wc-api/v3/orders") catch {
        std.debug.print("{s}\n", .{try rest.getLastErrorText(alloc)});
        return;
    };

    // When successful, the response status code will equal 200.
    if (rest.getResponseStatusCode() != 200) {
        // Examine the request/response to see what happened.
        std.debug.print("response status code = {d}\n", .{rest.getResponseStatusCode()});
        std.debug.print("response status text = {s}\n", .{try rest.getResponseStatusText(alloc)});
        std.debug.print("response header: {s}\n", .{try rest.getResponseHeader(alloc)});
        std.debug.print("response body (if any): {s}\n", .{response_json});
        std.debug.print("---\n", .{});
        std.debug.print("LastRequestStartLine: {s}\n", .{try rest.getLastRequestStartLine(alloc)});
        std.debug.print("LastRequestHeader: {s}\n", .{try rest.getLastRequestHeader(alloc)});
        return;
    }

    std.debug.print("{s}\n", .{response_json});
    std.debug.print("Success.\n", .{});
}