Zig
Zig
Google Search Console API - Add
See more Google Search Console Examples
Demonstrates how to add a site to the set of the user's sites in Search Console.Chilkat Zig Downloads
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 sb_url_to_add = try chilkat.StringBuilder.init();
defer sb_url_to_add.deinit();
sb_url_to_add.append("https://www.example.com/") catch {};
sb_url_to_add.encode("url", "utf-8") catch {};
const sb_url = try chilkat.StringBuilder.init();
defer sb_url.deinit();
sb_url.append("https://www.googleapis.com/webmasters/v3/sites/") catch {};
sb_url.appendSb(sb_url_to_add) catch {};
// Our URL looks like this: https://www.googleapis.com/webmasters/v3/sites/https%3A%2F%2Fwww.example.com%2F
std.debug.print("{s}\n", .{try sb_url.getAsString(alloc)});
const url = try sb_url.getAsString(alloc);
const resp = try chilkat.HttpResponse.init();
defer resp.deinit();
http.httpNoBody("PUT", url, resp) catch {
std.debug.print("{s}\n", .{try http.getLastErrorText(alloc)});
return;
};
const status_code = resp.getStatusCode();
std.debug.print("Response Status Code: {d}\n", .{status_code});
if (status_code >= 400) {
std.debug.print("{s}\n", .{try resp.getBodyStr(alloc)});
std.debug.print("Failed.\n", .{});
}
std.debug.print("Success\n", .{});
}