C
C
WordPress API Key Authentication with miniOrange Plugin
See more WordPress Examples
Demonstrates API key authentication using the miniOrange API Authentication plugin.Chilkat C Downloads
#include <C_CkHttp.h>
#include <C_CkStringBuilder.h>
#include <C_CkJsonArray.h>
#include <C_CkDtObj.h>
#include <C_CkJsonObject.h>
void ChilkatSample(void)
{
BOOL success;
HCkHttp http;
HCkStringBuilder sbResponseBody;
HCkJsonArray jarrResp;
int respStatusCode;
HCkDtObj date_gmt;
HCkJsonObject json;
int id;
const char *date;
const char *guidRendered;
const char *modified;
const char *modified_gmt;
const char *slug;
const char *status;
const char *v_type;
const char *link;
const char *titleRendered;
const char *contentRendered;
BOOL contentProtected;
const char *excerptRendered;
BOOL excerptProtected;
int author;
int featured_media;
const char *comment_status;
const char *ping_status;
BOOL sticky;
const char *template;
const char *format;
int j;
int count_j;
int intVal;
const char *href;
BOOL embeddable;
int count;
const char *taxonomy;
const char *name;
BOOL templated;
int i;
int count_i;
success = FALSE;
// This example assumes the Chilkat API to have been previously unlocked.
// See Global Unlock Sample for sample code.
http = CkHttp_Create();
// Use your API key here, such as TaVFjSBu8IMR0MbvZNn7A6P04GXrbtHm
// This causes the "Authorization: Bearer <api_key>" header to be added to each HTTP request.
CkHttp_putAuthToken(http,"api_key");
sbResponseBody = CkStringBuilder_Create();
success = CkHttp_QuickGetSb(http,"https://www.yoursite.com/wp-json/wp/v2/posts?page=1",sbResponseBody);
if (success == FALSE) {
printf("%s\n",CkHttp_lastErrorText(http));
CkHttp_Dispose(http);
CkStringBuilder_Dispose(sbResponseBody);
return;
}
jarrResp = CkJsonArray_Create();
CkJsonArray_LoadSb(jarrResp,sbResponseBody);
CkJsonArray_putEmitCompact(jarrResp,FALSE);
printf("Response Body:\n");
printf("%s\n",CkJsonArray_emit(jarrResp));
respStatusCode = CkHttp_getLastStatus(http);
printf("Response Status Code = %d\n",respStatusCode);
if (respStatusCode >= 400) {
printf("Response Header:\n");
printf("%s\n",CkHttp_lastHeader(http));
printf("Failed.\n");
CkHttp_Dispose(http);
CkStringBuilder_Dispose(sbResponseBody);
CkJsonArray_Dispose(jarrResp);
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
// Chilkat functions returning "const char *" return a pointer to temporary internal memory owned and managed by Chilkat.
// See this example explaining how this memory should be used: const char * functions.
date_gmt = CkDtObj_Create();
i = 0;
count_i = CkJsonArray_getSize(jarrResp);
while (i < count_i) {
json = CkJsonArray_ObjectAt(jarrResp,i);
id = CkJsonObject_IntOf(json,"id");
date = CkJsonObject_stringOf(json,"date");
CkJsonObject_DtOf(json,"date_gmt",FALSE,date_gmt);
guidRendered = CkJsonObject_stringOf(json,"guid.rendered");
modified = CkJsonObject_stringOf(json,"modified");
modified_gmt = CkJsonObject_stringOf(json,"modified_gmt");
slug = CkJsonObject_stringOf(json,"slug");
status = CkJsonObject_stringOf(json,"status");
v_type = CkJsonObject_stringOf(json,"type");
link = CkJsonObject_stringOf(json,"link");
titleRendered = CkJsonObject_stringOf(json,"title.rendered");
contentRendered = CkJsonObject_stringOf(json,"content.rendered");
contentProtected = CkJsonObject_BoolOf(json,"content.protected");
excerptRendered = CkJsonObject_stringOf(json,"excerpt.rendered");
excerptProtected = CkJsonObject_BoolOf(json,"excerpt.protected");
author = CkJsonObject_IntOf(json,"author");
featured_media = CkJsonObject_IntOf(json,"featured_media");
comment_status = CkJsonObject_stringOf(json,"comment_status");
ping_status = CkJsonObject_stringOf(json,"ping_status");
sticky = CkJsonObject_BoolOf(json,"sticky");
template = CkJsonObject_stringOf(json,"template");
format = CkJsonObject_stringOf(json,"format");
j = 0;
count_j = CkJsonObject_SizeOfArray(json,"meta");
while (j < count_j) {
CkJsonObject_putJ(json,j);
j = j + 1;
}
j = 0;
count_j = CkJsonObject_SizeOfArray(json,"categories");
while (j < count_j) {
CkJsonObject_putJ(json,j);
intVal = CkJsonObject_IntOf(json,"categories[j]");
j = j + 1;
}
j = 0;
count_j = CkJsonObject_SizeOfArray(json,"tags");
while (j < count_j) {
CkJsonObject_putJ(json,j);
intVal = CkJsonObject_IntOf(json,"tags[j]");
j = j + 1;
}
j = 0;
count_j = CkJsonObject_SizeOfArray(json,"_links.self");
while (j < count_j) {
CkJsonObject_putJ(json,j);
href = CkJsonObject_stringOf(json,"_links.self[j].href");
j = j + 1;
}
j = 0;
count_j = CkJsonObject_SizeOfArray(json,"_links.collection");
while (j < count_j) {
CkJsonObject_putJ(json,j);
href = CkJsonObject_stringOf(json,"_links.collection[j].href");
j = j + 1;
}
j = 0;
count_j = CkJsonObject_SizeOfArray(json,"_links.about");
while (j < count_j) {
CkJsonObject_putJ(json,j);
href = CkJsonObject_stringOf(json,"_links.about[j].href");
j = j + 1;
}
j = 0;
count_j = CkJsonObject_SizeOfArray(json,"_links.author");
while (j < count_j) {
CkJsonObject_putJ(json,j);
embeddable = CkJsonObject_BoolOf(json,"_links.author[j].embeddable");
href = CkJsonObject_stringOf(json,"_links.author[j].href");
j = j + 1;
}
j = 0;
count_j = CkJsonObject_SizeOfArray(json,"_links.replies");
while (j < count_j) {
CkJsonObject_putJ(json,j);
embeddable = CkJsonObject_BoolOf(json,"_links.replies[j].embeddable");
href = CkJsonObject_stringOf(json,"_links.replies[j].href");
j = j + 1;
}
j = 0;
count_j = CkJsonObject_SizeOfArray(json,"_links.version-history");
while (j < count_j) {
CkJsonObject_putJ(json,j);
count = CkJsonObject_IntOf(json,"_links.version-history[j].count");
href = CkJsonObject_stringOf(json,"_links.version-history[j].href");
j = j + 1;
}
j = 0;
count_j = CkJsonObject_SizeOfArray(json,"_links.predecessor-version");
while (j < count_j) {
CkJsonObject_putJ(json,j);
id = CkJsonObject_IntOf(json,"_links.predecessor-version[j].id");
href = CkJsonObject_stringOf(json,"_links.predecessor-version[j].href");
j = j + 1;
}
j = 0;
count_j = CkJsonObject_SizeOfArray(json,"_links.wp:attachment");
while (j < count_j) {
CkJsonObject_putJ(json,j);
href = CkJsonObject_stringOf(json,"_links.wp:attachment[j].href");
j = j + 1;
}
j = 0;
count_j = CkJsonObject_SizeOfArray(json,"_links.wp:term");
while (j < count_j) {
CkJsonObject_putJ(json,j);
taxonomy = CkJsonObject_stringOf(json,"_links.wp:term[j].taxonomy");
embeddable = CkJsonObject_BoolOf(json,"_links.wp:term[j].embeddable");
href = CkJsonObject_stringOf(json,"_links.wp:term[j].href");
j = j + 1;
}
j = 0;
count_j = CkJsonObject_SizeOfArray(json,"_links.curies");
while (j < count_j) {
CkJsonObject_putJ(json,j);
name = CkJsonObject_stringOf(json,"_links.curies[j].name");
href = CkJsonObject_stringOf(json,"_links.curies[j].href");
templated = CkJsonObject_BoolOf(json,"_links.curies[j].templated");
j = j + 1;
}
CkJsonObject_Dispose(json);
i = i + 1;
}
CkHttp_Dispose(http);
CkStringBuilder_Dispose(sbResponseBody);
CkJsonArray_Dispose(jarrResp);
CkDtObj_Dispose(date_gmt);
}