Java
Chilkat2 Java
WordPress API Key Authentication with miniOrange Plugin
See more WordPress Examples
Demonstrates API key authentication using the miniOrange API Authentication plugin.Chilkat Java Downloads
import com.chilkatsoft.chilkat2.ChilkatException;
import com.chilkatsoft.chilkat2.DtObj;
import com.chilkatsoft.chilkat2.Http;
import com.chilkatsoft.chilkat2.JsonArray;
import com.chilkatsoft.chilkat2.JsonObject;
import com.chilkatsoft.chilkat2.StringBuilder;
public class ChilkatExample {
public static void main(String[] args) {
// This example assumes the Chilkat API to have been previously unlocked.
// See Global Unlock Sample for sample code.
Http http = new Http();
// 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");
StringBuilder sbResponseBody = new StringBuilder();
try {
http.quickGetSb("https://www.yoursite.com/wp-json/wp/v2/posts?page=1", sbResponseBody);
} catch (ChilkatException e) {
System.out.println(e.getLastErrorText());
return;
}
JsonArray jarrResp = new JsonArray();
jarrResp.loadSb(sbResponseBody);
jarrResp.setEmitCompact(false);
System.out.println("Response Body:");
System.out.println(jarrResp.emit());
int respStatusCode = http.getLastStatus();
System.out.println("Response Status Code = " + respStatusCode);
if (respStatusCode >= 400) {
System.out.println("Response Header:");
System.out.println(http.getLastHeader());
System.out.println("Failed.");
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
DtObj date_gmt = new DtObj();
JsonObject json = null;
int id = 0;
String date = "";
String guidRendered = "";
String modified = "";
String modified_gmt = "";
String slug = "";
String status = "";
String v_type = "";
String link = "";
String titleRendered = "";
String contentRendered = "";
boolean contentProtected = false;
String excerptRendered = "";
boolean excerptProtected = false;
int author = 0;
int featured_media = 0;
String comment_status = "";
String ping_status = "";
boolean sticky = false;
String template = "";
String format = "";
int j = 0;
int count_j = 0;
int intVal = 0;
String href = "";
boolean embeddable = false;
int count = 0;
String taxonomy = "";
String name = "";
boolean templated = false;
int i = 0;
int count_i = jarrResp.getSize();
while (i < count_i) {
json = jarrResp.objectAt(i);
id = json.intOf("id");
date = json.stringOf("date");
json.dtOf("date_gmt", false, date_gmt);
guidRendered = json.stringOf("guid.rendered");
modified = json.stringOf("modified");
modified_gmt = json.stringOf("modified_gmt");
slug = json.stringOf("slug");
status = json.stringOf("status");
v_type = json.stringOf("type");
link = json.stringOf("link");
titleRendered = json.stringOf("title.rendered");
contentRendered = json.stringOf("content.rendered");
contentProtected = json.boolOf("content.protected");
excerptRendered = json.stringOf("excerpt.rendered");
excerptProtected = json.boolOf("excerpt.protected");
author = json.intOf("author");
featured_media = json.intOf("featured_media");
comment_status = json.stringOf("comment_status");
ping_status = json.stringOf("ping_status");
sticky = json.boolOf("sticky");
template = json.stringOf("template");
format = json.stringOf("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);
intVal = json.intOf("categories[j]");
j = j + 1;
}
j = 0;
count_j = json.sizeOfArray("tags");
while (j < count_j) {
json.setJ(j);
intVal = json.intOf("tags[j]");
j = j + 1;
}
j = 0;
count_j = json.sizeOfArray("_links.self");
while (j < count_j) {
json.setJ(j);
href = json.stringOf("_links.self[j].href");
j = j + 1;
}
j = 0;
count_j = json.sizeOfArray("_links.collection");
while (j < count_j) {
json.setJ(j);
href = json.stringOf("_links.collection[j].href");
j = j + 1;
}
j = 0;
count_j = json.sizeOfArray("_links.about");
while (j < count_j) {
json.setJ(j);
href = json.stringOf("_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 = json.stringOf("_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 = json.stringOf("_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 = json.stringOf("_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 = json.stringOf("_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 = json.stringOf("_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 = json.stringOf("_links.wp:term[j].taxonomy");
embeddable = json.boolOf("_links.wp:term[j].embeddable");
href = json.stringOf("_links.wp:term[j].href");
j = j + 1;
}
j = 0;
count_j = json.sizeOfArray("_links.curies");
while (j < count_j) {
json.setJ(j);
name = json.stringOf("_links.curies[j].name");
href = json.stringOf("_links.curies[j].href");
templated = json.boolOf("_links.curies[j].templated");
j = j + 1;
}
json.close();
i = i + 1;
}
}
}