Sample code for 30+ languages & platforms
Zig

ABN AMRO OAuth2 Client Credentials Authentication

See more ABN AMRO Examples

Demonstrates how to obtain an access token for an ABN AMRO online API using OAuth2 with the Client Credentials flow.

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

    // This example sends the following CURL request:

    //     curl -X POST -k https://auth-sandbox.connect.abnamro.com:8443/as/token.oauth2 \
    //     -v \
    //     --cert TPPCertificate.crt \
    //     --key TPPprivateKey.key \
    //     -H 'Cache-Control: no-cache' \
    //     -H 'Content-Type: application/x-www-form-urlencoded' \
    //     -d 'grant_type=client_credentials&client_id=TPP_test&scope=psd2:payment:sepa:write psd2:payment:sepa:read'

    const cert = try chilkat.Cert.init();
    defer cert.deinit();
    cert.loadFromFile("qa_data/certs/TPPCertificate.cer") catch {
        std.debug.print("{s}\n", .{try cert.getLastErrorText(alloc)});
        return;
    };

    const bd_key = try chilkat.BinData.init();
    defer bd_key.deinit();
    try bd_key.loadFile("qa_data/certs/TPPprivateKey.key");

    const priv_key = try chilkat.PrivateKey.init();
    defer priv_key.deinit();
    priv_key.loadAnyFormat(bd_key, "passwordIfNeeded") catch {
        std.debug.print("{s}\n", .{try priv_key.getLastErrorText(alloc)});
        return;
    };

    cert.setPrivateKey(priv_key) catch {
        std.debug.print("{s}\n", .{try cert.getLastErrorText(alloc)});
        return;
    };

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

    http.setSslClientCert(cert) catch {
        std.debug.print("{s}\n", .{try http.getLastErrorText(alloc)});
        return;
    };

    const req = try chilkat.HttpRequest.init();
    defer req.deinit();
    req.addParam("grant_type", "client_credentials");
    req.addParam("client_id", "TPP_test");
    req.addParam("scope", "psd2:payment:sepa:write psd2:payment:sepa:read");

    req.setHttpVerb("POST");
    req.setContentType("application/x-www-form-urlencoded");

    const resp = try chilkat.HttpResponse.init();
    defer resp.deinit();
    http.httpReq("https://auth-sandbox.connect.abnamro.com:8443/as/token.oauth2", req, resp) catch {
        std.debug.print("{s}\n", .{try http.getLastErrorText(alloc)});
        return;
    };

    if (resp.getStatusCode() != 200) {
        std.debug.print("{s}\n", .{try resp.getBodyStr(alloc)});
        return;
    }

    // Get the JSON result:
    // {"access_token":"TIhycwl8rfrZPkXGw15mwldASAAK","token_type":"Bearer","expires_in":7200}
    const json = try chilkat.JsonObject.init();
    defer json.deinit();
    json.load(try resp.getBodyStr(alloc)) catch {};
    std.debug.print("access_token: {s}\n", .{try json.stringOf(alloc, "access_token")});
    std.debug.print("token_type: {s}\n", .{try json.stringOf(alloc, "token_type")});
    std.debug.print("expires_in: {s}\n", .{try json.stringOf(alloc, "expires_in")});
}