Sample code for 30+ languages & platforms
Zig

Configure OAuth 1.0a Authentication for REST

See more REST Examples

Demonstrates Rest.SetAuthOAuth1, which associates an OAuth1 provider so Chilkat computes the OAuth 1.0/1.0a signature for each request. The second argument selects whether OAuth parameters are placed in the query string or the Authorization header.

Background. OAuth 1.0a signs each request using the consumer key/secret and the access token/secret. Chilkat generates the nonce, timestamp, and signature automatically for every request sent through the object.

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

    const rest = try chilkat.Rest.init();
    defer rest.deinit();
    const b_tls = true;
    const b_auto_reconnect = true;
    rest.connect("example.com", 443, b_tls, b_auto_reconnect) catch {
        std.debug.print("{s}\n", .{try rest.getLastErrorText(alloc)});
        return;
    };

    // Configure OAuth 1.0a request signing.  Provide the consumer credentials and the access token
    // credentials; Chilkat computes the OAuth signature for each request.
    const oauth1 = try chilkat.OAuth1.init();
    defer oauth1.deinit();
    // Normally you would not hard-code secrets in source.  You should instead obtain them from an
    // interactive prompt, environment variable, or a secrets vault.
    oauth1.setConsumerKey("CONSUMER_KEY");
    oauth1.setConsumerSecret("CONSUMER_SECRET");
    oauth1.setToken("ACCESS_TOKEN");
    oauth1.setTokenSecret("ACCESS_TOKEN_SECRET");
    oauth1.setSignatureMethod("HMAC-SHA1");

    // The 2nd argument selects whether OAuth parameters are placed in the query string (true) or the
    // Authorization header (false).
    const b_use_query_params = false;
    rest.setAuthOAuth1(oauth1, b_use_query_params) catch {
        std.debug.print("{s}\n", .{try rest.getLastErrorText(alloc)});
        return;
    };

    const response_text = rest.fullRequestNoBody(alloc, "GET", "/api/resource") catch {
        std.debug.print("{s}\n", .{try rest.getLastErrorText(alloc)});
        return;
    };

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