Sample code for 30+ languages & platforms
Zig

Shopware 6 - Get OAuth2 Access Token using Username and Password

See more Shopware 6 Examples

Demonstrates how to get an OAuth2 access token using your username and password.

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

    // Sends the following POST

    //     {
    //         "client_id": "administration",
    //         "grant_type": "password",
    //         "scopes": "write",
    //         "username": "<user-username>",
    //         "password": "<user-password>"
    //     }

    // Use this online tool to generate code from sample JSON:
    // Generate Code to Create JSON

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

    const json = try chilkat.JsonObject.init();
    defer json.deinit();
    json.updateString("client_id", "administration") catch {};
    json.updateString("grant_type", "password") catch {};
    json.updateString("scopes", "write") catch {};
    json.updateString("username", "<user-username>") catch {};
    json.updateString("password", "<user-password>") catch {};

    const resp = try chilkat.HttpResponse.init();
    defer resp.deinit();
    http.httpJson("POST", "https://my-shopware-6-shop.de/api/oauth/token", 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:
    // (Sample code for parsing the JSON response is shown below)

    // {
    //   "token_type": "Bearer",
    //   "expires_in": 600,
    //   "access_token": "xxxxxxxxxxxxxx",
    //   "refresh_token": "token"
    // }

    // Sample code for parsing the JSON response...
    // Use the following online tool to generate parsing code from sample JSON:
    // Generate Parsing Code from JSON

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

    // Save our JSON to a file (to be used in other examples..)
    try j_resp.writeFile("qa_data/tokens/shopware6.json");
}