Zig
Zig
WordPress API Key Authentication with miniOrange Plugin
See more WordPress Examples
Demonstrates API key authentication using the miniOrange API Authentication plugin.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 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();
// Use your API key here, such as TaVFjSBu8IMR0MbvZNn7A6P04GXrbtHm
// This causes the "Authorization: Bearer <api_key>" header to be added to each HTTP request.
http.setAuthToken("api_key");
const sb_response_body = try chilkat.StringBuilder.init();
defer sb_response_body.deinit();
http.quickGetSb("https://www.yoursite.com/wp-json/wp/v2/posts?page=1", sb_response_body) catch {
std.debug.print("{s}\n", .{try http.getLastErrorText(alloc)});
return;
};
const jarr_resp = try chilkat.JsonArray.init();
defer jarr_resp.deinit();
jarr_resp.loadSb(sb_response_body) catch {};
jarr_resp.setEmitCompact(false);
std.debug.print("Response Body:\n", .{});
std.debug.print("{s}\n", .{try jarr_resp.emit(alloc)});
const resp_status_code = http.getLastStatus();
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 http.getLastHeader(alloc)});
std.debug.print("Failed.\n", .{});
return;
}
// Sample JSON response:
// (Sample code for parsing the JSON response is shown below)
// [
// {
// "id": 1902,
// "date": "2020-11-16T09:54:09",
// "date_gmt": "2020-11-16T16:54:09",
// "guid": {
// "rendered": "http:\/\/cknotes.com\/?p=1902"
// },
// "modified": "2020-11-16T09:54:09",
// "modified_gmt": "2020-11-16T16:54:09",
// "slug": "xero-redirect-uri-for-oauth2-and-desktop-apps",
// "status": "publish",
// "type": "post",
// "link": "https:\/\/cknotes.com\/xero-redirect-uri-for-oauth2-and-desktop-apps\/",
// "title": {
// "rendered": "Xero Redirect URI for OAuth2 and Desktop Apps"
// },
// "content": {
// "rendered": "<p>...",
// "protected": false
// },
// "excerpt": {
// "rendered": "<p>...",
// "protected": false
// },
// "author": 1,
// "featured_media": 0,
// "comment_status": "closed",
// "ping_status": "open",
// "sticky": false,
// "template": "",
// "format": "standard",
// "meta": [
// ],
// "categories": [
// 815
// ],
// "tags": [
// 594,
// 816
// ],
// "_links": {
// "self": [
// {
// "href": "https:\/\/cknotes.com\/wp-json\/wp\/v2\/posts\/1902"
// }
// ],
// "collection": [
// {
// "href": "https:\/\/cknotes.com\/wp-json\/wp\/v2\/posts"
// }
// ],
// "about": [
// {
// "href": "https:\/\/cknotes.com\/wp-json\/wp\/v2\/types\/post"
// }
// ],
// "author": [
// {
// "embeddable": true,
// "href": "https:\/\/cknotes.com\/wp-json\/wp\/v2\/users\/1"
// }
// ],
// "replies": [
// {
// "embeddable": true,
// "href": "https:\/\/cknotes.com\/wp-json\/wp\/v2\/comments?post=1902"
// }
// ],
// "version-history": [
// {
// "count": 1,
// "href": "https:\/\/cknotes.com\/wp-json\/wp\/v2\/posts\/1902\/revisions"
// }
// ],
// "predecessor-version": [
// {
// "id": 1904,
// "href": "https:\/\/cknotes.com\/wp-json\/wp\/v2\/posts\/1902\/revisions\/1904"
// }
// ],
// "wp:attachment": [
// {
// "href": "https:\/\/cknotes.com\/wp-json\/wp\/v2\/media?parent=1902"
// }
// ],
// "wp:term": [
// {
// "taxonomy": "category",
// "embeddable": true,
// "href": "https:\/\/cknotes.com\/wp-json\/wp\/v2\/categories?post=1902"
// },
// {
// "taxonomy": "post_tag",
// "embeddable": true,
// "href": "https:\/\/cknotes.com\/wp-json\/wp\/v2\/tags?post=1902"
// }
// ],
// "curies": [
// {
// "name": "wp",
// "href": "https:\/\/api.w.org\/{rel}",
// "templated": true
// }
// ]
// }
// },
// ...
// ]
// Sample code for parsing the JSON response...
// Use the following online tool to generate parsing code from sample JSON:
// Generate Parsing Code from JSON
const date_gmt = try chilkat.DtObj.init();
defer date_gmt.deinit();
var id: i32 = 0;
var date: [:0]const u8 = "";
var guid_rendered: [:0]const u8 = "";
var modified: [:0]const u8 = "";
var modified_gmt: [:0]const u8 = "";
var slug: [:0]const u8 = "";
var status: [:0]const u8 = "";
var v_type: [:0]const u8 = "";
var link: [:0]const u8 = "";
var title_rendered: [:0]const u8 = "";
var content_rendered: [:0]const u8 = "";
var content_protected: bool = false;
var excerpt_rendered: [:0]const u8 = "";
var excerpt_protected: bool = false;
var author: i32 = 0;
var featured_media: i32 = 0;
var comment_status: [:0]const u8 = "";
var ping_status: [:0]const u8 = "";
var sticky: bool = false;
var template: [:0]const u8 = "";
var format: [:0]const u8 = "";
var j: i32 = 0;
var count_j: i32 = 0;
var int_val: i32 = 0;
var href: [:0]const u8 = "";
var embeddable: bool = false;
var count: i32 = 0;
var taxonomy: [:0]const u8 = "";
var name: [:0]const u8 = "";
var templated: bool = false;
var i: i32 = 0;
const count_i = jarr_resp.getSize();
while (i < count_i) {
const json = try jarr_resp.objectAt(i);
defer json.deinit();
id = json.intOf("id");
date = try json.stringOf(alloc, "date");
json.dtOf("date_gmt", false, date_gmt) catch {};
guid_rendered = try json.stringOf(alloc, "guid.rendered");
modified = try json.stringOf(alloc, "modified");
modified_gmt = try json.stringOf(alloc, "modified_gmt");
slug = try json.stringOf(alloc, "slug");
status = try json.stringOf(alloc, "status");
v_type = try json.stringOf(alloc, "type");
link = try json.stringOf(alloc, "link");
title_rendered = try json.stringOf(alloc, "title.rendered");
content_rendered = try json.stringOf(alloc, "content.rendered");
content_protected = json.boolOf("content.protected");
excerpt_rendered = try json.stringOf(alloc, "excerpt.rendered");
excerpt_protected = json.boolOf("excerpt.protected");
author = json.intOf("author");
featured_media = json.intOf("featured_media");
comment_status = try json.stringOf(alloc, "comment_status");
ping_status = try json.stringOf(alloc, "ping_status");
sticky = json.boolOf("sticky");
template = try json.stringOf(alloc, "template");
format = try json.stringOf(alloc, "format");
j = 0;
count_j = json.sizeOfArray("meta");
while (j < count_j) {
json.setJ(j);
j = j + 1;
}
j = 0;
count_j = json.sizeOfArray("categories");
while (j < count_j) {
json.setJ(j);
int_val = json.intOf("categories[j]");
j = j + 1;
}
j = 0;
count_j = json.sizeOfArray("tags");
while (j < count_j) {
json.setJ(j);
int_val = json.intOf("tags[j]");
j = j + 1;
}
j = 0;
count_j = json.sizeOfArray("_links.self");
while (j < count_j) {
json.setJ(j);
href = try json.stringOf(alloc, "_links.self[j].href");
j = j + 1;
}
j = 0;
count_j = json.sizeOfArray("_links.collection");
while (j < count_j) {
json.setJ(j);
href = try json.stringOf(alloc, "_links.collection[j].href");
j = j + 1;
}
j = 0;
count_j = json.sizeOfArray("_links.about");
while (j < count_j) {
json.setJ(j);
href = try json.stringOf(alloc, "_links.about[j].href");
j = j + 1;
}
j = 0;
count_j = json.sizeOfArray("_links.author");
while (j < count_j) {
json.setJ(j);
embeddable = json.boolOf("_links.author[j].embeddable");
href = try json.stringOf(alloc, "_links.author[j].href");
j = j + 1;
}
j = 0;
count_j = json.sizeOfArray("_links.replies");
while (j < count_j) {
json.setJ(j);
embeddable = json.boolOf("_links.replies[j].embeddable");
href = try json.stringOf(alloc, "_links.replies[j].href");
j = j + 1;
}
j = 0;
count_j = json.sizeOfArray("_links.version-history");
while (j < count_j) {
json.setJ(j);
count = json.intOf("_links.version-history[j].count");
href = try json.stringOf(alloc, "_links.version-history[j].href");
j = j + 1;
}
j = 0;
count_j = json.sizeOfArray("_links.predecessor-version");
while (j < count_j) {
json.setJ(j);
id = json.intOf("_links.predecessor-version[j].id");
href = try json.stringOf(alloc, "_links.predecessor-version[j].href");
j = j + 1;
}
j = 0;
count_j = json.sizeOfArray("_links.wp:attachment");
while (j < count_j) {
json.setJ(j);
href = try json.stringOf(alloc, "_links.wp:attachment[j].href");
j = j + 1;
}
j = 0;
count_j = json.sizeOfArray("_links.wp:term");
while (j < count_j) {
json.setJ(j);
taxonomy = try json.stringOf(alloc, "_links.wp:term[j].taxonomy");
embeddable = json.boolOf("_links.wp:term[j].embeddable");
href = try json.stringOf(alloc, "_links.wp:term[j].href");
j = j + 1;
}
j = 0;
count_j = json.sizeOfArray("_links.curies");
while (j < count_j) {
json.setJ(j);
name = try json.stringOf(alloc, "_links.curies[j].name");
href = try json.stringOf(alloc, "_links.curies[j].href");
templated = json.boolOf("_links.curies[j].templated");
j = j + 1;
}
i = i + 1;
}
}