Sample code for 30+ languages & platforms
Zig

Google Search Console API - List

See more Google Search Console Examples

Lists the user's Search Console sites.

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 uses a previously obtained access token having permission for the
    // Google Search Console scope.

    // In this example, Get a Google Search Console OAuth2 Access Token, the access
    // token was saved to a JSON file.  This example fetches the access token from the file..
    const json_token = try chilkat.JsonObject.init();
    defer json_token.deinit();
    try json_token.loadFile("qa_data/tokens/googleSearchConsole.json");
    if (!json_token.hasMember("access_token")) {
        std.debug.print("No access token found.\n", .{});
        return;
    }

    const http = try chilkat.Http.init();
    defer http.deinit();
    http.setAuthToken(try json_token.stringOf(alloc, "access_token"));

    const response_str = http.quickGetStr(alloc, "https://www.googleapis.com/webmasters/v3/sites") catch {
        std.debug.print("{s}\n", .{try http.getLastErrorText(alloc)});
        return;
    };

    const status_code = http.getLastStatus();
    std.debug.print("Response Status Code: {d}\n", .{status_code});

    // Sample response:

    // {
    //  "siteEntry": [
    //   {
    //    "siteUrl": "https://www.example.com/",
    //    "permissionLevel": "siteUnverifiedUser"
    //   },
    //   {
    //    "siteUrl": "http://www.chilkatsoft.com/",
    //    "permissionLevel": "siteOwner"
    //   }
    //  ]
    // }

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

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

    const json = try chilkat.JsonObject.init();
    defer json.deinit();

    try json.load(response_str);

    var site_url: [:0]const u8 = "";
    var permission_level: [:0]const u8 = "";

    var i: i32 = 0;
    const count_i = json.sizeOfArray("siteEntry");
    while (i < count_i) {
        json.setI(i);
        site_url = try json.stringOf(alloc, "siteEntry[i].siteUrl");
        std.debug.print("siteUrl: {s}\n", .{site_url});
        permission_level = try json.stringOf(alloc, "siteEntry[i].permissionLevel");
        std.debug.print("permissionLevel: {s}\n", .{permission_level});
        i = i + 1;
    }
}